You are on page 1of 4

Plotting Surfaces

We illustrate the use of Matlab’s functionis surf and contour by plotting the Rosenbrock banana function.

The Rosenbrock banana function is defined by

z = f (x, y ) = 100(y − x 2 )2 + (1 − x)2 .

The minimum of this function is z = 0 at (x, y ) = (1, 1).

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);

% The RosenbrockFunction2D function was written with the capability to


% input two matrices and output a matrix.

Z=RosenbrockFunction2D(X,Y);

% Let’s check that X,Y,Z are really matrices of the appropriate size.

str = fprintf(’The size of x is %d x %d.“n’, size(x));


str = fprintf(’The size of X is %d x %d.“n’, size(X));
str = fprintf(’The size of Z is %d x %d.“n’, size(Z));

The size of x is 1 x 31.


The size of X is 26 x 31.
The size of Z is 26 x 31.

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);

hax=gca; % To find out what gca does type: help gca

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])

% Tighten up the view

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);

% Since Z is a matrix, max(Z) is a vector, so we need max(max(Z)) to maximize Z.

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);

title(’Contour Plot of Rosenbrock Banana’)

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);

Contour Plot of Rosenbrock Banana


2 1500
500 0
11500500
10500
200 0

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

You might also like