user9259974
user9259974

Reputation:

Fitting equation using the power law

Please how can i fit the equation below using the powerlaw

S_u = S_u0*(p_new/p_i)**-alpha

S_u and p_new/p_i are know whiles S_u0 and alpha are unknowns.

Upvotes: 1

Views: 2759

Answers (1)

M Newville
M Newville

Reputation: 7862

You could use lmfit (http://lmfit.github.io/lmfit-py/) and something like

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()

Hope that helps.

Upvotes: 1

Related Questions