А еще в help-e к Matlab 6.5 нашел образец перехода от функции constr() к fmincon(). Приведу этот раздел help-a полностью
Цитата:
Example of Converting from constr to fmincon
Old Call to constr
Код:
OPTIONS = foptions;
OPTIONS(13) = 2; % Two equality constraints
OPTIONS(1) = 1;
OPTIONS(9) = 1;
A1 = [ 1 4 -3]; b1 = 2;
A2 = [ 2 5 0]; b2 = 9;
x0 = [1; .5; .8];
LB = []; UB = [];
[X,OPTIONS,LAMBDA,HESS] = ...
constr('myfuncon',x0,OPTIONS,LB,UB,'mygradcon',A1,b1,A2,b2);
% myfuncon.m
[F, C] = myfuncon(x,A1,b1,A2,b2)
F = x(1) + 0.0009*x(2)^3 + sin(x(3));
C(1,1) = A1*x-b; % Equality linear constraint
C(2,1) = 3*x(1)^2-1; % Equality nonlinear constraint
C(3,1) = A2*x-b2; % Inequality linear constraint
C(4,1) = 7*sin(x(2))-1; % Inequality nonlinear constraint
% mygradcon.m
[G, DC] = mygradcon(x,alpha)
G = [1; % Gradient of the objective
3*0.0009*x(2)^2;
cos(x(3))];
DC(:,1) = A1'; % Gradient of the constraints
DC(:,2) = [6*x(1); 0; 0];
DC(:,3) = A2';
DC(:,4) = [0; 7*cos(x(2)); 0];
Цитата:
New Call to fmincon
Код:
OPTIONS = optimset(...
'Display', 'iter', ...
'GradCheck', 'on', ... % Check gradients.
'GradObj', 'on', ... % Gradient of objective is provided.
'GradConstr', 'on'); % Gradient of constraints is provided.
A1 = [ 1 4 -3]; b1 = 2; % Linear equalities
A2 = [ 2 5 0]; b2 = 9; % Linear inequalities
x0 = [1; .5; .8];
LB = []; UB = [];
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = ...
fmincon(@myfun,x0,A2,b2,A1,b1,LB,UB,@mycon,OPTIONS);
% myfun.m
function [F,G] = myfun(x)
F = x(1) + 0.0009*x(2)^3 + sin(x(3));
G = [1;
3*0.0009*x(2)^2;
cos(x(3))];
% mycon.m
function [C,Ceq,DC,DCeq]= mycon(x)
Ceq(1,1) = 3*x(1)^2-1; % Equality nonlinear constraint
C(1,1) = 7*sin(x(2))-1; % Inequality nonlinear constraint
DCeq(:,1) = [6*x(1); 0; 0]; % Gradient of equality
% nonlinear constraint
DC(:,1) = [0; 7*cos(x(2)); 0]; % Gradient of inequality
% nonlinear constraint