I wish to fitting data with a 2D function in order to extract parameter (a) and (b) using lmfit package. Basically as a 1D function fitting, I am trying to fit every data point to the 2D function at the same coordinates (x,y). This is mean that every data point has its initial guess value which different from another data point since each data has different coordinate(x,y). This is my code:
#!/usr/bin/ python
import pyfits
import numpy as np
import math
from lmfit import minimize, Parameters, Parameter, report_errors,report_fit,
conf_interval, printfuncs
xn =np.linspace(0,3,4) # x-component
yn =np.linspace(0,3,4) # y-component
data= [0.0, 0.16, 0.33, 0.5, 0.2, 0.26, 0.38, 0.53, 0.4, 0.43, 0.52, 0.64, 0.6, 0.62,
0.67, 0.78] # (x1,y1) generate (data[0]), (x1,y2) generate (data[1]) and so on
params = Parameters()
params.add('a', value=3)
params.add('b', value=5)
def residual(params,x,y,data=None):
a = params['a'].value # parameter
b = params['b'].value # parameter
model=(x**2/a**2+y**2/b**2)**0.5 # 2D function
if data is None:
return data
return model - data
out=minimize(residual,params,args=(x,y,data,)) # lmfit minimizer
final=data+out.residual
report_fit(params)
ci = conf_interval(out, sigmas=[0.68,0.95]) # confidence interval
printfuncs.report_ci(ci)
However, I got this error message:
ValueError: operands could not be broadcast together with shapes (4) (16)
Obviously, dimension of x and y is not the same of data, but I don't know how to make data[0] takes (x1,y1), data[1] takes (x1,y2)..., data[5] takes (x2,y1) and so on. Please could anyone help me to fix this problem or make any suggestion, thank you in advance.