⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex1_1.m

📁 optimization using golden section search
💻 M
字号:
clear all;
close all;
clc;
%	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=-1:0.01:1;	% the semi-colon at the end prevents the echo 
x2=-1:0.01:1;	% 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

ineq3 = inecon3(X1,X2);% the inequality 3 is evaluated over the mesh
ineq4 = inecon4(X1,X2);% the inequality 4 is evaluated over the mesh

[C1,h1] = contour(x1,x2,ineq1,[0,0],'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.8,-0.8],'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,ineq3,[0,0],'b-');
clabel(C3,h3);
set(h3,'LineWidth',2)
k3 = gtext('g3');
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,ineq4,[-1,-1],'b--');
clabel(C4,h4);
set(h4,'LineWidth',2)
k4 = gtext('g4');
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

⌨️ 快捷键说明

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