% Example 5.4 r=10; x0=[r 1]; for k=1:10 x(k,:)=((r-1)/(r+1))^k*[r (-1)^k]; end x = [x0; x]; figure(1);hold on; axis equal; line(x(:,1),x(:,2),'marker','o') % x = linspace(-12,12); y = linspace(-5,5); [X,Y] = meshgrid(x,y); Z = X.^2 + r*Y.^2; contour(X,Y,Z,[0.1 0.5 1 2 5 10 20 30 50 70 100]) %----------------------------------------------------------------------------- % % Section 5.7 function f = objfun(x) f = exp(x(1))*(4*x(2)^2 + 2*x(2)^2 + 4*x(1)*b(2) + 2*x(2) + 1); end % function [c, ceq] = confun(x) % Nonlinear inequality constraint c = [1.5 + x(1)*x(2) – x(1) – x(2); -x(1)*x(2) – 10]; % Nonlinear equality constraints ceq = []; end % x0 = [-1,1]; % Make a starting guess at the solution options = optimset('LargeScale','off'); [x, fval, exitflag, output, lambda, grad, hessian] = ... fmincon(@objfun,x0,[],[],[],[],[],[],@confun, options) %----------------------------------------------------------------------------- % % Example 5.11 function f=quad2(x) global a f=x(1)^2+a*x(2)^2; end % function [c,ceq]=ring(x) c(1)=10^2-x(1)^2-x(2)^2; c(2)=x(1)^2+x(2)^2-20^2; ceq=[]; end % global a x0=[1,10];a=10; [x,fval,exitflag,output,lambda]=fmincon(@quad2,x0,[],[],[],[],[],[],@ring) %-----------------------------------------------------------------------------