help fmincon 中就有很明确的例子啊,我只把例子给你copy过来,仔细阅读一下吧
Examples
FUN can be specified using @:
X = fmincon(@humps,...)
In this case, F = humps(X) returns the scalar function value F of
the HUMPS function evaluated at X.
FUN can also be an anonymous function:
X = fmincon(@(x) 3*sin(x(1))+exp(x(2)),[1;1],[],[],[],[],[0 0])
returns X = [0;0].
If FUN or NONLCON are parameterized, you can use anonymous functions to
capture the problem-dependent parameters. Suppose you want to minimize
the objective given in the function myfun, subject to the nonlinear
constraint mycon, where these two functions are parameterized by their
second argument a1 and a2, respectively. Here myfun and mycon are
M-file functions such as
function f = myfun(x,a1)
f = x(1)^2 + a1*x(2)^2;
function [c,ceq] = mycon(x,a2)
c = a2/x(1) - x(2);
ceq = [];
To optimize for specific values of a1 and a2, first assign the values
to these two parameters. Then create two one-argument anonymous
functions that capture the values of a1 and a2, and call myfun and
mycon with two arguments. Finally, pass these anonymous functions to
FMINCON:
a1 = 2; a2 = 1.5; % define parameters first
options = optimset('Algorithm','active-set'); % run active-set algorithm
x = fmincon(@(x) myfun(x,a1),[1;2],[],[],[],[],[],[],@(x) mycon(x,a2),options)
See also optimset, optimtool, fminunc, fminbnd, fminsearch, @, function_handle.
Reference page in Help browser
doc fmincon |