Matlab Plots - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Matlab Plots

Description:

... is a scalar multiplication of each element in the alpha vector to create the x vector) ... Note the presence of the axis labels, plot title, and grid lines ... – PowerPoint PPT presentation

Number of Views:232
Avg rating:3.0/5.0
Slides: 16
Provided by: Walt90
Category:

less

Transcript and Presenter's Notes

Title: Matlab Plots


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

2
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

3
  • 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 81 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

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
4
close all clear clc format compact alpha
02360 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 firstincrementlast
  • 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.

5
  • Execute plot2.m from the console window and
    verify that you can get the results shown

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
6
close all clear clc format compact for i
11181 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)
    2pi/180 y(2) sin(x(2)) or y(2)
    sin(2pi/180)
  • The loop is repeated until i 181 and x(181)
    2(181-1)pi/180 or x(181) 2pi y(181)
    sin(x(181)) or y(181) sin(2pi)

7
  • Execute plot3.m from the console window and
    verify that you can get the results shown

8
  • 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.
  • ?max represents the maximum angle of the motion
    and is referred to as the amplitude of the
    cycle.

9
  • 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
  • ? is the angular frequency and is given in
    radians per unit time.

10
close all clear clc format compact g
9.807 L input('enter pendulum length
(m) ') thetaMax input('maximum angle (deg)
') T 2pisqrt(L/g) f 1/T omega 2pi/T
disp('period (secs) ') disp(T) disp('cyclical
frequency (Hz) ') disp(f) disp('angular
frequency (rad/s) ') disp(omega) dt
T/100 for i 11101 t(i) (i-1)dt
theta(i) thetaMax sin(omegat(i)) end
plot(t,theta) xlabel('time (s)') ylabel('angula
r 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)

11
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.
enter pendulum length (m) 5 maximum angle (deg)
30 period (secs) 4.4864 cyclical frequency
(Hz) 0.2229 angular frequency (rad/s)
1.4005
12
close all clear clc format compact tau
40 dt 3tau/100 for i 11101 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 ? (tau) is referred to as the time
    constant.
  • Note that when 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)

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

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
15
Assignment Complete and submit the following
m-files plot1.m plot2.m plot3.m pendulum
.m exponential.m dampedPendulum.m
Write a Comment
User Comments (0)
About PowerShow.com