computer simulation lab

20
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 5”

Upload: thea

Post on 22-Jan-2016

66 views

Category:

Documents


0 download

DESCRIPTION

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; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Simulation Lab

SUNY-New Paltz

Computer Simulation Lab

Electrical and Computer Engineering Department

SUNY – New Paltz

“Lecture 5”

Page 2: Computer Simulation Lab

SUNY-New Paltz

objectives

MATLAB’s high-level 2-D and 3-D plotting facilities

Introduction to graphics

Page 3: Computer Simulation Lab

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

Page 4: Computer Simulation Lab

SUNY-New Paltz

Drawing Lines

Plot([0 4], [1 3])

Page 5: Computer Simulation Lab

Exercise

SUNY-New Paltz

10

10

20

10

Page 6: Computer Simulation Lab

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

Page 7: Computer Simulation Lab

SUNY-New Paltz

Labels

• gtext(’text’)• grid• text(x, y, ’text’)• title(’text’)• xlabel(’horizontal’)• ylabel(’vertical’)

Page 8: Computer Simulation Lab

SUNY-New Paltz

Multiple plots on the same axes - Method 1

• hold

• hold off

• plot(x,sin(x));

• hold

• plot(x,cos(x));

Page 9: Computer Simulation Lab

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

Page 10: Computer Simulation Lab

SUNY-New Paltz

Multiple plots on the same axes - Method 3

• Use Matrices• Plot([x;x]’, [sin(x);cos(x)]’;

Page 11: Computer Simulation Lab

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

Page 12: Computer Simulation Lab

SUNY-New Paltz

Axis limits

axis( [xmin, xmax, ymin, ymax] )axis autov = axisaxis manualaxis equal

Page 13: Computer Simulation Lab

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)

Page 14: Computer Simulation Lab

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

Exercise

• Plot 4 sinusoids each lagging pi/2 from the previous one. The first sinusoid should have zero delay.

Page 15: Computer Simulation Lab

SUNY-New Paltz

New Graphical Windows

h = figure;

figure(h)

clf % clear figure

cla % clear all figures

Handle

Page 16: Computer Simulation Lab

SUNY-New Paltz

Logarithmic plots

semilogy(x, y)

x = 0:0.01:4;

semilogy(x, exp(x)), grid

Page 17: Computer Simulation Lab

SUNY-New Paltz

Polar plotsx = r cos(θ),y = r sin(θ),

x = 0:pi/40:2*pi;polar(x, sin(2*x))grid

θ

r

Page 18: Computer Simulation Lab

SUNY-New Paltz

Plotting rapidly changing mathematical functions

fplot

x = 0.01:0.001:0.1;plot(x, sin(1./x))fplot(’sin(1/x)’, [0.01 0.1])

.001

.0001

Page 19: Computer Simulation Lab

SUNY-New Paltz

3-D plots

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

plot3(x, y, z)

Page 20: Computer Simulation Lab

Exercise

1. Let t run from 0 to 10*pi

2. Plot the circle sin(t) versus cos(t)

3. Plot let the above circle to morph as a spiral by multiplying it by a 1/(10*pi)*t function.

4. Use a 3-D plot to rise the spiral from the x-y plane.

SUNY-New Paltz