MATLAB Answers

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

조회 수: 2(최근 30일)
Xiu Wen Kang
Xiu Wen Kang 2021년 5월 5일
댓글: Xiu Wen Kang 2021년 5월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Stephen Cobeldick
Stephen Cobeldick 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
Xiu Wen Kang
Xiu Wen Kang 2021년 5월 6일
I see, thanks for the reading recomentation! I will check it out.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by