ex2_2 different presentation of a surface in 3D

% ex2_2.m
%	Chapter 2: Optimization with Matlab
%  Dr. P.Venkataraman
%  Example 2 Sec.2.3
%
%	graphical solution using matlab (two design variables)
%  Unconstrained function illustrating global minimum
% 	Example will introduce 3D plots, 3D contours, filled 2D
%  contours and gradient information
%----------------------------------------------------------------
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

% filled contour with default colormap
% help graph3d gives you the choices for colormap
f1 = obj_ex2(X1,X2);% the objecive function is evaluated over the entire mesh
[C1,h1] = contourf(x1,x2,f1,...
   [0 0.1 0.3 0.6 0.8 1.0 1.2 1.5 1.8 2.0 2.2 2.4 2.6 2.8 3.0]);
clabel(C1,h1);
colorbar  % illustrates the scale
grid
set(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])
set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])
grid
xlabel(' x_1 values','FontName','times','FontSize',12);	% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12);
title({'Filled Labelled Contour','default color'}, ...
   'FontName','times','FontSize',10)
grid
% a new figue is used 
% basic contour plot superimposed with gradient information
% information is generated on a larger mesh to keep the 
% figure tidy. grid is removed for clarity

figure
y1 = -1:0.1:1.0;
y2 = -1:0.1:1;
[Y1,Y2] = meshgrid(y1,y2);
f2 = obj_ex2(Y1,Y2);
[C2,h2] = contour(y1,y2,f2,[0 0.5 0.75 1.0 1.5 1.75 2.0 2.5 2.5 3.0]);
clabel(C2,h2)
[GX, GY] = gradient(f2,0.2);%<span itemprop="syntax" style="font-family: Arial, Helvetica, sans-serif;"><tt>[...] = gradient(F,h)</tt></span><span style="font-family: Arial, Helvetica, sans-serif;">, where </span><tt>h</tt><span style="font-family: Arial, Helvetica, sans-serif;"> is a scalar, uses </span><tt>h</tt><span style="font-family: Arial, Helvetica, sans-serif;"> as the spacing between points in each direction.</span>
hold on 
quiver(Y1,Y2,GX,GY);%quiver(x,y,u,v) A quiver plot displays velocity vectors as arrows with components (u,v) at the points (x,y).
hold off
set(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])
set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])
xlabel(' x_1 values','FontName','times','FontSize',12);	% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12);
title({'2D Contour','with Gradient Vectors'}, ...
   'FontName','times','FontSize',10)


% this is a stacked contour map
figure
contour3(x1,x2, f1,[0 0.3 0.6 0.8 1.0 1.5 1.8 2.0 2.2 2.4 2.6 2.8 3.0]);
grid
set(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])
set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])
% change color map set color bar
colormap(spring)
colorbar
xlabel(' x_1 values','FontName','times','FontSize',12);	% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12)
title({'Stacked (3D) Contour','colormap - spring'}, ...
   'FontName','times','FontSize',10)

grid

% a mesh of the function
figure
colormap(cool)

mesh(y1,y2,f2)
set(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])
set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])
colorbar
xlabel(' x_1 values','FontName','times','FontSize',12);	% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12);
title({'Coarse Mesh Plot','colormap - cool'}, ...
   'FontName','times','FontSize',10)

grid


% surface plot with default colormap
figure
colormap(jet)
surf(y1,y2,f2)
grid
colorbar
set(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])
set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])
title({'Coarse Surface Plot','colormap - jet/default'}, ...
   'FontName','times','FontSize',10)

grid


xlabel(' x_1 values','FontName','times','FontSize',12);	% label for x-axes
ylabel(' x_2 values','FontName','times','FontSize',12);

hold off




%obj_ex2.m
function retval = obj_ex2(X1,X2)
% Optimization with Matlab
% Dr. P.Venkataraman
% Chapter 2. Example 2
% Problem appeared in Reference 2.3
%
%	f(x1,x2) = a*x1^2 + b*x2^2 -ccos(aa*x1)-d*cos(bb*x2) + c + d
a = 1; b = 2; c = 0.3; d = 0.4; aa = 3.0*pi; bb = 4.0*pi;
retval = a*X1.*X1 + b*X2.*X2 -c*cos(aa*X1) - ...
   d*cos(bb*X2) + c + d;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值