{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

16. Plotting in 3D

# 16. Plotting in 3D - Plotting in 3D Plotting a function z =...

This preview shows pages 1–7. Sign up to view the full content.

©2009 by L. Lagerstrom Plotting in 3D Plotting a function z = f(x,y) Experimenting with the interval size How meshgrid creates the grid Common error Surface plots Changing the shading and coloring Adding lighting effects Contour plots Combining 3D and contour plots

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom Note re. Code and Display In the code examples that follow, we will often have the results displayed simply by leaving off the semi-colon at the end of assignment statements. (The results will be displayed in a simulated Command window.) In addition, we will assume that the display format has been set to “format bank” (i.e., results displayed to 2 decimal places), simply to save space. Finally, the display of the results will have blank lines in it for readability, but the code that would create those blank lines (e.g., disp(‘ ’) ) will not be shown in the code examples (again, for readability’s sake).
©2009 by L. Lagerstrom Plotting a Function z = f(x,y) When we learned to plot a function y = f(x), there were three basic steps: 1. Define a row vector of x values. 2. Calculate the corresponding y values. 3. Plot the (x,y) values using the plot function. When we are plotting a function of two variables, z = f(x,y), the process is similar, but with one key extra step (#2): 1. Define row vectors of x and y values. 2. Create an (x,y) grid of points using the x and y values in step 1. (To do so, we use the "meshgrid" function.) 3. Calculate the corresponding z value for each (x,y) grid point. 4. Plot the (x,y,z) results using one of several possible 3D plotting functions (e.g., mesh, surf, meshc).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Matlab code Command window display ©2009 by L. Lagerstrom Plotting a Function z = f(x,y) %As an example, consider the function %z = y*exp(-(x^2 + y^2)) plotted for %x = -2 to 2 and y = -1 to 2. %Define some x and y values at %intervals of 0.1: x = -2:0.1:2; y = -1:0.1:2; %Use meshgrid function to create %the (x,y) grid of points: [X Y] = meshgrid(x,y); %Calculate Z values using X,Y grid of %points (note use of dot operators): Z = Y.*exp(-(X.^2 + Y.^2)); %Use mesh function with the X,Y,Z %values to create a mesh plot: figure(1), clf mesh(X,Y,Z) title('z = y*exp(-(x^2 + y^2))') xlabel('x'), ylabel('y'), zlabel('z') [Plot shown on next slide...]
Figure window display ©2009 by L. Lagerstrom Plotting a Function z = f(x,y), cont.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom Comments re. the 3D Plot The mesh function plots the Z values and then connects them with colored lines, thus the mesh effect seen in the figure. (So the actual points plotted are at the intersections of the mesh lines.) Matlab automatically displays the colors according to increasing or decreasing height (z value). (It's possible to specify a different color arrangement; we'll see some examples a little later.) It's very important to use the X and Y grid values, not the x and y values, when calculating the Z values. If you use x and y, the plot will not display correctly. (We'll see an example in a few slides.) We will take an inside look at the X and Y grid values in a minute, but note for now that X and Y are arrays, so you must use dot operators when calculating the Z values.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern