1 / 39

Computer Simulation

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)).

wooda
Download Presentation

Computer Simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Simulation “Lecture 7” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz

  2. Introduction to graphics objectives MATLAB’s high-level 2-D and 3-D plotting facilities SUNY-New Paltz

  3. 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

  4. Drawing Lines Plot([0 4], [1 3]) (4, 3) x x (0, 1) SUNY-New Paltz

  5. Exercise (10,10) 10 10 (0,0) 10 20 plot([0 10], [0 10]) SUNY-New Paltz

  6. 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

  7. 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

  8. 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

  9. Labels • gtext(’text’) • grid • text(x, y, ’text’) • title(’text’) • xlabel(’horizontal’) • ylabel(’vertical’) SUNY-New Paltz

  10. Exercise • Plot a 5-sided polygon • Place labels on its vertices using the gtext(‘text’) as follows: SUNY-New Paltz

  11. 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

  12. Exercise • Plot a N-sided polygon • Place labels on its vertices using the text(x, y, ‘text’). SUNY-New Paltz

  13. 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

  14. Multiple plots on the same axes - Method 1 • hold • hold off • plot(x,sin(x)); • hold • plot(x,cos(x)); SUNY-New Paltz

  15. 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

  16. 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

  17. 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

  18. Multiple plots on the same axes - Method 3 • Use Matrices • Plot([x;x]’, [sin(x);cos(x)]’; SUNY-New Paltz

  19. 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

  20. Line styles, markers and color COLOR MARKERS LINESTYLE SUNY-New Paltz

  21. Exercise • Practice plotting six(x), using different markers and line styles. Also, plot it using only markers and no lines. SUNY-New Paltz

  22. 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

  23. 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

  24. Axis limits axis( [xmin, xmax, ymin, ymax] ) axis auto v = axis axis manual axis equal SUNY-New Paltz

  25. Axis limits sin(x) -pi<x<pi Plot the following graph: SUNY-New Paltz

  26. 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

  27. 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

  28. 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

  29. 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

  30. New Graphical Windows Handle h = figure; figure(h) clf % clear figure cla % clear all figures SUNY-New Paltz

  31. 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

  32. Logarithmic plots semilogy(x, y) x = 0:0.01:4; semilogy(x, exp(x)), grid SUNY-New Paltz

  33. Logarithmic plots Plot exp(10*x^2) for 0<x<10 on a normal and semilog graph. SUNY-New Paltz

  34. 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

  35. Exercise • Use polar statement to plot an N sided polygon! SUNY-New Paltz

  36. 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

  37. 3-D plots plot3(x, y, z) SUNY-New Paltz

  38. Exercise • Use plot3 function to plot a 1X1X1 cube. SUNY-New Paltz

  39. 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

More Related