minimize function one element of array input

조회 수: 1 (최근 30일)
franco otaola
franco otaola 2021년 1월 21일
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에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by