Here is the solution. I think lmfit is a good alternative to scipy to fit the curve.
from lmfit import minimize, Parameters, Parameter, report_fit
import numpy as np
# create data to be fitted
xf = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
yf = [0.02,4]
# define objective function: returns the array to be minimized
def fcn2min(params, x, data):
pw = params['pw'].value
adj1 = params['adj1'].value
adj2 = params['adj2'].value
model = adj1 * np.power(x + adj2, pw)
return model - data
pw=2
# create a set of Parameters
params = Parameters()
params.add('pw', value= pw, vary=False)
params.add('adj1', value= 1)
params.add('adj2', value= 1)
# do fit, here with leastsq model
result = minimize(fcn2min, params, args=(xf, yf))
# calculate final result
final = yf + result.residual
# write error report
report_fit(result.params)
adj1=result.params['adj1']
adj2=result.params['adj2']
# try to plot results
x = np.linspace(0, 1, 100)
y = adj1 * np.power(x + adj2, pw)
import pylab
pylab.plot(xf, yf, 'ko')
pylab.plot(x, y, 'r')
pylab.show()
source
share