390 likes | 406 Views
Computer Simulation. “Lecture 7”. 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(y) plot(rand(1, 20)) plot(x, y) x = 0:pi/40:4*pi; plot(x, sin(x)).
E N D
Computer Simulation “Lecture 7” 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(y) plot(rand(1, 20)) plot(x, y) x = 0:pi/40:4*pi; plot(x, sin(x)) SUNY-New Paltz
Drawing Lines Plot([0 4], [1 3]) (4, 3) x x (0, 1) SUNY-New Paltz
Exercise (10,10) 10 10 (0,0) 10 20 plot([0 10], [0 10]) SUNY-New Paltz
Exercise • Plot a Polygon with N Sides – using a for loop. • Find an equation for all points. cos(theta), sin(theta) • From a for loop to generate x and y vectors. • Plot the vectors SUNY-New Paltz
Exercise • Plot a Polygon with N Sides - vectorize • Find an equation for all points. cos(theta), sin(theta) • Make 2 vectors for all points (x , y) • Plot the vectors SUNY-New Paltz
Solution to Polygon clc clear all N = 8; theta = 0:2*pi/N:2*pi; x = cos(theta); y = sin(theta); plot(x, y); axis equal; SUNY-New Paltz
Labels • gtext(’text’) • grid • text(x, y, ’text’) • title(’text’) • xlabel(’horizontal’) • ylabel(’vertical’) SUNY-New Paltz
Exercise • Plot a 5-sided polygon • Place labels on its vertices using the gtext(‘text’) as follows: SUNY-New Paltz
Solutions to labels (gtext) N = 5; theta = 0:2*pi/N:2*pi; x = cos(theta); y = sin(theta); plot(x, y); axis equal; gtext('P1'); gtext('P2'); gtext('P3'); gtext('P4'); gtext('P5'); SUNY-New Paltz
Exercise • Plot a N-sided polygon • Place labels on its vertices using the text(x, y, ‘text’). SUNY-New Paltz
N = 3; theta = 0:2*pi/N:2*pi; x = cos(theta); y = sin(theta); plot(x, y); axis equal; text(-.4, .6, 'P1'); text(-.4, -.6, 'P2'); text(1.1, 0, 'P3'); 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
Exercise – multiple graphs Prompt the user to enter n for the number of trigonometric functions to be plotted: 1 – means only cos(x) 2 – means cos(x) and sin(x) 3 - means cos(x), sin(x) and sin(x)*cos(x) Hint: Use ‘hold’ SUNY-New Paltz
Multiple plots on the same axes - Method 2 plot(x1, y1, x2, y2, x3, y3, ... ) plotyy(x1, y1, x2, y2) plotyy(x,sin(x), x, 10*cos(x)) SUNY-New Paltz
Exercise – multiple graphs Prompt the user to enter n for the number of trigonometric functions to be plotted: 1 – means only cos(x) 2 – means cos(x) and sin(x) 3 - means cos(x), sin(x) and sin(x)*cos(x) Hint: use method 2 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
Line styles, markers and color COLOR MARKERS LINESTYLE SUNY-New Paltz
Exercise • Practice plotting six(x), using different markers and line styles. Also, plot it using only markers and no lines. SUNY-New Paltz
Exercise Plot sin(x), sin(x+pi/2), sin(x + pi) and sin(x+3*pi/2) on the same plot using different colors and line styles using a single plot statement. SUNY-New Paltz
Solutions to multiple sinuses x=0:pi/40:2*pi; plot(x,sin(x), ‘.-',x,sin(x+pi/2),'r--', x,sin(x+pi),'m-.',x,sin(x+3*pi/2),'c:‘); SUNY-New Paltz
Axis limits axis( [xmin, xmax, ymin, ymax] ) axis auto v = axis axis manual axis equal SUNY-New Paltz
Axis limits sin(x) -pi<x<pi Plot the following graph: 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. Initially, write 4 plot statements. Then, use a for loop. SUNY-New Paltz
Solutions to subplots c=['bgrk'] x=0:.1:10; fori=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
Exercise Plot a cosine function between 0 and 5*pi. Allow the user to place use the mouse to place the following labels on the plot: max1, max2, max3 min1, min2, min3 SUNY-New Paltz
New Graphical Windows Handle h = figure; figure(h) clf % clear figure cla % clear all figures SUNY-New Paltz
Exercise Write a program that generates 2 windows. Plot sin(x) on the first and cos(x) on the second window. Make sure both windows exist. Then, prompt the user to select a plot, i.e. 1 or 2. Then, bring the selected window on and close the other window. SUNY-New Paltz
Logarithmic plots semilogy(x, y) x = 0:0.01:4; semilogy(x, exp(x)), grid SUNY-New Paltz
Logarithmic plots Plot exp(10*x^2) for 0<x<10 on a normal and semilog graph. SUNY-New Paltz
Polar plots r x = r cos(θ), y = r sin(θ), θ Polar(angle, magnitude) x = 0:pi/40:2*pi; polar(x, sin(2*x)) grid SUNY-New Paltz
Exercise • Use polar statement to plot an N sided polygon! 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
3-D plots plot3(x, y, z) SUNY-New Paltz
Exercise • Use plot3 function to plot a 1X1X1 cube. SUNY-New Paltz
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