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?

 채택된 답변

Alan Weiss
Alan Weiss 2013년 3월 5일

1 개 추천

Alan Weiss
MATLAB mathematical toolbox documentation

댓글 수: 1

Hugo
Hugo 2013년 3월 6일
Thank you, the three alternatives work fine for my project!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2013년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by