MATLAB can be found on any of the cluster machines. You can
use it to generate plots of the surfaces we were looking at in class.
(More information can be obtained by typing
>> helpdeskin the MATLAB command window and looking at the information on graphics.)
Example: Plot the graph of f(x,y) = x^2 - 2y^2.
We will plot over the rectangle where x runs from -5 to 5 and y runs from -3 to 3. First we need to set up a grid of points. It takes a few commands to do this:
>> x = -5:0.5:5;
>> y = -3:0.25:3;
>> [ x_grid, y_grid ] = meshgrid(x,y);The first MATLAB command above makes a list of x values running from -5 to 5 in steps of length 0.5. The second command makes a list of y-values from -3 to 3 in steps of length 0.25. The third step makes a grid of points on the rectangle where x varies from -5 to 5 and y varies from -3 to 3. We need now to compute the z-values for this rectangular grid of points.
>> z = x_grid.^2 - 2*y_grid.^2;
>> x_grid .* y_gridIf you type
>> x_grid * y_gridwithout the dot before the asterisk then it means to multiply the matrix x_grid by the matrix y_grid instead of multiplying corresponding entries.
Now we are ready to plot:
>> mesh(x_grid, y_grid, z);This produces an open mesh plot of this saddle-shaped surface. You can also try
>> surf(x_grid, y_grid, z);To understand the surface, it helps to be able to view it from different angles. You can do this with the mouse. First you have to click on the icon that looks like a dotted circle with an arrow pointing counterclockwise in the panel at the top of your plot window. Then if you click on the plot and drag with the mouse you can turn the surface and view it from different angles. You can also add various options to your plotting commands --- for more information about how to do this consult the help desk feature of MATLAB or type
>> help surf
>> help meshNow suppose you want to plot more than one surface at a time. For example if we want to see how the plane z = x + 2y intersects this surface we can do the following sequence:
>> mesh(x_grid, y_grid, z);
>> hold on
>> surf(x_grid, y_grid, x_grid + 2*y_grid);
>> hold offHere the "hold on" command tells MATLAB to keep add the next plot to the previous one. When you have all the pieces you want just type "hold off".
In class we also talked about plotting level curves for a function. We can do this in MATLAB using commands like
>> contour(x_grid, y_grid, z);
>> contour3(x_grid,y_grid, z);Example: In class we computed partial derivatives for the function z = sin(xy^2). Make a plot of this surface using MATLAB.
Because this surface bends a lot more than our first example we need to plot with higher resolution. I will restrict to a smaller region than before to get a better plot.
>> x = -2:0.1:2;
>> y = -2:0.1:2;
>> [x_grid,y_grid] = meshgrid(x,y);
>> z = sin(x_grid.*y_grid.^2);
>> surf(x_grid,y_grid,z)As an exercise replot on the rectangle -3 <= x <= 3 and -3 <= y <= 3.