请问如何使用powerlaw
拟合下面的等式S_u = S_u0*(p_new/p_i)**-alpha
S_u
和p_new/p_i
知道S_u0
和alpha
是未知数。
答案 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()
希望有所帮助。