200 likes | 333 Vues
Computer Simulation Lab. “Lecture 5”. Electrical and Computer Engineering Department SUNY – New Paltz. Introduction to graphics. objectives MATLAB’s high-level 2-D and 3-D plotting facilities. Basic 2-D graphs. plot(x, y) plot(rand(1, 20)) ezplot(’tan(x)’) x = 0:pi/40:4*pi;
E N D
Computer Simulation Lab “Lecture 5” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Introduction to graphics objectives MATLAB’s high-level 2-D and 3-D plotting facilities SUNY-New Paltz
Basic 2-D graphs plot(x, y) plot(rand(1, 20)) ezplot(’tan(x)’) x = 0:pi/40:4*pi; plot(x, sin(x)) SUNY-New Paltz
Drawing Lines Plot([0 4], [1 3]) SUNY-New Paltz
Exercise 10 10 10 20 SUNY-New Paltz
Exercise • Plot a Polygon with N Sides N=9; theta=linspace(0,2*pi,N) plot(cos(theta),sin(theta)) axis equal • Find an equation for all points • Make 2 vectors for all points (x , y) • Plot the vectors SUNY-New Paltz
Labels • gtext(’text’) • grid • text(x, y, ’text’) • title(’text’) • xlabel(’horizontal’) • ylabel(’vertical’) SUNY-New Paltz
Multiple plots on the same axes - Method 1 • hold • hold off • plot(x,sin(x)); • hold • plot(x,cos(x)); SUNY-New Paltz
Multiple plots on the same axes - Method 2 plot(x1, y1, x2, y2, x3, y3, ... ) plotyy(x1, y1, x2, y2, x3, y3) plotyy(x,sin(x), x, 10*cos(x)) SUNY-New Paltz
Multiple plots on the same axes - Method 3 • Use Matrices • Plot([x;x]’, [sin(x);cos(x)]’; SUNY-New Paltz
Line styles, markers and color plot(x, y, ’--’) plot(x, y, ’o’) plot(x, sin(x), x, cos(x), ’om--’) Different Colors: c, m, y, k, r, g, b, w SUNY-New Paltz
Axis limits axis( [xmin, xmax, ymin, ymax] ) axis auto v = axis axis manual axis equal SUNY-New Paltz
Multiple plots in a figure:subplot subplot(m, n, p) subplot(4, 1, 1) subplot(2, 2, 1) subplot(1, 4, 1) SUNY-New Paltz
Exercise • Plot 4 sinusoids each lagging pi/2 from the previous one. The first sinusoid should have zero delay. c=['bgrk'] x=0:.1:10; for i=1:4 subplot(2,2,i) plot(x,sin(x+pi/2*i),c(i)) title(['sin(x+' num2str(i-1) '*pi/2)']) end SUNY-New Paltz
New Graphical Windows Handle h = figure; figure(h) clf % clear figure cla % clear all figures SUNY-New Paltz
Logarithmic plots semilogy(x, y) x = 0:0.01:4; semilogy(x, exp(x)), grid SUNY-New Paltz
Polar plots r x = r cos(θ), y = r sin(θ), θ x = 0:pi/40:2*pi; polar(x, sin(2*x)) grid SUNY-New Paltz
Plotting rapidly changing mathematical functions fplot .001 x = 0.01:0.001:0.1; plot(x, sin(1./x)) .0001 fplot(’sin(1/x)’, [0.01 0.1]) SUNY-New Paltz
3-D plots plot3(x, y, z) t = 0:pi/50:10*pi; plot3(exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t),t), ... xlabel(’x-axis’), ylabel(’y-axis’), zlabel(’z-axis’) SUNY-New Paltz
Exercise • Let t run from 0 to 10*pi • Plot the circle sin(t) versus cos(t) • Plot let the above circle to morph as a spiral by multiplying it by a 1/(10*pi)*t function. • Use a 3-D plot to rise the spiral from the x-y plane. SUNY-New Paltz