1

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_power_law)

So, I think I got the actual function part down. For this I used

if(x<xc)
    y =x^a1;
if(x>xc)
    y = x^(a1-a2)*x^a2;
if(x==xc)
    y = 0;

Where xc, a1 and a2 are the parameters. However, I then get to the point where you have to choose a bunch of stuff (parameter ranges, script you run to guess initial values, etc) and I have no clue what to put in there. Does anyone have some experience with this?

Srivatsan
  • 9,225
  • 13
  • 58
  • 83
user129412
  • 765
  • 1
  • 6
  • 19
  • should it only be done in OriginLab? – Srivatsan Nov 26 '14 at 17:15
  • Hm, it would be preferable, as I'm pretty comfortable with it and all. However, I'm also familiar with MATLAB, so that too is an option. From my initial searches, that too is a little tricky though. – user129412 Nov 26 '14 at 18:08
  • I have dealt with fitting a curve using a broken power law function in Python. If you have access to Python, then I will be ready to post the answer. It takes only a couple of lines of code to do the fitting and get your best-fit parameters – Srivatsan Nov 26 '14 at 18:44
  • I've never actually used python. I suppose I could start now, for lack of an alternative. Would be much appreciated. – user129412 Nov 26 '14 at 19:24
  • Did my answer work? If so can it be accepted? – Srivatsan Dec 06 '14 at 18:18
  • My bad! Totally forgot to do that last thing. Thanks a lot for your effort, it worked! Still would like to do it in matlab too but this is fine. – user129412 Dec 06 '14 at 18:57

1 Answers1

3

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) )[x<xc]
    y2 = (x**(a1-a2) )[x>xc]
    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!

Srivatsan
  • 9,225
  • 13
  • 58
  • 83