I am trying to use lsqcurvefit in MATLAB to fit an equation to some data in order to solve for a couple variables. I have included the part of the code below that covers this. I am trying to solve for coeff(1) and coeff(2), The problem is that when I run lsqcurvefit, it is just using whatever my initial guesses are and outputting that as the solution. I suspect it is because my values for the coefficients will be several orders of magnitude different. You can kind of get an idea for this by looking at coeff0. Has anyone else run into this problem and/or do you know how to work around it? Any insight would be greatly appreciated.
V = [V1, Vp]; %voltages
a = 1.1792;
b = 0.5;
e = 1.60217662e-19
Area = 4.7909e-7;
mi = 39.948./(6.022e23.*1000);
coeff0 = [7e10 4]; %initial guess
Eqn7 = @(coeff, VV) ((e^1.5)*coeff(1)*Area*sqrt(coeff(2)/(2*pi*mi)))*100^3*... (a*(-VV(:,1)/coeff(2)).^b.*tanh(VV(:,2)/(2*coeff(2))) + ...
(a*(-VV(:,1)/coeff(2)).^b - a*(-(VV(:,1)+VV(:,2))/coeff(2)).^b)./(exp(VV(:,2)/coeff(2))+1));
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','OptimalityTolerance',...
1e-16,'FunctionTolerance',1e-16);
lb = [];
ub = [];
[vals, resnorm, out, flag] = lsqcurvefit(Eqn7, coeff0, V, Ip(:,point),lb,ub);
plot(data(:,1),data(:,2),'x',data(:,1),Eqn7(vals,V),'b-')
I can manually tune in the initial guesses so that the curve is very close to what it should be so I know my initial guesses aren’t bad guesses.
Andrew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.