Professional Documents
Culture Documents
We illustrate the use of Matlab’s functionis surf and contour by plotting the Rosenbrock banana function.
STEP ONE: First, we define vectors x and y that specify the discrete rectangular domain over which we
want to graph the function. Then we define matrices X, Y, Z where X(i , j) = x(i ), Y (i , j) = y (j), and
Z(i , j) = f (X(i , j)), Y (i , j)), for some function z = f (x, y ).
xmin=-1.5;
xmax=1.5;
delta=0.1;
ymin=-1;
ymax=1.5;
x=[xmin:delta:xmax];
y=[ymin:delta:ymax];
[X,Y]=meshgrid(x,y);
Z=RosenbrockFunction2D(X,Y);
% Let’s check that X,Y,Z are really matrices of the appropriate size.
STEP TWO: Plot the surface z = f (x, y ) using the surf command. In this plot, color proportional to height.
Also, let’s mark the minimum with a large magenta dot (located just above the min so we can see it).
STEP THREE: Once the figure has been made you can manually rotate it. When you are happy with
the view you can find out what it is by typing [az,el]=view and then hard code that if you like using
view(az,el).
STEP FOUR: Always use a large enough font, title your figure, label your axes, specify appropriate ticks,
and if needed tighten up the axes limits.
figure
1
surf(X,Y,Z)
% You need the following command to add a second plot to the figure.
hold on
plot3(1,1,5,’m.’,’markersize’,30)
hold off
az=170;
el=30;
view(az,el);
set(hax,’fontname’,’helvetica’,’fontsize’,18);
title(’Rosenbrock Banana’)
xlabel(’x’)
ylabel(’y’)
zlabel(’z’)
ticks=[-2:1:2];
set(hax,’xtick’,ticks,’xticklabel’,ticks)
set(hax,’ytick’,ticks,’yticklabel’,ticks)
set(hax,’ztick’,[0:500:1000],’zticklabel’,[0:500:1000])
zlim([0,1000])
2
Rosenbrock Banana
1000
500
z
0
−1
2 0 −1
y 1
x
Next we make a contour plot of the same function. Since the Rosenbrock banana-shaped valley has very
steep sides we use handpicked contour values, in the vector V rather than the default equally spaced values.
The call to contour produces the contour curves themselves. clabel labels the contours by their height,
and colorbar adds the colorbar. Notice how we set the various attributes of the curves, labels, and color
bar using set calls with the appropriate handles.
xmin=-2;
xmax=2;
delta=0.01;
ymin=-2;
ymax=2;
x=[xmin:delta:xmax];
y=[ymin:delta:ymax];
[X,Y]=meshgrid(x,y);
Z=RosenbrockFunction2D(X,Y);
MaxValue=max(max(Z));
DeltaV=50;
V1=[0:DeltaV:4*DeltaV];
V2=[10*DeltaV:20*DeltaV:MaxValue/2];
V=[V1 V2];
figure
3
hax=gca;
[c hc]=contour(X,Y,Z,V);
hcl=clabel(c, hc);
hcb=colorbar;
hold on
plot(1,1,’m.’,’markersize’,30)
hold off
set(hax,’fontname’,’helvetica’,’fontsize’,18);
xlabel(’x’)
ylabel(’y’)
ticks=[-2:1:2];
set(hax,’xtick’,ticks,’xticklabel’,ticks)
set(hax,’ytick’,ticks,’yticklabel’,ticks)
colorticks=[0:500:2000];
set(hcb,’ytick’,colorticks,’yticklabel’,colorticks)
set(hcl,’fontname’,’helvetica’,’fontsize’,18);
set(hc,’linewidth’,2);
20
50
20
120
0
0
50 10 1
50
15
50
0
100
0 50
1
50 1000
500 0
115000
200
20
5
50 1
0
500
150
0
0
y
150
0
00 15
50 500
0
−1 20 100 0 00
0 15 2
50
0
150
0
50
0
150
0
−2 0
−2 −1 0 1 2
x