minimize function one element of array input

1 次查看(过去 30 天)
Hello,
I was not sure how to even title the question.
I have a function that have several inputs, to give an easy example a function that calculates a line (ax+b=y) that aproximates the best some data.
param0=[1,2];
[param,resnorm,~,exitflag]=lsqnonlin(@(param) LineFun(param,valuesX,valuesY),param0,[],[],optionsLsq);
function [res]= LineFun(param,valuesX,valuesY)
a=param(1);
b=param(2);
Vector=a*valuesX+b;
res=valuesY-Vector;
end
in the example I write the code will minimize the function so the best pair of values.
obviously, my case is a little bit more complicated than the example i gave but what for the porpouse it should be enought. what I would like to know is how can I (using the same function), minimize the function by one of the elements of the param. for example minimize the function with an a (param(1)) or a b (param(2)) fixed without the need to re define 3 times "similar" functions. something like this:
param0=[1,2];
%minimize for the two param a and b
[param,resnorm,~,exitflag]=lsqnonlin(@(param) LineFun(param,valuesX,valuesY),param0,[],[],optionsLsq);
%minimize for the two param a (so b is fixed to 2)
[param,resnorm,~,exitflag]=lsqnonlin(@(param(1)) LineFun(param,valuesX,valuesY),param0(1),[],[],optionsLsq);
%minimize for the two param b (so a is fixed to 1)
[param,resnorm,~,exitflag]=lsqnonlin(@(param(2)) LineFun(param,valuesX,valuesY),param0(2),[],[],optionsLsq);
function [res]= LineFun(param,valuesX,valuesY)
a=param(1);
b=param(2);
Vector=a*valuesX+b;
res=valuesY-Vector;
end
I know that I could define 3 different functions where the param means a and b or a alone or b alone and have the value of the other parameter inside the function. but I can imagine that there is a more organized way to do it as I have several functions with several parameters.
best regards,

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by