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!