computer simulation lab
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 PresentationTRANSCRIPT
SUNY-New Paltz
Computer Simulation Lab
Electrical and Computer Engineering Department
SUNY – New Paltz
“Lecture 5”
SUNY-New Paltz
objectives
MATLAB’s high-level 2-D and 3-D plotting facilities
Introduction to graphics
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])
Exercise
SUNY-New Paltz
10
10
20
10
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 autov = axisaxis manualaxis 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)
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.
SUNY-New Paltz
New Graphical Windows
h = figure;
figure(h)
clf % clear figure
cla % clear all figures
Handle
SUNY-New Paltz
Logarithmic plots
semilogy(x, y)
x = 0:0.01:4;
semilogy(x, exp(x)), grid
SUNY-New Paltz
Polar plotsx = r cos(θ),y = r sin(θ),
x = 0:pi/40:2*pi;polar(x, sin(2*x))grid
θ
r
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
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)
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