2 views (last 30 days)

Show older comments

I have a script which purpose is to optimise a fit of a function (func), in the function, beta0 is the input initial value for two coefficients. And y1, y2, y3 are data in column format (604X1). My codes for lsqnonlin optimisation and my function (func) looks like this:

[beta,resnorm,residual,exitflag,output,lambda,jacobian] = lsqnonlin(@Func,beta0,lb,ub,[],y1,y2,y3);

ci = nlparci(beta,residual,jacobian);

function f1 = Func(beta,y1,y2,y3)

f1 = y2 - beta(1)*y1 - beta(2)*y3 ;

end

when the input for Func writen in this way "Func(beta,y1,y2,y3) ", I optain the correct coefficients.

however if the variable order in the input for Func change to "Func(y1,y2,y3,beta) ", it returns with wrong coefficients.

Why does the order in the self defined function important for lsqnonlin fitting?

And, what is the relationship between the Func variables input order and lsqnonlin variables input order?

Thank you !

Steven Lord
on 5 May 2021

Why does the order in the self defined function important for lsqnonlin fitting?

For the same reason that trying to feed yourself mashed potatoes through your ears won't work very well. In your body your mouth and ears have very specific purposes or "definitions" and trying to use one to do the other's job at best will get messy.

The description of the fun input argument in the documentation for the lsqnonlin function sets out the requirements lsqnonlin imposes on the signature of the function whose handle you pass into lsqnonlin as its first input argument. [That is slightly modified by the techniques used to pass additional parameters to the function.]

And, what is the relationship between the Func variables input order and lsqnonlin variables input order?

If you're asking about the relationship between beta, y1, y2, y3 and beta0, lb, ub, etc. they don't really relate to one another. lsqnonlin has a specific order in which it requires its inputs to by specified and a specific order in which it will pass data to the objective function.

Stephen Cobeldick
on 5 May 2021

"Why does the order in the self defined function important for lsqnonlin fitting?"

In some languages inputs can be specified by the input name, but MATLAB only has positional input arguments: the input argument names have no real syntactic importance. This means if you change the order then the inputs values are supplied in different positions. This is a very basic MATLAB concept: you will not get very far if you keep moving inputs around.

"what is the relationship between the Func variables input order and lsqnonlin variables input order? "

None, because you are using an obsolete syntax which is discouraged and has not been documented for many years.

The recommended ways to pass extra parameters are explained here:

The most common approach is to use an anonymous function:

y1 = ..;

y2 = ..;

y3 = ..;

fun = @(x)Fun(x,y1,y2,y3);

[..] = lsqnonlin(fun,beta0,lb,ub);

..

function f1 = Func(beta,y1,y2,y3)

f1 = y2 - beta(1)*y1 - beta(2)*y3;

end

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

Start Hunting!