使用幂律拟合方程
Fitting equation using the power law
请问我如何使用幂律拟合下面的方程
S_u = S_u0*(p_new/p_i)**-alpha
S_u
和 p_new/p_i
是已知的,而 S_u0
和 alpha
是未知的。
你可以使用 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()
希望对您有所帮助。
请问我如何使用幂律拟合下面的方程
S_u = S_u0*(p_new/p_i)**-alpha
S_u
和 p_new/p_i
是已知的,而 S_u0
和 alpha
是未知的。
你可以使用 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()
希望对您有所帮助。