CS112: Lab 12 :: More fun with plotting

Here are a couple of useful things that MATLAB can do with 2D plots:

 

What about 3D plots?

MATLAB handles 3D plots in much the same way as 2D plots.

For line plotting, use plot3 rather than plot, as shown below.


t = 0:pi/50:5*pi;
plot3(sin(t),cos(t),t);
grid on;

Sometimes it is useful to plot 3D surfaces.
Here is an example of a simple mathematical equation.

        z = x2 - y2

How to plot this equation:

  1. Create two datapoint series. Say -10<=x<=10 and -8<=y<=8.
  2. Create two new vectors that contain the squares of the x and y vectors from Step 1.
  3. Use meshgrid to create matrices of all the x squared and y squared values in proper orientation.

    The effect of meshgrid is to create a matrix X with the x-values along each row, and a matrix Y with the y-values along each column. This is done so that it is easy to evaluate a function z=f(x,y) of two variables on the rectangular grid. Click here for an example of how meshgrid works.

  4. Create the matrix Z in terms of X and Y (where Z(i,j) = X(i)^2 - Y(j)^2)

 

Here are some fun 3D plots:

Source Code

Here is code for other fun 3D plots: