Implementing a Broken Power Law as a fitting function in Origin

前端 未结 1 809
星月不相逢
星月不相逢 2021-01-25 14:03

Good day, I\'m trying to use the function builder in origin (OriginLab) to create a new function to fit with, the broken power law (http://en.wikipedia.org/wiki/Power_law#Broken

1条回答
  •  孤城傲影
    2021-01-25 14:32

    Even though the question asks for a suggestion using OriginLab, this one is using Python as the OP has accepted to give it a try!

    The curve-fitting method that exists in Python is from the Scipy package (curve_fit). All python packages for windows can be downloaded in a jiffy from THIS WEBSITE HERE!

    When doing a curve fit, the first thing one needs to know is the fitting equation. As you already know the broken power law equation that will fit your data, you are ready to go.

    The code for fitting an example data, lets call them x and y. Here the fitting parameters are a1 and a2.

    import numpy as np # This is the Numpy module
    from scipy.optimize import curve_fit # The module that contains the curve_fit routine
    import matplotlib.pyplot as plt # This is the matplotlib module which we use for plotting the result
    
    """ Below is the function that returns the final y according to the conditions """
    def fitfunc(x,a1,a2):
        y1 = (x**(a1) )[xxc]
        y3 = (0)[x==xc]
        y = np.concatenate((y1,y2,y3))
        return y
    
    x = Your x data here
    y = Your y data here
    
    """ In the above code, we have imported 3 modules, namely Numpy, Scipy and matplotlib """
    
    popt,pcov = curve_fit(fitfunc,x,y,p0=(10.0,1.0)) #here we provide random initial parameters a1,a2
    
    a1 = popt[0] 
    a2 = popt[1]
    residuals = y - fitfunc(x,a1,a2)
    chi-sq = sum( (residuals**2)/fitfunc(x,a1,a2) ) # This is the chi-square for your fitted curve
    
    """ Now if you need to plot, perform the code below """
    curvey = fitfunc(x,a1,a2) # This is your y axis fit-line
    
    plt.plot(x, curvey, 'red', label='The best-fit line')
    plt.scatter(x,y, c='b',label='The data points')
    plt.legend(loc='best')
    plt.show()
    

    Just insert your data here and it should work just fine!! For more detailed information if needed on how the code works, CHECK OUT THIS WEBSITE I couldn't find a proper example for your fit-function, so x and y have been left blank. But once you have your data, just plug them in!

    0 讨论(0)
提交回复
热议问题