ex2_2 different presentation of a surface in 3D

来源:互联网 发布:linux 重装samba 编辑:程序博客网 时间:2024/05/26 20:22

% 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 colormapf1 = 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 scalegridset(gca,'xtick',[-1 -0.5 0.0 0.5 1.0])set(gca,'ytick',[-1 -0.5 0.0 0.5 1.0])gridxlabel(' x_1 values','FontName','times','FontSize',12);% label for x-axesylabel(' 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 clarityfigurey1 = -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 offset(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-axesylabel(' x_2 values','FontName','times','FontSize',12);title({'2D Contour','with Gradient Vectors'}, ...   'FontName','times','FontSize',10)
% this is a stacked contour mapfigurecontour3(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]);gridset(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 barcolormap(spring)colorbarxlabel(' x_1 values','FontName','times','FontSize',12);% label for x-axesylabel(' x_2 values','FontName','times','FontSize',12)title({'Stacked (3D) Contour','colormap - spring'}, ...   'FontName','times','FontSize',10)grid
% a mesh of the functionfigurecolormap(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])colorbarxlabel(' x_1 values','FontName','times','FontSize',12);% label for x-axesylabel(' x_2 values','FontName','times','FontSize',12);title({'Coarse Mesh Plot','colormap - cool'}, ...   'FontName','times','FontSize',10)grid
% surface plot with default colormapfigurecolormap(jet)surf(y1,y2,f2)gridcolorbarset(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)gridxlabel(' x_1 values','FontName','times','FontSize',12);% label for x-axesylabel(' x_2 values','FontName','times','FontSize',12);hold off




%obj_ex2.mfunction 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 + da = 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
原创粉丝点击