MATLAB:Contour Plots

From PrattWiki
Revision as of 16:30, 26 September 2012 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

MATLAB is capable of making contour plots and also of labeling the contours using specified values. The following are some examples. Be sure to look up MATLAB:Plotting Surfaces first to understand how MATLAB makes surfaces.

Example 1: Pinched

[r, theta] = meshgrid(linspace(0,1,20), linspace(0, 2*pi, 50));
x = r.*cos(theta);
y = r.*sin(theta);
z = cos(theta).*r;

%% Surface with contours
figure(1)
surfc(x, y, z)
shading interp
colormap gray

%% Contour plot
figure(2)
[C, H] = contour(x, y, z);
colormap gray
clabel(C, H)

%% Contour plot specifying 5 contours
figure(3)
[C, H] = contour(x, y, z, 5);
colormap gray
clabel(C, H)

%% Contour plot specifying contour locations
figure(4)
[C, H] = contour(x, y, z, [-.75:.25:.75]);
colormap gray
clabel(C, H)

Example 2: Egg Crate

[x, y] = meshgrid(linspace(0, 3, 64), linspace(0, 1, 41));
z = -(1-cos(4*pi*y)).^(1/4).*(1-cos(2*pi*x)).^(1/4);

%% Surface plot with contours
figure(1)
surfc(x, y, z)
shading interp
colormap gray

%% Contour plot
figure(2)
[C, H] = contour(x, y, z);
colormap gray
clabel(C, H)

%% Contour plot specifying 5 contours
figure(3)
[C, H] = contour(x, y, z, 5);
colormap gray
clabel(C, H)

%% Contour plot specifying contour locations
figure(4)
[C, H] = contour(x, y, z, [-1.75:.25:1.75]);
colormap gray
clabel(C, H)