你好,我正在尝试使用函数构建器原点(OriginLab)来创建一个新的函数来与之相适应,即破坏幂律(法)
所以,我想我把实际的功能部分弄掉了。为此我用了
if(x<xc)
y =x^a1;
if(x>xc)
y = x^(a1-a2)*x^a2;
if(x==xc)
y = 0;其中xc、a1和a2是参数。但是,我到了必须选择一堆东西(参数范围、您运行的脚本来猜测初始值等)的程度,我不知道在其中放什么。有人有这方面的经验吗?
发布于 2014-11-26 20:24:28
尽管这个问题要求使用OriginLab的建议,但这个问题是使用Python,因为OP已经接受了尝试!
适合)。中存在的曲线拟合方法是从这个网站在这里!下载的所有用于windows的Python包。
在进行曲线拟合时,首先需要知道的是拟合方程。因为你已经知道了适合你的数据的破幂定律方程,你已经准备好了。
用于拟合示例数据的代码,让我们称它们为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()只要在这里插入你的数据,它就能正常工作!如果需要的话,有关代码如何工作的更详细信息,看一下这个网站 I无法为您的fit函数找到适当的示例,因此x和y被保留为空白。但是一旦你有了你的数据,就把它们插入!
https://stackoverflow.com/questions/27153048
复制相似问题