I am trying to do a nonlinear least squares fit using scipy.optimize.leastsq. I have done this several times pretty easily for fitting some analytical function. However, now I am fitting the parameters of a numerical model to my data. As far as I can tell, the only thing that should change is that the fitting function gets much more complicated (since I have to generate one of these models at every iteration).
My problem is this: leastsq is not varying the parameters, so I keep getting the exact same model and eventually it tells me that 'The cosine of the angle between func(x) and any column of the\n Jacobian is at most 0.000000 in absolute value'
I have played with the epsfcn keyword, increasing it to 0.1 and it still never changes the parameters. What am I doing wrong? I will include some code, but not all since it is quite long:
The function call:
fitout = leastsq(ErrorFunction, pars, args=(chips[i], const_pars), full_output=True, epsfcn = 0.1)
The Error Function:
def ErrorFunction(pars, chip, const_pars):
all_pars = const_pars
all_pars.extend(pars)
print "Pars: ", pars
model = FitFunction(chip, all_pars)
return chip.y - model.y
The Fit Function:
def FitFunction(chip, pars):
print pars
temperature = pars[0]
pressure = pars[1]
co2 = pars[2]
co = pars[3]
o3 = pars[4]
wavenum_start = pars[5]
wavenum_end = pars[6]
ch4 = pars[7]
humidity = pars[8]
resolution = pars[9]
angle = pars[10]
#Generate the model:
model = MakeModel.Main(pressure, temperature, humidity, wavenum_start, wavenum_end, angle, co2, o3, ch4, co, chip.x, resolution)
if "FullSpectrum.freq" in os.listdir(TelluricModelDir):
cmd = "rm "+ TelluricModelDir + "FullSpectrum.freq"
command = subprocess.check_call(cmd, shell=True)
return model
Kevin Gullikson