python - 在 Origin 中将破幂律实现为拟合函数

标签 python curve-fitting originlab

美好的一天, 我正在尝试使用 origin (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 是参数。然而,然后我就到了你必须选择一堆东西(参数范围、你运行来猜测初始值的脚本等)的地步,我不知道要放什么。有人有这方面的经验吗?

最佳答案

尽管问题要求使用 OriginLab 提供建议,但这个问题正在使用 Python,因为 OP 已接受尝试!

Python中存在的曲线拟合方法来自the Scipy package (curve_fit).所有适用于 Windows 的 Python 包都可以从 THIS WEBSITE HERE! 快速下载。

进行曲线拟合时,首先需要知道的是拟合方程。由于您已经知道适合您的数据的 splinter 幂律方程,因此您已准备好开始。

用于拟合示例数据的代码,我们将它们称为x和y。这里的拟合参数是a1和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 留空。但是一旦你有了数据,只需将它们插入即可!

关于python - 在 Origin 中将破幂律实现为拟合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27153048/

相关文章:

user-interface - 如何为 matplotlib 创建前端?

函数的 Python 变量参数

python - ruamel.yaml 在 Ubuntu 容器构建中需要 python-dev

python - 如何使用 "scipy.optimize.curve_fit"平滑地拟合我的数据点?

Python-在数据上拟合对数模型曲线

python - 为什么关键字参数不能位于可变位置参数之前?

emacs 中的 python 3

python-2.7 - Python 条形图上的曲线拟合

matlab - 如何对实验数据进行非线性数据拟合函数