Surface Plots

 

 

·       Here is an example of a surface plot.  This represents the lowest natural frequency (the frequency it wants to oscillate at) of a building as a function of both the column stiffness (k) and the floor mass (m).  If the ground moves at the natural frequency in an earthquake, watch out!  The building will likely collapse.

 

 

 

 

 

 

Plotting z(x,y)=x+y

 

·       Let’s examine how one creates a surface plot in MATLAB…

·       Let’s say we want a plot of the plane z(x,y)=x+y over the range –1<x<1 and –2<y<2.  Enter these commands at the prompt, one at a time, and examine each output.

 

clear; close all; clc

x=linspace(-1,1,5)

y=linspace(-2,2,5)

[X,Y]=meshgrid(x,y)

 

·       This has how we set up a grid of x,y points.  X and Y are matrices.  We can add these point-by-point to get the corresponding z(x,y) values.

 

Z=X+Y  % adds every element in 2D array X to corresponding element in Y

 

·       Now we are ready to display our matrix Z, as a function of the corresponding values stored in X and Y.

 

mesh(X,Y,Z)

xlabel('x');

ylabel('y');

zlabel('z=x+y');

title('Mesh Plot of a Plane');

 

 

Plotting a Gaussian Surface (3D “bell” curve)

 

·       These commends evaluate and plot a Gaussian surface.

 

x=[-2:.1:2];

y=[-2:.1:2];

[X,Y]=meshgrid(x,y);

std=1;

gauss=exp( (-X.^2-Y.^2)/std^2 );

mesh(X,Y,gauss)

 

·       Now try alternative surface plotting options…

 

% contour used in weather maps for barometric pressure and used in elevation

% topography maps.

contour(X,Y,gauss)

 

          % makes a standard surface plot

surf(X,Y,gauss)

 

% makes a surface plot with lighting effects

surfl(X,Y,gauss)

colormap('gray')

 

% Try this, this is my favorite

surfl(X,Y,gauss)                             

shading interp  % makes surface smooth, very nice!

colormap('gray')

 

Filling a 2D Array the “Brute Force” Way

 

·       You can use loops to fill a 2D array rather than use meshgrid( ).  This is necessary when the surface you are plotting does not involve operations that apply to 2D arrays.  Here is an example.

 

·       Here we find the root a straight line for various slopes (m) and y-intercepts (b).

 

m=[1:.5:5];

b=[0:-.5:-3];

 

for m_idx = 1 : length(m)

 

for b_idx = 1 : length(b)

 

myroots(m_idx,b_idx)=roots([m(m_idx),b(b_idx)]);

 

end

 

end

 

 

% make surface plot

mesh(b,m,myroots);

view(70,30)  % optional, sets the view angles (horizontal, vertical)

xlabel('y-intercept ');

ylabel('slope ');

zlabel('root of line ');

 

 

·       Let’s take a closer look at how the output is stored in a 2D array (myroots) here.

 

Gaussian Surface Animation

 

·       Try this…

 

clear; close all; clc;

 

figure(1)

x=[-2:.1:2];

y=[-2:.1:2];

[X,Y]=meshgrid(x,y);

count=1;

for std=.01:.05:1

          gauss=exp( (-X.^2-Y.^2)/std^2 );

          surfl(X,Y,gauss)

shading interp

          colormap('gray')

          S=sprintf('std=%.2f',std);

          text(1,1,.9,S)

          M(count)=getframe;

          count=count+1;

end

 

          movie(M,3,10);