lsqnonlin with jacobian problem

TE=2:5:5*20;
S(1,:)=104*exp(-TE/10);
options=optimset('Algorithm','levenberg-marquardt','Display','off','Jacobian ','on','Tolfun',1e-6 );
P0=[59 30];
P=lsqnonlin(@test,P0,[],[],options,S,TE)
function [F,J]=test(P,S,TE)
Ft=P(1)*exp(-TE/P(2));
F=S-Ft;
if nargout >1
J(:,1)=exp(-TE/P(2));
J(:,2)=P(1)*TE.*exp(-TE/P(2))/(P(2)^2);
end

1 commentaire

Walter Roberson
Walter Roberson le 19 Juil 2012
Are you encountering an error message? If so, what message and where?

Connectez-vous pour commenter.

Réponses (5)

Mus Bohr
Mus Bohr le 19 Juil 2012

0 votes

Thank you for your response.
My example contains only two variables (parameters). The result of the fitting is exact when I turn Jacobian 'off'. Otherwise, the result is completely erroneous.
Best,
MB

1 commentaire

Walter Roberson
Walter Roberson le 19 Juil 2012
Modifié(e) : Walter Roberson le 19 Juil 2012
P=lsqnonlin(@test,P0,[],[],options,S,TE)
  1. @test
  2. P0
  3. []
  4. []
  5. options
  6. S
  7. TE
That is 7 parameters. lsqnonlin() does not accept anything after "options".

Connectez-vous pour commenter.

Mus Bohr
Mus Bohr le 19 Juil 2012

0 votes

Thank you for this precision, but lsqnonlin function accept parameters after "options".
Indeed, I found the solution to my problem. Actually, my scripts is correct, but I should to swap Ft and S (in 'test' function). In other words, F=Ft-S instead of F=S-Ft.
Anyway, thank you so much for you time.
Warm regards,
MB.

2 commentaires

Walter Roberson
Walter Roberson le 19 Juil 2012
parameters after "options" has no defined result, and so is subject to change at any time, without notice. We repeatedly get Questions here from people who have attempted to pass extra parameters in a similar manner only to have the function fail because of it. Is there a point in relying on accidental behavior when a simple and well-documented adjustment is available? http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-7.html
Mus Bohr
Mus Bohr le 19 Juil 2012
I take note. Again thank you.
Best,
MB

Connectez-vous pour commenter.

Mus Bohr
Mus Bohr le 19 Juil 2012

0 votes

or provide Jacobian with the negative sign.
Star Strider
Star Strider le 19 Juil 2012

0 votes

Swapping S and Ft so that F = Ft - S will likely solve your problem. In the objective function you gave it, the lsqnonlin function uses the Jacobian of F in its calculation, not the Jacobian of Ft, and while they may look the same, the derivatives of F = S - Ft will be the negative of the ones you posted, while the derivatives of F = Ft - S will have the same signs as those you posted.
This is likely the reason that with the ‘Jacobian’ option ‘off’, your function converged.

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Question posée :

le 19 Juil 2012

Community Treasure Hunt

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

Start Hunting!

Translated by