% 1. basic plots t = 0:0.01:2*pi; c = cos(t); plot(t,c) hold on % to keep the current plot plot(t,c,'g+') % to plot a coarser range s = 0:0.3:2*pi; c2 = cos(s); plot(s,c2,'rx') plot(s,c2,'--') hold off % clear for new plot c3 = cos(3*s); plot(s,c3) figure c4 = cos(3*t); plot(t,c4) % in plotting we plot x and y values % if we want to add to figure 1, % we type figure(1) hold on plot(t,c4,'r') axis([0,2*pi,-2,2]) % 2. polar plots % spiral have very natural representations % in polar coordinates angle = 0:0.01:10; r = 0.01*angle.^2 -0.02; % Observe the . in front of the ^ operator polar(angle,r) clf polar(angle,r) % to illustrate the componentwise multiplication a = rand(2,2); b = rand(2,2); c = a*b c = 0.5276 0.2963 0.6619 0.7518 d = a.*b d = 0.5152 0.0354 0.0884 0.4995 % The first a*b is the matrix multiplication % The a.*b is the componentwise multiplication % 3. Three dimensional plots % Suppose we want to plot z = cos(x*y) % for x and y both in the interval [-2,2]. % First we determine how many samples we take: xa = -2:0.01:+2; ya = -2:0.01:+2; % Secondly, we must setup the grid: [x,y] = meshgrid(xa,ya); % Thirdly: we sample z = cos(x*y): z = cos(x.*y); % observe the .* % Fourth: we render the plot figure mesh(x,y,z) view([-34,50]); view(3) % we can make subplots figure subplot(2,2,1) mesh(x,y,z) subplot(2,2,2) mesh(x,y,z) view([10,10]) subplot(2,2,3) mesh(x,y,z) view([10,70]) subplot(2,2,4) mesh(x,y,z) view([10,-30]) % to restore the default view for % the top right plot subplot(2,2,2) view(3) % For a space curve we use the plot3 command % example the helix defined by % x = cos(t), y = sin(t), z = t t = 0:0.01:20*pi; x = cos(t); y = sin(t); z = t; figure plot3(x,y,z) % contour plots % we will plot the surface z = exp(-x^2-y^2) % for x and y ranging from -2 to +2 % We still have the x and y from before, % which is not true! We have to remake % x and y from the xa and ya we had before: [x,y] = meshgrid(xa,ya); z = exp(-x.^2 - y.^2); % this is the surface z = exp(-x^2 - y^2) figure subplot(2,1,1) mesh(x,y,z) subplot(2,1,2) contour(z) diary off