美好的一天, 我试图在原点(OriginLab)中使用函数构建器创建一个适合的新函数,破坏的幂律(http://en.wikipedia.org/wiki/Power_law#Broken_power_law)
所以,我认为我的实际功能部分已经失效了。为此我用了
if(x<xc)
y =x^a1;
if(x>xc)
y = x^(a1-a2)*x^a2;
if(x==xc)
y = 0;
其中xc,a1和a2是参数。然而,我接着你必须选择一堆东西(参数范围,你运行的脚本来猜测初始值等),我不知道该放什么。有没有人有这方面的经验?
答案 0 :(得分:3)
即使这个问题要求使用OriginLab
的建议,但是这个问题正在使用Python,因为OP已经接受了尝试!
Python中存在的曲线拟合方法来自the Scipy package (curve_fit).所有用于Windows的python包都可以从THIS WEBSITE HERE!
快速下载。在进行曲线拟合时,首先需要知道的是拟合方程。正如您已经知道适合您的数据的破坏的幂律方程式,您已经准备好了。
用于拟合示例数据的代码,可以调用它们x and y
。拟合参数为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()
只需在此处输入您的数据,它应该可以正常工作!!如果需要有关代码如何工作的更详细信息,CHECK OUT THIS WEBSITE 我找不到适合函数的正确示例,因此x和y留空。但是一旦掌握了数据,只需将其插入即可!