% 1. basic plotting % we always separate the computation of the samples % from the actual rendering, done by plot x = -pi:0.01:pi; y = sin(x); plot(x,y) axes([-pi,pi,-1,1]) ??? Error using ==> axes Invalid object handle. axis([-pi,pi,-1,1]) hold on plot(x,y,'r+') % with hold on the plot in the window stays % we can mark the samples with red crosses hold off x = 0:pi x = 0 1 2 3 c = cos(x); plot(x,c) x = 0:0.5:pi x = Columns 1 through 5 0 0.5000 1.0000 1.5000 2.0000 Columns 6 through 7 2.5000 3.0000 c = cos(x); plot(x,c) x = 0:0.1:2*pi; c = cos(x); plot(x,c) axis([0,2*pi,-2,2]) hold on plot(x,c,'gx') figure % 2. polar plots t = 0:0.1:10*pi; r = 0.01*t.^2 -0.02; % Observe the dot . before the hat ^ ! % Dotwise operations mean that we execute % it componentwise. polar(t,r) % 3. three dimensional plots % For surfaces, we must first define a grid. % For plotting cos(x*y), for x in [-2,2] and % y also in [-2,2], we first need to make a grid. xa = -2:0.2:+2; ya = -2:0.2:+2; [x,y] = meshgrid(xa,ya); z = cos(x .* y); % observe the dot before * !!! figure mesh(x,y,z) view(3) view(3) view([-16,5]) % Special Three dimensional plots % with plot3 we draw space curves t = 0:0.1:10*pi; figure x = cos(3*t); y = sin(3*t); z = t; plot3(x,y,z) % subplots % we can choose a different window for every plot % or create several plots in the same window figure subplot(2,2,1) plot3(x,y,z) subplot(2,2,2) plot3(x,y,z) subplot(2,2,3) plot3(x,y,z) subplot(2,2,4) plot3(x,y,z) % contour plots r = -2:.1:2; [x,y] = meshgrid(r,r); z = x.*exp(-x.^2 - y.^2); figure subplot(2,1,1) mesh(x,y,z) subplot(2,1,2) contour(z) contour(z,20) subplot(2,1,1) % make first subplot active again view(3) % restore the default view diary off