% MCS 320 Wednesday 11 April 2007 % Lecture 2 : Plotting % stage 1 : sampling of the function % stage 2 : rendering of the samples % % 1. Basic Plotting % x = -pi:pi/20:pi; % defines plotting range s = sin(x); % sample the sine function plot(x,s); % render the samples % The increment pi/20 determines the resolution % of the samples plot(x,s,'+'); % MATLAB overwrote the first figure with the new plot hold on plot(x,s,'r'); % with the hold on we have asked MATLAB to % keep the previous plot figure % create a new figure window plot(x,s,'g'); axis([-pi,pi,-2,+2]); % % 2. Polar plots % % suppose we want to plot a spiral t = 0:0.1:10*pi; r = 0.01*t.^2 - 0.02; % COMPONENTWISE squaring polar(t,r); % % 3. Three Dimensional Plots % % Note the distinction between a space curve, % which is one dimensional, and a surface, % which is two dimensional. % First we plot the surface cos(x*y): % Stage 1: we define a grid to sample the cos() xa = -2:0.2:+2; % range for x ya = xa; % same range for y [x,y] = meshgrid(xa,ya); % creates a 2D grid size(x) ans = 21 21 % Stage 2: we compute the samples COMPONENTWISE z = cos(x.*y); figure mesh(x,y,z); % the rendering is Stage 3 % The other 3D object we can plot is a space curve % Let us plot a helix: t = 0:0.1:10*pi; % sampling range plot3(cos(3*t),sin(3*t),t); % sample and plot rotate3d % plot3 is to plot space curves % mesh will render surfaces % For a surface, instead of "mesh" we can make % a contourplot figure contour(z) figure mesh(x,y,z); % % 4. Subplots % % Often we like to have different plots % in the same figure window, like a contourplot % next to a the full 3D visualization of % a surface figure subplot(2,1,1) % declare 2 rows 1 column mesh(x,y,z) subplot(2,1,2) % activate the second plot contour(z) subplot(2,1,1) % activate the first plot view([10,-10]); view([10,10]); diary off