MATLAB: How to use lsqnonlin to fit model to measurements

MATLAB: How to use lsqnonlin to fit model to measurements

fitlsqnonlin

I have to fit a modelled vector to a vector of measurements using lsqnonlin: The model is described by the function directDelta2 The set of measurements is Qfin.values_interp
delta_eq = directDelta2(SS1, SS2, SS1Molec, SS2Molec, SS1Eddy, SS2Eddy, ...
delta_atm, Qatm2, dx, x10./Rstd, [], 1,site,T,D,'15N',alpha);
The parameter to optimize is alpha.
Here’s the code I use:
fun = @(alpha)delta_eq(:,503) - Qfin.values_interp;
alpha = lsqnonlin(fun,0.2);
The error is:
Error using snls (line 47)
Objective function is returning undefined values at initial point. lsqnonlin cannot continue.
Error in lsqncommon (line 155)
[xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]= ...
Error in lsqnonlin (line 237)
[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Error in main (line 30)
alpha = lsqnonlin(fun,0.2);

Best Answer

  • I believe that you made a mistake: you wrote
    @(alpha)delta_eq(:,503)
    when you meant
    @(alpha)directDelta2(SS1, SS2, SS1Molec, SS2Molec, SS1Eddy, SS2Eddy, ...
    delta_atm, Qatm2, dx, x10./Rstd, [], 1,site,T,D,'15N',alpha);
    It is possible that this isn’t exactly what you want, either, in that maybe you want the 503 column of the directDelta2 output. If so, write a new function that returns what you want by calling directDelta2 and then returning exactly what you want.
    Alan Weiss
    MATLAB mathematical toolbox documentation
  • Add a Comment

    Your email address will not be published. Required fields are marked *