% ex2_1.m
% Chapter 2: Optimization with Matlab
% Dr. P.Venkataraman
% Example 1 (modified graphics)(Sec 2.1- 2.2)
% Section:2.3.4 Tweaking the display
%
% graphical solution using matlab (two design variables)
% the following script should allow the graphical solution
% to example [ problem 3-90 from text]
%
% Minimize f(x1,x2) = (x1-3)**2 + (x2-2)**2
%
% h1(x1,x2) = 2x1 + x2 = 8
% h2(x1,x2) = (x1-1)^2 + (x2-4)^2 = 4
% g1(x1,x2) : x1 + x2 <= 7
% g1(x1,x2) : x1 - 0.25x2^2 <= 0.0
%
% 0 <= x1 <= 10 ; 0 <= x2 <= 10
%
%
% WARNING : The hash marks for the inequality constraints must
% be determined and drawn outside of the plot
% generated by matlab
%
%----------------------------------------------------------------
x1=0:0.1:10; % the semi-colon at the end prevents the echo
x2=0:0.1:10; % these are also the side constraints
% x1 and x2 are vectors filled with numbers starting
% at 0 and ending at 10.0 with values at intervals of 0.1
[X1 X2] = meshgrid(x1,x2);
% generates matrices X1 and X2 correspondin
% vectors x1 and x2
f1 = obj_ex1(X1,X2);% the objecive function is evaluated over the entire mesh
ineq1 = inecon1(X1,X2);% the inequality g1 is evaluated over the mesh
ineq2 = inecon2(X1,X2);% the inequality g2 is evaluated over the mesh
eq1 = eqcon1(X1,X2);% the equality 1 is evaluated over the mesh
eq2 = eqcon2(X1,X2);% the equality 2 is evaluated over the mesh
[C1,h1] = contour(x1,x2,ineq1,[7,7],'r-');
clabel(C1,h1);%给线上标上数值
set(h1,'LineWidth',2)
% ineq1 is plotted [at the contour value of 8]
hold on % allows multiple plots
k1 = gtext('g1');
set(k1,'FontName','Times','FontWeight','bold','FontSize',14,'Color','red')
% will place the string 'g1' on the lot where mouse is clicked
[C2,h2] = contour(x1,x2,ineq2,[0,0],'--r');
clabel(C2,h2);
set(h2,'LineWidth',2)
k2 = gtext('g2');
set(k2,'FontName','Times','FontWeight','bold','FontSize',14,'Color','red')
[C3,h3] = contour(x1,x2,eq1,[8,8],'b-');
clabel(C3,h3);
set(h3,'LineWidth',2)
k3 = gtext('h1');
set(k3,'FontName','Times','FontWeight','bold','FontSize',14,'Color','blue')
% will place the string 'g1' on the lot where mouse is clicked
[C4,h4] = contour(x1,x2,eq2,[4,4],'--b');
clabel(C4,h4);
set(h4,'LineWidth',2)
k4 = gtext('h2');
set(k4,'FontName','Times','FontWeight','bold','FontSize',14,'Color','blue')
[C,h] = contour(x1,x2,f1,'g');
clabel(C,h);
set(h,'LineWidth',1)
% the equality and inequality constraints are not written
% with 0 on the right hand side. If you do write them that way
% you would have to include [0,0] in the contour commands
xlabel(' x_1 values','FontName','times','FontSize',12,'FontWeight','bold');
% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12,'FontWeight','bold');
set(gca,'xtick',[0 2 4 6 8 10])
set(gca,'ytick',[0 2.5 5.0 7.5 10])
k5 = gtext({'Chapter 2: Example 1','pretty graphical display'})
set(k5,'FontName','Times','FontSize',12,'FontWeight','bold')
clear C C1 C2 C3 C4 h h1 h2 h3 h4 k1 k2 k3 k4 k5
%
grid
hold off
%obj_ex1.m
function retval = obj_ex1(X1,X2)
retval = (X1 - 3).*(X1 - 3) +(X2 - 2).*(X2 - 2);
%inecon1.m
function retval = inecon1(X1, X2)
retval = X1 + X2;
%eqcon1.m
function retval = eqcon1(X1,X2)
retval = 2.0*X1 + X2;
%eqcon2.m
function retval = eqcon2(X1,X2)
retval = (X1 - 1).*(X1 - 1) + (X2 - 4).*(X2 - 4);