You are on page 1of 15

Matlab Plots

This presentation should be viewed as a slideshow. Use the alt-tab key


combination to switch between the slideshow and Matlab as you work through
the examples.
Pay close attention to each program step and explanation. Be sure that you
understand the purpose of each program instruction. It is not a typing exercise.
There will be a challenging program to for you to write at the end of the tutorial.
close all;
clear;
clc;
format compact;

alpha = [0 45 90 135 180 225 270 315 360];
x = alpha * pi/180;
y = sin(x);

disp('x = ');
disp(x);
disp('y = ');
disp(y);

plot(x, y);
xlabel('x axis label');
ylabel('y axis label');
title('plot title');
grid on;
Create a Matlab m-file with the lines shown:
initialization (close all previously open plot
windows, clear workspace, clear command
console, use compact output formatting)
create the row vector alpha containing a
sequence of angles from 0 to 360 degrees on
increments of 45 degrees
create a second row vector x with the angles
converted from degrees to radians (note: this
is a scalar multiplication of each element in
the alpha vector to create the x vector)
calculate the sine of each angle and store
the result in a third vector , y (note: the sin()
function is applied to each element of the x
vector to create the y vector)
display the values stored in x and y
plot y vs. x
add labels for the x and y axes
add a title for the plot
add grid lines
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x axis label
y

a
x
i
s

l
a
b
e
l
plot title
x =
Columns 1 through 5
0 0.7854 1.5708 2.3562
3.1416
Columns 6 through 9
3.9270 4.7124 5.4978 6.2832
y =
Columns 1 through 5
0 0.7071 1.0000 0.7071
0.0000
Columns 6 through 9
-0.7071 -1.0000 -0.7071 -0.0000
Save the m-file in your current directory
as plot1.m
Execute the m-file by typing prog1 in the
command console
You should see a new plot window open
with the x-y plot shown at left
Note the presence of the axis labels, plot
title, and grid lines
The command console should contain the
output shown
Note that since we divided the total 360
degree range of angles into 8 intervals of
45 degrees, there are a total of 8+1 or 9
values of alpha, x, and y
With only 9 plot points and straight line
segments connecting the points, the sine
function plot has a jagged appearance
more plot points would help
close all;
clear;
clc;
format compact;

alpha = [0:2:360];
x = alpha * pi/180;
y = sin(x);

disp('x = ');
disp(x);
disp('y = ');
disp(y);

plot(x, y);
xlabel('x axis label');
ylabel('y axis label');
title('plot title');
grid on;
Modify plot1.m as shown and save the new
version as plot2.m
The vector alpha is created using the colon
operator to specify the sequence of angle
values
The syntax for a sequence specified by the
colon operator is
alpha = [first:increment:last]
In this case, the sequence starts with 0, ends
at 360, and each value is separated by 2
Note:
The sequence of values is generated by
the colon operator.
Placing square braces [] around the
operator causes the creation of a vector
to store the values.
The equal sign assigns the new vector
to the variable alpha. The equal sign is
an assignment operator, not a statement
of equality.
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x axis label
y

a
x
i
s

l
a
b
e
l
plot title
x =
Columns 1 through 6
0 0.0349 0.0698 0.1047
0.1396 0.1745
Columns 7 through 12
0.2094 0.2443 0.2793 0.3142
0.3491 0.3840
Execute plot2.m from the console window
and verify that you can get the results
shown
close all;
clear;
clc;
format compact;

for i = 1:1:181
x(i) = 2*(i-1) * pi/180;
y(i) = sin(x(i));
end

plot(x, y);
xlabel('x axis label');
ylabel('y axis label');
title('plot title');
grid on;
Modify plot2.m as shown and save the new version
as plot3.m
Note that the disp commands have been deleted
This version of the program uses a for-loop to
control the program flow
The command specifies that the lines contained
between the for and end commands should be
repeated for each value of the loop variable i
Note that the values of i are specified with the colon
command
Within the loop, new elements are added to the x
and y vectors for each value of i
For the first pass through the loop, i = 1 and
x(1) = 2*(1-1)*pi/180 or x(1) = 0
y(1) = sin(x(1)) or y(1) = sin(0)
For the second pass through the loop, i = 2 and
x(2) = 2*(2-1)*pi/180 or x(2) = 2*pi/180
y(2) = sin(x(2)) or y(2) = sin(2*pi/180)
The loop is repeated until i = 181 and
x(181) = 2*(181-1)*pi/180 or x(181) = 2*pi
y(181) = sin(x(181)) or y(181) = sin(2*pi)
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x axis label
y

a
x
i
s

