problem with lsqcurvefit: initial values are not updated
4 views (last 30 days)
Show older comments
Hi,
I'm trying to perform some data fitting using the following function:
function F = myfun(x,xdata)
%fitting parameters
m = x(1);
g = x(2);
tu = x(3);
td = x(4);
%some constants
T = 0.145;
nc = 2.09e11;
hb = 6.582e-16;
mb = 5.788e-5;
kb = 8.617e-5;
ej = 1.60218e-19;
me = 9.10994e-31;
e = 1.60218e-19;
%
%Define some variables
Ef = (hb*hb*2*pi*nc*1e4/(2*m*me))*ej;
wc = e.*xdata./(m*me);
hw = hb.*wc;
QX = 2.*pi.*pi.*kb.*T./hw;
Ez = g.*mb.*xdata./2;
F = 0.*xdata;
%strart the loop
for i=1:5
A = (2.*i.*QX)./sinh(i.*QX);
B = exp(-i.*pi./(wc.*tu));
C = exp(-i.*pi./(wc.*td));
PHI_U = (2.*pi.*i.*Ef./hw).*(1.+(Ez./Ef))-(pi.*i);
PHI_D = (2.*pi.*i.*Ef./hw).*(1.-(Ez./Ef))-(pi.*i);
F = F + A.*(B.*cos(PHI_U)+C.*cos(PHI_D)).*30;
end
end
When I run the lsqcurvefit using the initial values x0:
[x,resnorm] = lsqcurvefit(@myfun,x0,xdata,ydata)
the fitting seems to start normally, but at the end, nothing is updated. The output values (x) are exactly same as the initial values (x0). I tried to fix this problem by changing the tolerance options, but to no avail! Nothing has so far worked.
Any suggestion/workaround is highly appreciated.
0 Comments
Answers (2)
Star Strider
on 2 Jul 2015
That usually occurs if your initial parameter estimates are either exactly correct or very far off. I would use an optimoptions options structure to see the process, perhaps:
opts = optimoptions('lsqcurvefit', 'Diagnostics','on', 'Display','iter-detailed');
then add it to your lsqcurvefit call. Experiment with these to find the ones that are most helpful in tracking what lsqcurvefit is doing.
1 Comment
Star Strider
on 3 Jul 2015
My pleasure.
I don’t know what your data are, but your ‘B’ and ‘C’ variables are complex exponentials, so ‘F’ is likely also complex. That could be very difficult to fit, even to complex data.
If you know more accurate initial estimates for your parameters, use them. Otherwise, if you have the Global Optimization Toolbox, consider using a genetic algorithm, pattern search, or another routine to estimate your parameters, then use lsqcurvefit to fit them precisely.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!