Fondamenti di Informatica Examples: Plotting 2013/06/13 Prof. Emiliano Casalicchio emiliano.casalicchio@uniroma2.it
Objectives This chapter presents the principles and practice of plotting in the following forms: Basic two-dimensional line plots Two-dimensional parametric plots Three-dimensional line and parametric plots Basic three-dimensional surface plots 1-2
Plot Enhancements % Listing 11-1 - Creating a subplot clf x = -2*pi:.05:2*pi; subplot(2,3,1) plot(x, sin(x)) title('1 - sin(x)'); subplot(2,3,2) plot(x, cos(x)) title('2 - cos(x)'); subplot(2,3,3) plot(x, tan(x)) title('3 - tan(x)'); subplot(2,3,4) plot(x, x.^2) title('4 - x^2'); subplot(2,3,5) plot(x, sqrt(x)) title('5 - sqrt(x)'); subplot(2,3,6) plot(x, exp(x)) title('4 - e^x'); 1-3
Functions for enhancing plots n axis n colormap n grid on/off n hold on/off n legend n shading n text n title n xlabel n ylabel 4
Multiple 2-D Plots % Listing 11-2 - Simple 2-D plots clear clc close all x = linspace(-1.5, 1.5, 30); y1 = x; y2 = x.^2; y3 = x.^3; y4 = x.^4; plot(x,y1,x,y2,x,y3,x,y4) xlabel('x') ylabel('value') title('powers of x') legend({'1','2','3','4'},... 'Location','SouthEast') 1-5
Special 2-D Effects % Listing 11-3 - Demonstrating plot options clear clc close all x = linspace(0, 2*pi); subplot(2, 3, 1) plot(x, sin(x)) axis([0 2*pi -0.5 0.5]) title('changing Data Range on an Axis') subplot(2, 3, 2) plot(x, sin(x)) hold on plot(x, cos(x)) axis tight title('multiple Plots with hold on') subplot(2, 3, 3) plot(x, sin(x),'-- ) hold on plot(x, cos(x), 'r: ) axis tight title('multiple Plots with hold on') 1-6
Special 2-D Effects % Listing 11-3 - Demonstrating plot options % Continued... subplot(2, 3, 4) N = 16; xr = linspace(0, 1, N); yr = xr + rand(1,n) - 0.5; plot(xr, yr, 'r+ ) title('symbols for Real Data') subplot(2, 3, 5) x = 0:0.01:20; y1 = 20*exp(-0.05*x).*sin(x); y2 = 0.8*exp(-0.5*x).*sin(3*x); plotyy(x,y1,x,y2,'plot'); xlabel('zero to 20 \musec. ) title('plotting on the Second Axis') subplot(2, 3, 6) pow = linspace(0, 1); w = 10.^pow; fr = complex(0, w); h = (fr.^2 + 0.1*fr + 7.5)..../ (fr.^4 + 0.12*fr.^3 + 9*fr.^2); loglog(w, abs(h)) title('log - log plots') 1-7
Transforming a Circle to an Airfoil % Listing 11-4 - Parametric plots clear clc close all th = linspace(0, 2*pi, 40); r = 1.1; g =.1; cx = sqrt(r^2-g^2) - 1; cy = g; x = r*cos(th) + cx; y = r*sin(th) + cy; plot( x, y, 'r' ) axis equal grid on hold on z = complex(x, y); w = z + 1./z; plot( real(w), imag(w), 'k' ); 1-8
2-D plot capabilities n bar(x,y) n barh(x,y) n fill(x,y,n) n hist(x,y) n loglog(x,y) n pie(y) n polar(th,y) n semilogx(x,y) n semilogy(x,y) 9
3-D View of a 2-D Plot 1-10
3-D Line plots % Listing 11-5 - % Simple 3-D line plots clear clc close all x=0:0.1:3.*pi; y1=zeros(size(x)); z1=sin(x); z2=sin(2.*x); z3=sin(3.*x); y3=ones(size(x)); y2=y3./2; plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g ) grid on xlabel('x-axis ) ylabel('y-axis ) zlabel('z-axis') 1-11
Parametric Line Plots 1-12
Parametric Line Plots % Listing 11-6 - Linear parametric 3-D plots clear clc close all subplot(1, 2, 1) theta = 0:0.1:10.*pi; plot3(sin(theta),cos(theta),theta) title('parametric curve based on angle'); grid on 1-13
Parametric Line Plots % Listing 11-6 - Linear parametric 3-D plots subplot(1, 2, 2) N = 20; dvx = rand(1, N) - 0.5 % random v changes dvy = rand(1, N) - 0.5 dvz = rand(1, N) - 0.5 vx = cumsum(dvx); % integrate to get v vy = cumsum(dvy); vz = cumsum(dvz); x = cumsum(vx); % integrate to get pos y = cumsum(vy); z = cumsum(vz); plot3(x,y,z) grid on title('all 3 axes varying with parameter t') text(0,0,0,'start'); text(x(n),y(n),z(n),'end'); 1-14
Designing a Cube Surface plot 1-15
Cube Surfaces 1-16
Cube Surfaces % Listing 11-7 - Simple solid cube clear clc close all xx = [-1-1 1 1-1 % A-B-C-D-A -1-1 1 1-1] % D-E-F-G-D yy = [ 1-1 -1 1 1 % A-B-C-D-A 1-1 -1 1 1] % D-E-F-G-D zz = [ 1 1 1 1 1 % A-B-C-D-A -1-1 -1-1 -1] % D-E-F-G-D subplot(1, 2, 1) surf(xx, yy, zz) axis equal shading interp view(-36, 44) axis off 1-17
Cube Surfaces xx = [ 0 0 0 0 0 % P-P-P-P-P -1-1 1 1-1 % A-B-C-D-A -1-1 1 1-1 % D-E-F-G-D 0 0 0 0 0] % Q-Q-Q-Q-Q yy = [ 0 0 0 0 0 % P-P-P-P-P 1-1 -1 1 1 % A-B-C-D-A 1-1 -1 1 1 % D-E-F-G-D 0 0 0 0 0] % Q-Q-Q-Q-Q zz = [ 1 1 1 1 1 % P-P-P-P-P 1 1 1 1 1 % A-B-C-D-A -1-1 -1-1 -1 % D-E-F-G-D -1-1 -1-1 -1] % Q-Q-Q-Q-Q subplot(1, 2, 2) surf(xx, yy, zz) axis equal shading interp view(-36, 44) axis off 1-18
Simple Surface Plot % Listing 11-8 - Simple surface plot clear clc close all x=-3:3; y = x ; [xx,yy]=meshgrid(x,y); zz=xx.^2 + yy.^2; mesh(xx,yy,zz) axis tight title('z = x^2 + y^2 ) xlabel('x'),ylabel('y'),zlabel('z') 1-19
Homework n Completare il capitolo 11 studiando gli altri esempi di plotting 20
Compound Surface Plot 1-21
Using External Illumination 1-22
Designing a Cylinder Plot 1-23
Cylinder Plot 1-24
Sphere Plot 1-25
Rotating About the X Axis 1-26
Rotating About the Z Axis 1-27
Bodies of Rotation 1-28
Rotating Arbitrary Shapes 1-29
A Solid Disk 1-30
A Klein Bottle 1-31
Map of Atlanta 1-32
Summary Basic two-dimensional line plots are accomplished by using plot(x,y Two-dimensional parametric plots are accomplished by using plot(x,y), where both x and y are dependent on another independent variable Three-dimensional line and parametric plots are accomplished by using plot3(x,y,z) Basic three-dimensional surface plots are accomplished by building a plaid using meshgrid(x,y), computing the zz layer as a function of xx and yy, then plotting the surface using mesh(xx, yy, zz) or surf(xx, yy, zz) Parametric surface plots, like parametric line plots, are achieved by building the plaid with two independent variables and making xx, yy, and zz functions of those independent variables Bodies of rotation are a special case of parametric surface plots where one of the independent variables is an angle with values between 0 and 2. 1-33