Info

This question is closed. Reopen it to edit or answer.

Can't get 'fit' to work in nonlinear least squares mode

1 view (last 30 days)
I'm having trouble getting the fit function to work in NonlinearLeastSquares mode, and was hoping someone could tell me what I am doing wrong. The data is flux values vs wavelength from a black body. For whatever reason, the 'fit' routine never seems to change from the initial conditions. Interestingly, when I use nlinfit, I get an answer that makes sense and is robust to the initial conditions. Here is some sample code and data. I don't think the problem is in the fit equation, since the nlinfit routine works well. The problem likely is in the way I am setting up the fit, but I just don't see it.
clear
close all
x=[5;5.5;6;6.5;7;7.5;8;8.5;9;9.5;10;10.5;11;11.50;12];
y=[4.44e-09;5.97e-09;7.34e-09;8.46e-09;9.35e-09;...
9.95e-09;1.01e-08;1.04e-08;1.03e-08;1.01e-08;...
9.81e-09;9.41e-09;8.95e-09;8.53e-09;8.07e-09];
s=fitoptions('Method', 'NonlinearLeastSquares');
f=fittype('a.*3.741E+04./(x.^5.*(exp(1.438E+04./(x.*T))-1))', 'options', s);
fit1=fit(x, y, f, 'Startpoint', [330 1e-6]);
plot( fit1, 'r-', x, y, 'k.');
xlabel('Wavelength')
ylabel('Radiance')
fit1
%%try nlinfit
modelFun = @(p,x) p(2).*3.741E+04./(x.^5.*(exp(1.438E+04./(x.*p(1)))-1));
startingVals=[300 1e-5];
coefEsts=nlinfit(x, y, modelFun, startingVals)
hold all
plot(x, modelFun(coefEsts, x), 'g-');
legend('Raw Data', 'Fit', 'nlinfit')

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!