l
a
b
e
l
plot title
Execute plot3.m from the console window
and verify that you can get the results
shown
The sine and cosine functions are of particular importance
in physics and engineering as they provide a mathematical
description for harmonic or periodic phenomena, i.e.,
processes that repeat.
Consider a simple pendulum. From basic physics and
calculus, it can be shown that the angular motion is given
approximately by





T is the time required to complete one cycle of the motion
and is referred to as the period of the cycle.
u
max
represents the maximum angle of the motion and is
referred to as the amplitude of the cycle.
g
L
T
T
t
t
t u u
2
2
=
|
.
|

\
|
= sin
max
An alternate representation of the motion is given by





f is the cyclical frequency or the number of cycles per unit
time. It is typically given in units Hz or cycles per second.
And a third representation is given by



e is the angular frequency and is given in radians per unit
time.
( )
T
f
t f
1
2
=
= t u u sin
max
( )
T
f
t
t
t e
e u u
2
2 = =
= sin
max
close all;
clear;
clc;
format compact;

g = 9.807;
L = input('enter pendulum length (m): ');
thetaMax = input('maximum angle (deg): ');
T = 2*pi*sqrt(L/g);
f = 1/T;
omega = 2*pi/T;

disp('period (secs): ');
disp(T);
disp('cyclical frequency (Hz): ');
disp(f);
disp('angular frequency (rad/s): ');
disp(omega);

dt = T/100;
for i = 1:1:101
t(i) = (i-1)*dt;
theta(i) = thetaMax * sin(omega*t(i));
end

plot(t,theta);
xlabel('time (s)');
ylabel('angular displacement (deg)');
title ('Pendulum Motion');
Create the m-file shown and save it as
pendulum.m
Note that one period of the motion is
divided into 100 equal time steps.
Including the angular displacement at the
starting time or t = 0 results in 101 values for
the time and angle vectors.
We are plotting theta (y-axis) vs. t (x-axis)
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
-30
-20
-10
0
10
20
30
time (s)
a
n
g
u
l
a
r

d
i
s
p
l
a
c
e
m
e
n
t

(
d
e
g
)
Pendulum Motion
enter pendulum length (m): 5
maximum angle (deg): 30
period (secs):
4.4864
cyclical frequency (Hz):
0.2229
angular frequency (rad/s):
1.4005
Execute pendulum.m and verify that you can
get the results shown for a pendulum length
of 5 m with a maximum angular deflection of
30 deg.
close all;
clear;
clc;
format compact;

tau = 40;
dt = 3*tau/100;
for i = 1:1:101
t(i) = (i-1)*dt;
a(i) = exp(-t(i)/tau);
end

plot(t,a);
xlabel('time (s)');
ylabel('amplitude');
title('Exponential Decay');

Another functional form that appears frequently in
physics and engineering is the exponential


When the coefficient in the exponent is negative, the
exponential asymptotically approaches zero for large
values of t. This provides a mathematical description for
processes that decay or are attenuated over time.
For decaying processes, it is common write the
exponential in terms of a time constant


where t (tau) is referred to as the time constant.
Note that when t = t


or, the parameter y has been attenuated to 36.8% of its
value at t = 0
Create the program shown and save it as exponential.m
(note: time constant is set to 40 s)
bt
Ae y =
t
t
e y

=
368 0
1
. = =

e y
0 20 40 60 80 100 120
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
time (s)
a
m
p
l
i
t
u
d
e
Exponential Decay
Execute exponential.m and verify that you
can get the results shown.

Note that the amplitude has been reduced
to 0.368 at t = 40 s as expected.
0 5 10 15 20 25 30 35 40 45
-30
-20
-10
0
10
20
30
time (s)
a
n
g
u
l
a
r

d
i
s
p
l
a
c
e
m
e
n
t

(
d
e
g
)
Pendulum Motion
enter pendulum length (m): 5
maximum angle (deg): 30
time constant (s):20
period (secs):
4.4864
cyclical frequency (Hz):
0.2229
angular frequency (rad/s):
1.4005
The exponential and sine functions can be
combined to represent the motion of a
damped pendulum, i.e., a pendulum that
is affected by friction.


The exponential function creates an
attenuation envelope which reduces the
amplitude of the oscillations over time.
Create a new Matlab m-file called
dampedPendulum.m that will generate
the results shown for a pendulum length
of 5 m, initial deflection angle of 30 deg,
and a damping time constant of 20 s.
For the calculations, the time increment
was set to 1/100 of the period of the
oscillation. The total time for the
calculation was set to 10 oscillation
periods for a total of 1001 time steps.
( ) t e
t
e u u
t
sin
max
=

Assignment:
Complete and submit the following m-files:
plot1.m
plot2.m
plot3.m
pendulum.m
exponential.m
dampedPendulum.m

You might also like