Non linear constraint for optimization
이전 댓글 표시
I have a code which aims to optimize a function with linear and non linear constraint. However, it doesn't work due to the non linear constraint x^2 <= y. I wrote where the problem is
% definition of x and y
x=-10:0.1:10;
y=-0.4:0.1:10;
% define a grid (x,y)
[xx,yy]=meshgrid(x,y);
%
% Evaluation of f(x,y) on this grid
%
zz = f(xx,yy); %%%%TO DEFINE
% 3D surface
figure(1), surf(x,y,zz), colormap hsv
camlight;
shading interp
lighting gouraud
view(3)
% Visualize the level sets:
figure(2),
contour(x,y,zz,[0:1:10]);
%or contour3(x,y,zz,[0:1:10]);
%Quasi Newton
fun = @(x)(x(2)-cos(2*x(1))-((x(1).^2)/10)).^2 +exp((x(1).^2 + x(2).^2)/100);
x0 = [1, 1]; % initial conditions
A=[-1,-1]; % -x - y <= b
b=[-4]; % If several linear constraints, put a vector
options = optimset('Display','iter');
options2 = optimoptions('lsqnonlin','Display','iter');
%options2 = optimoptions('lsqnonlin','Display','iter','SpecifyObjectiveGradient',true);
[qN_x, qN_fval] = fmincon(fun, x0, A, b,[],[],[],[],[], options);
% With a new non linear constraint
% THE PROBLEM IS HERE !!!
[qN_x_nonlin, qN_fvalnonlin] = fmincon(fun,x0,A,b,[],[],[],[], @mycon, options);
%Least Square
[ls_x, ls_resnorm] = lsqlin(fun, x0, [],[], options2);
%Simplex
[NMs_x, NMs_fval] = fminsearch(fun, x0, options);
function z=f(x,y)
z=(y-cos(2*x)-((x.^2)/10)).^2 +exp((x.^2 + y.^2)/100);
end
% THE PROBLEM IS HERE !!!!
function [c,ceq] = mycon(x)
c= x(2).^2 - x(3);
ceq=0;
end
Thanks
채택된 답변
추가 답변 (1개)
Nolwen Brosson
2018년 11월 8일
0 개 추천
댓글 수: 4
Torsten
2018년 11월 8일
Look at the documentation on how to call lsqlin.
Further, it's not appropriate for your problem because you have a nonlinear objective function and a nonlinear constraint.
Nolwen Brosson
2018년 11월 8일
Torsten
2018년 11월 8일
But you pass "fun" as first argument which is a nonlinear function, not a constant matrix as required.
Nolwen Brosson
2018년 11월 8일
카테고리
도움말 센터 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!