ex1.m

来自「good code for matlab by mili , i than yo」· M 代码 · 共 76 行

M
76
字号
%	Chapter 2: Optimization with Matlab
%  Dr. P.Venkataraman
%  Example 1 (Sec 2.1- 2.2)
%
%	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 x2f1 = obj_ex1(X1,X2);% the objecive function is evaluated over the entire meshineq1 = inecon1(X1,X2);% the inequality g1 is evaluated over the mesh
ineq2 = inecon2(X1,X2);% the inequality g2 is evaluated over the mesheq1 = 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);
% ineq1 is plotted [at the contour value of 8]
hold on	% allows multiple plots
gtext('g1');
% will place the string 'g1' on the lot where mouse is clicked
[C2,h2] = contour(x1,x2,ineq2,[0,0],'r--');
clabel(C2,h2);
gtext('g2');

[C3,h3] = contour(x1,x2,eq1,[8,8],'b-');
clabel(C3,h3);
% ineq1 is plotted [at the contour value of 8]
hold on	% allows multiple plots
gtext('h1');
% will place the string 'g1' on the lot where mouse is clicked
[C4,h4] = contour(x1,x2,eq2,[4,4],'b--');
clabel(C4,h4);
gtext('h2');

[C,h] = contour(x1,x2,f1,'g');
clabel(C,h);%% 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(' x1 values','FontName','times','FontSize',12,'FontWeight','bold');	% label for x-axesylabel(' x2 values','FontName','times','FontSize',12,'FontWeight','bold');%gridhold off

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?