why is the variable order in a user defined function important in lsqnonlin optimisation?

1 次查看(过去 30 天)
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
Steven Lord 2021-5-5
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.
  3 个评论
Stephen23
Stephen23 2021-5-6
"I am wondering why do i get different results when I change the sequence of beta, y1, y2, y3 in function..."
You have already answered your own question: because you changed the order in the function signature.
You are still thinking that somehow the variable names have some significance or are somehow taken into account. Only the input positions are significant, just as I wrote in my answer, not their names.

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2021-5-5
"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
  3 个评论
Stephen23
Stephen23 2021-5-6
"Why is that the "function f1 = Func(beta,y1,y2,y3)" becomes obsolete syntax?"
It isn't, there is absolutely no problem with defining a function like that (tip: read the function documentation).
What is obsolete is specifying the objective function parameters as extra inputs to lsqnonlin (or and ODE solver, or similar): that syntax is obsolete, has been removed from the documentation, and should not be used for any new code. The documentation that I linked to shows the recommended approaches for parameterizing objective functions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by