【问题标题】:Implementing a Broken Power Law as a fitting function in Origin在 Origin 中实现破幂律作为拟合函数
【发布时间】:2015-05-30 16:54:42
【问题描述】:

美好的一天, 我正在尝试使用原点 (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里做吗?
  • 嗯,这会更好,因为我对此非常满意。但是,我也熟悉 MATLAB,所以这也是一种选择。不过,从我最初的搜索来看,这也有点棘手。
  • 我已经使用 Python 中的破幂律函数处理了拟合曲线。如果您可以访问 Python,那么我将准备发布答案。只需几行代码即可进行拟合并获得最佳拟合参数
  • 我从来没有真正使用过python。我想我现在可以开始了,因为没有其他选择。将不胜感激。
  • 我的回答有用吗?如果可以的话可以接受吗?

标签: python curve-fitting originlab


【解决方案1】:

尽管问题要求使用 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 留空。但是,一旦您有了数据,只需将它们插入!

【讨论】:

  • chi-sq 在 Python 中不是一个有效的变量名,它应该是 chi_sq
猜你喜欢
  • 2019-06-14
  • 2021-05-30
  • 1970-01-01
  • 2015-11-23
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
相关资源
最近更新 更多