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?

 Accepted Answer

Alan Weiss
MATLAB mathematical toolbox documentation

1 Comment

Thank you, the three alternatives work fine for my project!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!