clear; clc; close ; format ; all

advertisement
clear; clc; close all; format shortg;
%Today we look at 3D plotting
%The three commands we'll look at are plot3, surf, and mesh
%NOTE: Be sure to use element by element operations for all calculations
%today. This means .* ./ and .^
%plot3
%This commands plots points in 3D space. Then it'll connect the dots just
%like the plot command does in 2D. You creat your vectors x,y, and z. Then
%plot3(x,y,z). You can also add the same line modifiers you could from the
%plot command.
%Example plotting a cork screw
t = 0:.1:6*pi;
x = sqrt(t).*sin(2*t);
y = sqrt(t).*cos(2*t);
z = .5*t;
plot3(x,y,z)
%surf and mesh
%Mesh and surface plots are three-dimensional plots used for plotting
%functions of the from z = f(x,y), where both x,y are independent
%variables.
%The two functions plot the exact same shape. The difference is the grid is
%filled in with the surface plot but not with the mesh plot.
%Similar to the plot command it plots points and then connects the dots.
%But instead of just connecting the dots like point 1 to point 2 to point 3
%etc... We'll use a meshgrid command to put the points we want to plot in a
%matrix. Then, when matlab plots the points it won't just connect a point
%to the one before it and after it, but all points that are next to it in
%the matrix meaning up and down as well. This creates the "mesh."
%To create the matricies we first create vectors that define the domain,
%e.g. if -2 < x < 1 and -3 < y < .5, we create the vectors: x = -2:.1:1 and
%y = -3:.1:.5.
%Remember that we want enough points that when we connect the dots it looks
%like a curve and not a connect the dot drawing.
%then we create the matricies with the meshgrid command:
%[X Y] = meshgrid(x,y)
%Then, we use whatever the formula is for Z = f(X,Y).
%We can then plot with either surf(X,Y) or mesh(X,Y)
%Ex:
x = -1:.1:3;
y = 1:.1:4;
[X Y] = meshgrid(x,y);
Z = X.*Y.^2./(X.^2+Y.^2);
1
figure;
mesh(X,Y,Z)
figure;
surf(X,Y,Z)
%noteice how the surf plot has all the grids filled in and the mesh plot
%looks like a mesh (hence the name).
%HINTS:
%Problem 8: Plot the cone and the ice cream separately. Use the hold on and
%hold off commands to put the two plots on the same graph.
2
3
Published with MATLAB® R2014b
4
Download