I recently found lmfit, and am attempting to use it to fit to data in the attached Excel file. I am starting very simple, and following the Gaussian fit tutorial, but using my function and data. The code is as appears below. I am hoping to get a fit comparable to that in the Excel workbook, where the values of the coefficients were simply guessed to give the best fit.
import numpy as np
from numpy import exp, linspace, random, sqrt, pi, append
import matplotlib.pyplot as plt
import xlrd
wb = xlrd.open_workbook("HDDA_Eric.xlsx")
sheet = wb.sheet_by_index(0)
xdata=[]
for i in range(12,sheet.nrows):
xd=sheet.cell_value(i,0)
xdata.append(xd)
measuredBTDF=[]
for i in range(12,sheet.nrows):
yd=sheet.cell_value(i,1)
measuredBTDF.append(yd)
xarray = np.asarray(xdata)
BTDFarray = np.asarray(measuredBTDF)
incident_angle = sheet.cell_value(0,1)
def BSDF2(theta_s,b0,l,s):
theta_0_radx = incident_angle*pi/180
theta_s_radx = theta_s*pi/180
betax = np.sin(theta_0_radx) # this is the sin of the incident angle
beta_0x = np.sin(theta_s_radx) # this is the sin of the measured angle
BSDF2 = b0*(1+((abs(betax - beta_0x))/l)**2)**(-s/2)
return BSDF2
gmodel2 = Model(BSDF2)
#print(gmodel2.param_names)
#print(gmodel2.independent_vars)
params3 = gmodel2.make_params(b0=7.0,l=0.01,s=2.1)
yeval = gmodel2.eval(params3,theta_s=xarray)
result2 = gmodel2.fit(yeval,params3,theta_s=xarray)
print(result2.fit_report())
The problem is that the fit report shows that there is no attempt at finding new values for the coefficients. They are being returned unaltered, at their initial values.
At this point I really do not know where I am going wrong, but I have a feeling it is probably with the BSDF equation itself, and how it uses the absolute value of the angle data. Any light that could be shed on this would be greatly appreciated!