ex2_1graphical solution

来源:互联网 发布:公司优化方案 编辑:程序博客网 时间:2024/04/29 07:18



% 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]%%Minimizef(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 x2f1 = obj_ex1(X1,X2);% the objecive function is evaluated over the entire meshineq1 = inecon1(X1,X2);% the inequality g1 is evaluated over the meshineq2 = inecon2(X1,X2);% the inequality g2 is evaluated over the mesheq1 = eqcon1(X1,X2);% the equality 1 is evaluated over the mesheq2 = 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 plotsk1 = 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 commandsxlabel(' x_1 values','FontName','times','FontSize',12,'FontWeight','bold');% label for x-axesylabel(' 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%gridhold off


%obj_ex1.mfunction retval = obj_ex1(X1,X2)retval = (X1 - 3).*(X1 - 3) +(X2 - 2).*(X2 - 2);

%inecon1.mfunction retval = inecon1(X1, X2)retval = X1 + X2;

%eqcon1.mfunction retval = eqcon1(X1,X2)retval = 2.0*X1 + X2;

%eqcon2.mfunction retval = eqcon2(X1,X2)retval = (X1 - 1).*(X1 - 1) + (X2 - 4).*(X2 - 4);







0 0
原创粉丝点击