MATLAB:Contour Plots

From PrattWiki
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 copper

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

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

%% Contour plot specifying contour locations
figure(4)
[C, H] = contour(x, y, z, [-.75:.25:.75]);
colormap copper
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 copper

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

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

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