lsqcurvefit issues due to variables being several orders of magnitude different
I am trying to use lsqcurvefit 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.
load(‘V1.mat’)
load(‘Vp.mat’)
load(‘data.mat’)
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, data(:,2),lb,ub);
plot(data(:,1),data(:,2),’x’,data(:,1),Eqn7(vals,V),’b-‘)I am trying to use lsqcurvefit 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.
load(‘V1.mat’)
load(‘Vp.mat’)
load(‘data.mat’)
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, data(:,2),lb,ub);
plot(data(:,1),data(:,2),’x’,data(:,1),Eqn7(vals,V),’b-‘) I am trying to use lsqcurvefit 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.
load(‘V1.mat’)
load(‘Vp.mat’)
load(‘data.mat’)
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, data(:,2),lb,ub);
plot(data(:,1),data(:,2),’x’,data(:,1),Eqn7(vals,V),’b-‘) curve fitting MATLAB Answers — New Questions