使用幂律拟合方程

时间:2018-02-07 07:55:27

标签: python python-3.x curve-fitting

请问如何使用powerlaw

拟合下面的等式
S_u = S_u0*(p_new/p_i)**-alpha

S_up_new/p_i知道S_u0alpha是未知数。

1 个答案:

答案 0 :(得分:0)

你可以使用lmfit(http://lmfit.github.io/lmfit-py/)和

之类的东西
import numpy as np
from lmfit import Model

# get data into numpy ndarrays.  If in a simple data file, 
# with columns of data, that might look like:
data = np.loadtxt('datafile name')
p   = data[0, :]
s_u = data[1, :]

# define your model function (independent var in first argument)
def mod_su(p, su0=1, alpha=1):  # (values used as starting guesss)
    return su0 * (p)**(-alpha)

# now define the fitting model
model = Model(mod_su)

# make a set of parameters (for 'su0' and 'alpha'):
params = model.make_params(su0=10)  # can also set initial values here

# optionally, put min/max bounds on parameters:
params['alpha'].min = 0.0
params['su0'].min = 0.0
params['su0'].max = 1e6

# run the fit with Model.fit(Data_Array, Parameters, independent vars)
result = model.fit(s_u, params, p=p)

# print report with results and fitting statistics
print(result.fit_report())

# plot data and best fit
import matplotlib.pyplot as plt
plt.plot(p, s_u, label='data')
plt.plot(p, result.best_fit, label='fit')
plt.show()

希望有所帮助。