Extra variables in target function in fmincon
이전 댓글 표시
Hello. My question is about fmincon and is an easy question but I am lost. My question is how to update/change variables inside the "objectivefunction" and the "nonlcon" function without affecting the variables itself. An example is provided using the Matlab example from fmincon, the code looks like this (this one runs totally fine):
function f=myfun(x)
f=-x(1)*x(2)*x(3);
A=[-1 -2 -2; 1 2 2];
b=[0;72];
x0=[10;10;10];
[x,fval]=fmincon(@myfun,x0,A,b);
Now, I want to add an extra variable in "myfun" which is not any of the variables to optimize (neither x1, x2 nor x3), in this case I want to multiply times "a", where "a" is updated in every optimization. Theoretically the myfun and the code should look like this:
function f=myfun(x,a) % x are my variables, "a" is my constant to change.
f=-x(1)*x(2)*x(3)*a;
A=[-1 -2 -2; 1 2 2];
b=[0;72];
x0=[10;10;10];
a=5;
[x,fval]=fmincon(@myfun(x,a),x0,A,b);
But this causes an error (obviously), any idea of how to assign values to extra variables in target function myfun (or nonlcon) without affecting the optimization variables?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Choose a Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!