📄 plot_any.m
字号:
% 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 = 2
% h2(x1,x2) = (x1-1)^2 + (x2-4)^2 = 4% g1(x1,x2) : x1 + x2 <= 8% g1(x1,x2) : x1 - 3x2^2 <= 0.5%% 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 meshcontour(x1,x2,ineq1,[7,7],'r-');
% 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
contour(x1,x2,ineq2,[0,0],'r--');
gtext('g2');
contour(x1,x2,eq1,[8,8],'b-');
% 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
contour(x1,x2,eq2,[4,4],'b--');
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'); % label for x-axesylabel(' x2 values');%hold off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -