lsqcurvefit issues due to variables being several orders of magnitude different
6 次查看(过去 30 天)
显示 更早的评论
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
qn7 = @(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(qn7, coeff0, V, data(:,2),lb,ub);
plot(data(:,1),data(:,2),'x',data(:,1),qn7(vals,V),'b-')
5 个评论
回答(1 个)
Torsten
2024-7-31
编辑:Torsten
2024-7-31
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 = [1e-5 10]; %initial guess
Eqn7 = @(coeff, VV) coeff(1)*sqrt(coeff(2))*...
(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));
plot(data(:,1),data(:,2),'x',data(:,1),Eqn7(vals,V),'b-')
format long
vals(1)
vals(1)=vals(1)*sqrt(2*pi*mi)/(e^1.5*Area*100^3);
vals(1)
vals(2)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!