You are on page 1of 39

ENGR 2340: A Guide to Dynamics

Halley Pollock-Muskin and Toni Saylor


11/13/2014

Contents
1 Coordinate Systems
1.1 Cylindrical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Spherical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Intrinsic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Math
2.1 Coordinate Transforms . . . . .
2.2 3D Rotation in Space . . . . .
2.2.1 3-1-3 . . . . . . . . . . .
2.2.2 3-2-1 . . . . . . . . . . .
2.3 Linear Momentum and Impulse
2.4 Angular Momentum . . . . . .
2.5 Absolute Velocity . . . . . . . .
2.6 Absolute Acceleration . . . . .
2.7 Eulers Equations . . . . . . . .
2.8 Laplace Transform . . . . . . .
2.8.1 Poles in s plane . . . . .
2.8.2 Background on Laplace
2.9 Harmonic Response . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

3 Simulation Process

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

2
2
4
5
7
8
9
9
10
10
11
11
12
12
13
14
14
14
16

4 Useful Matlab Functions


16
4.1 ode45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1.1 events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.1.2 RelTol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5 Graphing and Visual Simulation
5.1 Three Dimensional Graphing .
5.2 Surface and PColor Plots . . .
5.3 Animations . . . . . . . . . . .

Help
20
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6 In Class Problems and Solutions


23
6.1 Linear Momentum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6.2 Pendulum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6.3 Absolute Acceleration of Mass on a Rotating Table . . . . . . . . . . . . . . . . . . . 28
1

6.4
6.5

Rotating Reference Frames: Propeller Tip . . . . . . . . . . . . . . . . . . . . . . . . 30


Gyroscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

7 Code for In-Class Problems


35
7.1 Projectile Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7.2 Gyroscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Coordinate Systems

This section will explain and give an example of the polar, spherical, cylindrical, and intrinsic
coordinate systems.

1.1

Cylindrical

The cylindrical coordinate system is essentially spherical (also called polar) in 2D. This is confusing,
because the cylindrical coordinate system is three dimensional. However, it is simply taking the r
and the of polar and extruding that out in the z direction. Thus, the unit vectors in the cylindrical
coordinate system are er , e , and ez .

Figure 1: A 3D view of the cylindrical set up.

x
Figure 2: The same set up in 2D
Looking at Figure 1 it is clear that:
~r(t) = r(t)
er + z(t)
ez
= xi + yj + z k

(1)
(2)

It is important to note that er contains .



d
~v = ~r(t)
dt 01

d
= (r
er + z
ez
dt 01
= r
er + re r + z
ez + z e z

(3)
(4)
(5)

In this case, z goes to zero because ~r(t) is not moving in the z direction. We can, however, find
e r .
We know from coordinate transforms (section 2.1) that:
 
 0 
er
cos() sin() er
=
e0
sin() cos() ez

d
e0 er
er = lim r

t 0
dt 01
t
cos()
er + sin()
e er
= lim
t 0
t
For small values of , sin() goes to , and cos() goes to 1. So:

d

e
er = lim

t 0 t
dt 01
d
=
e
dt
e
= (t)

(6)
(7)
(8)

(9)
(10)
(11)

In short,
e
e r =
and
er
e =
. Inputting this into the formula for velocity we get:
e + z
~v = r
er + r
ez
When we take the derivative of this, we find that
e + zez
~a = (
r r2 )
er + (r + 2r )

1.2

Spherical

Rather than using two lengths and an angle as in the cylindrical coordinate system, spherical uses
two angles, and and one length, .

Figure 3: An example of the spherical coordinate system.


Getting from the rectangular coordinate system to the spherical takes two rotations. If you are
confused about coordinate transforms or successive rotations, see sections 2.1 and 2.2. The process
is as follows.


i
e1
cos() sin() 0
e2 = sin() cos() 0 j
(12)
e3
0
0
1
k


e
cos() 0 sin() e1
e = 0
e2
1
0
(13)
er
sin() 0 cos()
e3
The rotation matrix that results from those rotations is:

cos()cos() cos()sin() sin()


sin()

cos()
0
sin()cos() sin()sin() cos()
4

Now, like in cylindrical, we want to know the derivatives of the unit vectors we are using. Youre
going to have to trust me (and Chris 2014 notes) that the following is correct.
d
er
= e
d
d
e
=0
d
d
e
=
er
d
d
er
= sin()
e
d
d
e
= sin()
er cos(phi)
e
d
d
e
= cos()
e
d
Using those definitions for our unit vector derivatives, we can calculate the acceleration:
~r(t) =
er

d
~v (t) = ~r(t)
dt

(14)
(15)
(16)
(17)
(18)
(19)

(20)
(21)

01

=
er + e r

(22)

er

er
=
er + (
+
)

e + sin()

=
er +
e

(23)
(24)
(25)

~a(t) = [
2 2 sin2 ()]
er + [ + 2 2 sin()cos()]
e

+[sin()
+ 2 sin
+ 2 cos()]
e

1.3

(26)
(27)

Intrinsic

The intrinsic coordinate system is useful when you know the path of your object (e.g. the roller
coaster problem).
t

n
t

Figure 4: Chris Lees roller coaster problem diagram modified for our purposes.
As you can see above in Figure 4, when you have a path, the intrinsic coordinate system at
any point s along the path is the vector, en normal to the path at that point, and the vector, et
tangential to the path at that point. Just to clarify:
5

S is the position along the path. en is normal to the path at s. et is a function of s or t, and is
tangent to the path at s.
The direction of en depends on the concavity of the path at that point. You can see that at the
first and second point in Figure 4 en is pointing inward with respect to the concavity. If you have
a path where the line is straight, en can point in either direction.
Now well go through finding the velocity and acceleration of a particle in the intrinsic coordinate
system:

d
~v = ~r(t)
(28)
dt 01

d ~
(29)
= R(s)
dt 01
~ ds
dR
=
(30)
ds dt
~
dR
~v = s
(31)
ds
Now we need to find

~
dR
ds :

~ = R(s
~ + s) R(s)
~
R
~
~ + s) R(s)
~
dR
R(s
= lim
s 0
ds
s
s
et
=
s
= et

(32)
(33)
(34)
(35)

So the velocity of a particle in the intrinsic coordinate system is:


~v = s
et
Next we find the acceleration:

d2
~a = 2 ~r(t)
dt 01

d2 ~
= 2 R(s)
dt

(36)
(37)

01

= se t
en
e t =
en
~a(t) = set + s
We need to eliminate from this equation.

(38)
(39)
(40)

Figure 5: Chris Lees drawing from his fall 2014 notes.


As you can see from Figure 5,
s =
and
s =
so,
s
~a(t) = set + en

So how do we find ? We already have an expression for in terms of and s, but we need in
terms of x and y.

ds

dy

dx
Figure 6: Chris Lees drawing from his fall 2014 notes.
The relationship between x and y is clear from Figure 6. From that figure it is clear that:
dy
= tan()
dx
ds2 = dx2 + dy 2

(41)
(42)

Math

This section will use Chris notes from the fall of Dynamics in 2014 to explain the mathematical
concepts in the course.

2.1

Coordinate Transforms

Vectors in space can be written in any reference frame. If you are need to combine vectors that are
in different reference frames, you will need to transform the vectors so that they are in the same
reference frame. A reason you might need to combine vectors in different reference frames can be
seen in example xyz.
First, a simple 2D example:

Where ~r is a vector in the ij plane.


Knowing theta, we can write:
i0 = cos()i + sin()j
j 0 = sin()i + cos()j
Which, represented in matrix form, is:
 
 0 
i
cos() sin() i
j 0 = sin() cos() j

(43)
(44)

(45)

Also from figure 1 we can determine the vector r in terms of x, y, x, and y. If we know the x
and y components of ~r, we can fairly easily convert the vector into the ij plane.
~r = xi + yj = x0i0 + y 0 j 0

(46)

Rearranging equation 4, we can find:


 
 
 0 0  x0

 x
i j
= i j
y
y0

(47)

To move on from here, it should be noted that:


 0 T
i
 0 0

j 0 = i j

(48)

If we call the matrix of sines and cosines from equation 3 [R], we can write equation 3 as:
[i0 ; j 0 ]T = [R] [i; j]
In 3 dimensions,

(49)

Figure 7: As you can see, the x and y direction have rotated around the z axis.

0

x
x


y 0 = R y
z0
z

cos() sin() 0
[R] = sin() cos() 0
0
0
1

2.2

(50)

(51)

3D Rotation in Space

Euler angles are a set of 3 angles that specify an orientation in space. In this case, we will look
at three successive rotations in space about a body fixed axis. This means that the axis that is
rotating is fixed to the object we are rotating. There are two common cases, a 3-1-3 rotation and
a 3-2-1 rotation. This section will derive the rotation matrix for a 3-1-3 rotation, and also give the
rotation matrix from a 3-2-1.
2.2.1

3-1-3

The first rotation is about the z axis. The rotation matrix that results from that is:
0

x
cos() sin() 0
x
y 0 = sin() cos() 0 y
z0
0
0
1
z
The second rotation is about the x axis.
00
0
x
1
0
0
x
y 00 = 0 cos() sin() y 0
z 00
0 sin() cos()
z0
The third rotation is about the z axis.
000
00
x
cos() sin() 0
x
y 000 = sin() cos() 0 y 00
z 000
0
0
1
z 00
9

(52)

(53)

(54)

But we want to know how to get straight from the body frame to the triple prime frame:
000

x
x
y 000 = [R ][R ][R ] y
(55)
z 000
z
[R313 ] = [R ] [R ] [R ]

000
x
x
y = [R313 ]1 y 000
z
z 000
[R313 ]1 =

(56)
(57)
(58)

cos()cos() cos()sin()sin()
sin()cos() + cos()cos()sin() sin()sin()
cos()sin() cos()sin()cos() sin()sin() + cos()cos()sin() sin()cos()
sin()sin()
sin()cos()
cos()
2.2.2

3-2-1

For a 3-2-1 rotation, the first rotation is about the z-axis (yaw). The second rotation is about
the y-axis (pitch). The final rotation is about the x axis (roll). The R321 1 matrix that results
from following the process we followed to find the 3-1-3 is:

cos()cos()
cos()sin()
sin()
sin()sin()cos() cos()sin() sin()sin()sin() cos()cos() sin()cos()
cos()sin()cos() + sin()sin() cos()sin()sin() sin()cos() cos()cos()

2.3

Linear Momentum and Impulse


F~ = m~a

(59)


d
= m ~v
dt 01

(60)
(61)

If m is constant, then:

d
m ~v = p~
dt 01
If we take the integral of both sides we can see:
Z t2
Z
~
F dt = m
t1

t2

~adt

(62)

t1

t2

p~dt

(63)

t1

= m(~v (t2 ) ~v (t1 ))


So we have come to a formula for impulse, I =

R t2
t1

10

m~adt.

(64)

2.4

Angular Momentum
~ 0 = ~rxm~v
H

(65)

= |~r||m~v |sin()k

(66)

In two dimensions,
~r = rxi + ry j
m~v = m(vxi + vy j)

(67)

H0 = (rxi + ry j)xm(vxi + vy j)
= m(rx vy )(ixj) + m(ry vx )(jxi)

(69)

(68)

= m(rx vy ry vx )k

2.5

(70)
(71)

Absolute Velocity

If you have a point on a rotating rigid body, and you need to find velocity, one way to do it is to
use the rotating referecne frame approach.
Each time you use this approach, you will have two things. First, an inertial frame: this is the
normal, everyday, ij k frame, not rotating. You will also have a rotating rigid body. The frame
attached to and rotating with the rigid body is called the body frame. You may also have any
number of intermediate frames rotating at different angular velocities with respect to the inertial
and body frames.
Rather than derive the expression for absolute velocity, we are just going to go through an
example of finding it.
Lets say we have the situation below.

rp/03
rp/01
r03/02
r02/01
Where the point, P, and its frame, 03, are rotating with respect to the 02 frame, and the 02
frame is rotating with respect to the 01 frame.
To find absolute velocity, we would take the derivative of ~rP/01




d
d
d
d
1~r
=
1~r
+
1~r
+
1~r
(72)
dt 0 P/01 dt 0 02/01 dt 0 03/02 dt 0 P/03
But, we cant take the derivatives of the vectors ~r03/02 and ~rP/03 in the 01 frame, because their
frames are changing direction in the 01 frame. We can, however, take the derivative of them in

11

their own frame. We will get extra terms from this transformation, so the formula looks like this:
~ 02 + 02
~ 03 = 01
~ 03 )
(It is important to note here that by the angular velocity addition theorem, 01




d
d
d
d
~
~ 03 ~r
01
02
1~r
=
1~r
+
2~r
+ ~r03/02 + 3~rP/03 + 01
(73)
P/03
dt 0 P/01 dt 0 02/01 dt 0 03/02
dt 0
Were going to stop here with the math. If you need more detail about what to plug into this
equation, check out some of the in class problems.

2.6

Absolute Acceleration

So, the absolute acceleration is very similar to the absolute velocity. It is, in fact, almost identical,
except that you are taking the second derivative, which gives you several extra terms. The derivation of the formula is below. If youre interested in knowing what to plug in for these values, see
the in class problems.




d2
d2
d2
d2
1~
r
=
1~
r
+
1~
r
+
1~r
(74)
dt2 0 P/01 dt2 0 02/01 dt2 0 03/02 dt2 0 P/03


d2
d2
1~r
=
1~r
+
(75)
dt2 0 P/01 dt2 0 02/01



d 01 ~ 02
d
d2
~
~ 02 (01
~ 02 ~r
01
02
2~r
+ 2 ~r03/02 +
~r03/02 + 01
(76)
03/02 )+
dt2 0 03/02
dt 02
dt 02



d2
d
d 01 ~ 03
~
~ 03 (01
~ 03 ~r
01
03
3~r
+ 2 ~rP/03 +
~rP/03 + 01
(77)
P/03 )
dt2 0 P/03
dt 03
dt 03

2.7

Eulers Equations

The inertia matrix is

Ixx Ixy Ixz


[I] = Iyx Iyy Iyz
Izx Izy Izz

(78)

R
R
2 + k 2 )dm. For example, I
2
2
Note that Ixy = Iyx . Also, Iii = m (j
=
xx
R
R m (y + z )dm. This term is
larger than 0. Additionally, Iij = m (ij)dm. For example, Ixy = m (xy)dm. This term can be
greater than, less than, or equal to 0. This is for a body based coordinate system.
The equations of motion for the rotation of a rigid body relate external applied moments to
change in angular momentum to change in orientation.
X
~0 = H
~0
M
(79)

X

~ G = d H~G
(80)
M
dt 01
~ = I~
H
~
(81)
 
X

~ G = d I~
M
~
(82)
dt 01

 
X
d ~ 
~
~ +
~ I~
~
(83)
MG = I
dt
~

~
~

12

02


~ is 0 in the in the O2 frame. Thus, written with respect to the b-frame,
I~

02

d
dt


d 01 ~ 02 01 ~ 02 ~ 01 ~ 02 
~
~
MG = I
+ I
dt 02

(84)

~ will give us three first order ODEs that we can solve to get
~ , and M
~ . We
Multiplying out I~,
can solve the first order ODE Euler rate equations (3-1-3 or 3-2-1) to get , , and .
The procedure for Eulers equations is as follows:
1. Choose principal axis of body (consider this to be the b-frame).
~

2. Determine I~G . Find principal moments of inertia, I11 , I22 , I33 , which are the eigenvalues of I~
3. (a) Determine
~ with respect to the b-frame.
(b) Transform
~ to b1 b1 + b2 b2 + b3 b3


d
~ =
~ = b1 b1 + b2 b2 + b3 b3
4. Determine dt
02

5. Evaluate the right hand side after putting everything in the b-frame.
P ~
~B
~ = |A||
~ B|
~ sin
MG use cross product definition, A
6. (a) Determine
P
~ G = M1b1 + M2b2 + M3b3
(b) Transform to b-frame,
M
7. Equate LHS = RHS, after both are in ~b-frames. Extract the three scalar equations. This will
~ to
relate the applied M
~ b.
8. Get orientation of rigid body
(a) Choose Euler angles (3-1-3 or 3-2-1)
(b) Euler rate equations give three first order ODEs, relating b to , ,

2.8

Laplace Transform

The Laplace transform turns a function in the time domain, f(t), into a function of a complex
variable, s= + i. We will use this to solve linear equations of motion, as it turns ODEs into
algebraic equations. The proceedure to do this is:
1. Take the Laplace transform of equations of motion
2. Do algebra in the s domain
3. Take the inverse of the Laplace transform to return to the time domain from the s domain.
The definition of the Laplace transform is as follows:



L f (t) F (s) =

Z
0

13

f (t)est

(85)

2.8.1

Poles in s plane

After applying the Laplace transform, look at the resulting numerator and denominator. Zeros
occur when the numerator is 0, and poles occur when the denominator is 0. The pole placement
describes the behavior of the function.

2.8.2

Background on Laplace

The Laplace transform is the Fourier transform for every value of .


For the function f (t) = 2e2t cos(t) 3e2t sin(t), the Laplace transform is F (s) = s22s+1
. To
+4s+5
2
find the poles, we set the denominator of F(s), s + 4s + 5, equal to 0. We find that the poles are
2 i and 2 + i. The zeros are found by setting the numerator equal to 0, and 2s + 1 = 0 gives
the solution of 1/2. Because the poles are complex, f(t) is an exponentially modulated sinusoid.
The graph shows poles of F(s), and the rainbow graph demonstrates how the Laplace transform is
the Fourier transform for all . Every line in this graph is a Fourier transform.

2.9

Harmonic Response

This is essentially a mass-spring damper system (you may remember this from Linearity). Some
input, F(t), m
x + cx + kx some output, x(t), where x(t) is displacement with respect to
equilibrium and F(t) is the force applied to the system. Note that c is empirical, and thus must be
measured. The damping in this system is typically viscous. The basic components of this physical
system are inertia, stiffness, and energy dissipation.
Its important to note that the m-c-k system is linear in x(t). This means that we dont need

14

to solve the ODE. For viscous damping, by definition,


m
x + cx + kx = F (t)
cx
kx
F (t)
x
+
+
=
m
m
m
F
(t)
x
+ 2n x + n2 x =
m
where is the damping ratio, or c/(2n m). The natural frequency, n , is equal to
parameters will specify the response.
To get x(t), start by assuming x(t)=et . Then,

(86)
(87)
(88)
q

k
m.

These two

x = et

(89)

x
= 2 et

(90)

Now, take the free response, F(t) = 0.


F (t)
m
=0

x
+ 2n x + n2 x =

(91)

2 et + 2n et + n et


2 + 2n + n et = 0

(92)

2 + 2n + n = 0

(94)

Using the quadratic formula, we find that the roots of this are:
p
1,2 = n n 2 1

(93)

(95)

We can show that the total solution is a linear combination. You may have seen the following
equation in Linearity.
x(t) = c1 e1 t + c2 e2 t

(96)

From this, we get


x0 2 v0
2 1
v0 x0 1
c2 =
2 1
c1 =

(97)
(98)

Note that if m0, c 0, and k0, then n > 0. Additionally, k0 is an unstable solution and
m
x kx = 0 diverges. When k0, n > 0.
There are four types of responses when n > 0. These are:
1. Undamped: = 0, 1,2 = in which is two complex roots
p
2. Overdamped: > 1 2 1 > 0, and 1,2 = n n 2 1 which is two real roots
3. Critically damped: = 1 2 1 = 0, and 1,2 = n which is two real, repeated roots
4. Underdamped: 0 < < 1 2 1 < 0, and 1,2 = a bi, where the roots are two complex
conjugates
Undamped systems keep oscillating. Overdamped systems do not oscillate and rather go straight
to equilibrium. Critically damped systems approach equilibrium the fastest and do not oscillate.
Underdamped systems oscillate frequenty and approach equilibrium.
15

Simulation Process

This section will explain the best process to doing a dynamics simulation. Essentially, the process
is:
1. Find acceleration
2. Free body diagram
3. F = ma
4. Solve for state variables
To lend more clarity to the situation, if I had a double pendulum problem, I would:
1. use the rotating reference frame approach to find absolute acceleration.
2. draw a free body diagram
3. find acceleration by solving F = ma
4. set the accelerations equal to each other
5. arrange the resulting equations with all state variables on one side, and all other terms on
the other
6. either make a matrix out of these equations, or skip this step and
7. enter into matlab
Making a matrix out of the equations is not as hard as it sounds. Simply make a column vector
of your state variables. Imagine that you were multiplying a matrix by your column vector that
would result in a matrix or vector of the values on the rhs of your equation. Your matrix would
look like the coefficients of each state variable in each equation. For a terrible example, if I had the
equations
2x + 3y + 4z = 5l
6x + 7x + 8y = 9l
10x + 11y + 12z = 13l
then my equation in matrix form would

2
6
10

look like:

3 4
x
5l

7 8
y = 9l
11 12
z
13l

Useful Matlab Functions

This section will detail Matlab function useful in creating simulations.

16

(99)

4.1

ode45

ode45 is the matlab function you will be seeing the most of in dynamics. You will use it to solve
differential equations. Essentially, whenever youre doing a simulation and you want to solve for
acceleration, you will use ode45 to get you from position and velocity to acceleration. Below is an
example of code using ode45 to simulate the motion of a baseball. We are going to go through it
section by section. This code was given to fall 2014 dynamics students by Nick Eyre 15.
% Parameters
g = [ 0 ; 9 . 8 1 ] ; % m/ s 2
Cd = 0 . 3 ;
rho = 1 . 3 ; % kg /m3
Area = 4 . 2 e 3; % m2
These statements are defining the variables needed to calculate acceleration in this case. For
the baseball, the only forces acting on it are gravity and drag. To calculate the accelerations due
to those forces, we need t o know the acceleration due to gravity, the drag constant, the density of
the medium the ball is flying through, and the area of the balls surface in contact with the force.
% State Variables : [ x , y , x velocity , y v e l o c i t y ]
% I n i t i a l Conditions
X0 = [ 0 1 45 c o s d ( 3 5 ) 45 s i n d ( 3 5 ) ] ;
This statement is defining your state variables, which are the values you are giving ode45 in
order for it to be able to solve for whatever you want it to solve for. Less vaguely put, in this case
your state variables are position and velocity. In order for ode45 to solve for acceleration, it needs
to know the initial conditions for position and velocity.
% Time Span ( s e c o n d s )
Tspan = [ 0 4 0 ] ;
To run ode45 you need to give it a time span to run for. The time span is not real life time as
measured by you, but it is time in your simulation.For now, our time step is 40 seconds.
% Setup Events Function t o End S i m u l a t i o n When B a l l H i t s Ground
o p t i o n s = o d e s e t ( Events , @ e v e n t s f u n c t i o n ) ;
We will talk about events functions and other options in the subsection, but for now this is how
you declare an option for ode45.
% Run ODE S i m u l a t i o n and Output Time and S t a t e s
[ T, X] = ode45 ( @ d e r i v a t i v e s f u n c t i o n , Tspan , X0 , o p t i o n s ) ;
ode45 calls must be formatted this way. In brackets you indicate what you want ode45 to solve
for. In this case we are solving for a column vector of time points and an array of solutions. Each
row of X corresponds to a solution at the time returned in the corresponding row of T.
Then, you call ode45. You give ode45 three things always. The first is the name of the function
you are running ode45 on. In this case, derivativesf unction. Then, you give it the time span you
want it to run for. Third, you give it the initial values of your state variables. You can also give
ode45 options. Some of the options you will use are discussed later.
%
%
%
%

Calculate Derivatives of a given state


X i s t h e c u r r e n t s t a t e [ x y vx vy ]
T i s t h e c u r r e n t time
D e r i v a t i v e s c o n t a i n s d e r i v a t i v e s o f s t a t e v a r i a b l e s [ dx/ dt dy/ dt dvx/ dt dvy/ dt ]
17

f u n c t i o n d e r i v a t i v e s = d e r i v a t i v e s f u n c t i o n ( Time , InCon )
This is an example of an ode function. The name of the function is derivativesf unction, and
it looks like it takes in time and initial conditions. It should be noted that ode45 knows to give the
function Tspan and X0 no matter what you name the variables in your function definition. What
does matter is that whatever you are passing back to the ode solver is a variable named derivatives,
because we have definied derivatives = derivativesf unction. If you dont tell ode45 where to look,
this wont work.
% Unpack i n t o V e c t o r s
R = InCon ( 1 : 2 ) ;
V = InCon ( 3 : 4 ) ;
% Vector Math t o C a l c u l a t e A c c e l e r a t i o n
A c c e l = g . 5 rho Area Cdnorm (V) V;
% Output D e r i v a t i v e s
d e r i v a t i v e s = [V; A c c e l ] ;
end
So, into your ode function you are feeding time, position, and velocity. There will be times
when you will use both position and velocity, but in this case you are only using velocity in your
acceleration formula. When you pass information back to the ode though, you pass back velocity
and acceleration. But, the array of solutions X that you get will contain position and velocity
again. The acceleration is simply used to calculate the position and velocity at every point.
If ever you need to (and you will need to) plot acceleration from the ode, you will have to write
a for loop that uses the information from X to calculate the acceleration again.
Just for fun, here is what this outputs.

18

4.1.1

events

There is essentially one events function you will use in dynamics. You will use it to determine when
your simulation should stop. For example, in the baseball code, you would not want to run that
function for the full 40 seconds if it hits the ground at 20 seconds. Nicks code explains the way
in which you check to see if the ball reaches the ground.
% Function t o l o o k f o r p o i n t s o f i n t e r e s t
% Inputs :
% T: c u r r e n t time
% X: c u r r e n t s t a t e [ x y vx vy ]
% Outputs :
% e v e n t v a l u e : v a l u e t h a t we c a r e i f h i t s 0 , i n t h i s c a s e y p o s i t i o n .
% s t o p c a l c u l a t i o n : 1 i f you want t o s t o p t h e s i m u l a t i o n when i t h i t s
%
t h i s event
% d i r e c t i o n : 0 i f you don t c a r e which way i t c r o s s e s 0 through , 1
%
o r +1 i f you do c a r e
f u n c t i o n [ e v e n t v a l u e , s t o p c a l c u l a t i o n , d i r e c t i o n ] = e v e n t s f u n c t i o n (T, X)
e v e n t v a l u e = X( 2 ) ;
stopcalculation = 1;
direction = 0;
end
4.1.2

RelTol

RelTol is another option for ode45. Simply put, RelTol decrease the amount of error allowed in
your data, or the amount of deviation.It increases the accuracy that the ode is looking for. For
example, if I add RelTol to the baseball code, my graph goes from chunky to smooth.
o p t i o n s = o d e s e t ( Events , @ e v e n t s f u n c t i o n , RelTol , 1 e 6);

19

Graphing and Visual Simulation Help

This section will help with graphing and visualization.

5.1

Three Dimensional Graphing

The following is an example of three dimensional graphing. Lets go through it.


% Run ODE S i m u l a t i o n and Output Time and S t a t e s
[ T, X] = ode45 ( @ d e r i v a t i v e s f u n c t i o n , Tspan , X0 ) ;
% Convert output from s p h e r i c a l t o c a r t e s i a n .
[ x , y , z ] = s p h 2 c a r t (X( : , 1 ) , X( : , 2 ) , X ( : , 3 ) ) ;
The first line of this is straightforward to you if you have looked at the earlier section on ode
45. The second line uses the built in Matlab function sph2cart to convert the output of this ode
to the cartesian coordinate system. This code was taken from Toni Saylors 17 homework, and
was used to simulate a marble rolling in a bowl. The state variables in this case were two angles
and a vector. In order to graph a useful quantity, I would want to see what the marble is doing in
a 3D, cartesian reference frame.
Now, heres how to graph it.
% h o l d a l l makes s u r e t h a t a l l o f t h e t h i n g s you a r e
% g r a p h i n g remain on t h e graph , and don t r e p l a c e each o t h e r
hold a l l
% g r i d on makes a g r i d , which i s h e l p f u l i n o r i e n t i n g t h e graph
g r i d on
% f o r some r e a s o n i n 3D graphing , u s i n g h o l d a l l makes Matlab mad
% t o a v o i d Matlab s anger , u s e view ( 3 )
view ( 3 )
% f i n a l l y , p l o t 3 d o e s e x a c t l y what i t sounds l i k e .
plot3 (x , y , z )

20

Figure 8: The output of Toni Saylors 17 marble bowl code

5.2

Surface and PColor Plots

Do they need to know surf and pcolor for dynamics? did we ever use that?

5.3

Animations

An animation can be made with a for loop. Typically, the for loop should run through all the
elements in your position or velocity vector. We can determine the number of elements in an array
by using n = numel(A), which returns the the number of elements, n, in array A. If the output of
our ODE is [x1, y1, x2, y2], we can have the for loop run from 1 to numel(x1). This is because the
vectors x1, y1, x2, and y2 should be the same length. We should also make a new figure before the
for loop. Thus, our for loop begins with:
figure 1
f o r k = 1 : numel ( x1 )
end
In our for loop, we need to clear the figure it is making repeatedly. We also need to hold on
to all the points it finds. We can do this with clf and hold on. It is important to clear the figure
BEFORE holding on to the new point.
figure 1
f o r k = 1 : numel ( x1 )
clf ;
h o l d on ;
end
Next, we need to set the size of the plot. Lets go from -1 to 1 on the x-axis and from -2 to 2
on the y-axis.
21

figure 1
f o r k = 1 : numel ( x1 )
clf ;
h o l d on ;
xlim ( [ min( 1) max ( 1 ) ] ) ;
ylim ( [ min( 2) max ( 2 ) ] ) ;
end
Now we can tell MATLAB what we want to plot. If we were graphing a double pendulum, we
would want to plot the following:
figure 1
f o r k = 1 : numel ( x1 )
clf ;
h o l d on ;
xlim ( [ min( 1) max ( 1 ) ] ) ;
ylim ( [ min( 2) max ( 2 ) ] ) ;
p l o t ( [ 0 , x1 ( k ) ] , [ 0 , y1 ( k ) ] , k ) ;
p l o t ( x1 ( k ) , y1 ( k ) , bo ) ;
p l o t ( [ x1 ( k ) , x2 ( k ) ] , [ y1 ( k ) , y2 ( k ) ] , k ) ;
p l o t ( x2 ( k ) , y2 ( k ) , bo ) ;
end
To animate the plot, we have to add a pause and tell MATLAB to draw after the pause.
figure 1
f o r k = 1 : numel ( x1 )
clf ;
h o l d on ;
xlim ( [ min( 1) max ( 1 ) ] ) ;
ylim ( [ min( 2) max ( 2 ) ] ) ;
p l o t ( [ 0 , x1 ( k ) ] , [ 0 , y1 ( k ) ] , k ) ;
p l o t ( x1 ( k ) , y1 ( k ) , bo ) ;
p l o t ( [ x1 ( k ) , x2 ( k ) ] , [ y1 ( k ) , y2 ( k ) ] , k ) ;
p l o t ( x2 ( k ) , y2 ( k ) , bo ) ;
%// MATLAB p a u s e s f o r 0 . 0 0 1 s e c b e f o r e moving on
%// t o e x e c u t e t h e next i n s t r u c t i o n
%// and t h u s c r e a t e s an a n i m a t i o n
pause ( 0 . 0 0 1 ) ;
drawnow
end
Finally, we can have MATLAB save the animation as a video file.

22

figure 1
a v i o b j = a v i f i l e ( Pendulum . a v i , c o m p r e s s i o n , None ) ;
f o r k = 1 : numel ( x1 )
clf ;
h o l d on ;
xlim ( [ min( 1) max ( 1 ) ] ) ;
ylim ( [ min( 2) max ( 2 ) ] ) ;
p l o t ( [ 0 , x1 ( k ) ] , [ 0 , y1 ( k ) ] , k ) ;
p l o t ( x1 ( k ) , y1 ( k ) , bo ) ;
p l o t ( [ x1 ( k ) , x2 ( k ) ] , [ y1 ( k ) , y2 ( k ) ] , k ) ;
p l o t ( x2 ( k ) , y2 ( k ) , bo ) ;
%// MATLAB p a u s e s f o r 0 . 0 0 1 s e c b e f o r e moving on
%// t o e x e c u t e t h e next i n s t r u c t i o n
%// and t h u s c r e a t e s an a n i m a t i o n
pause ( 0 . 0 0 1 ) ;
drawnow
a v i o b j = addframe ( a v i o b j , g c f ) ;
end

In Class Problems and Solutions

Lets look at projectile motion. We will use ode45 to find position. Imagine
P ~ throwing a 2ball with
F = m~g cv v, where
some initial velocity v0 . The ball travels in the x-y plane. Assume that
c is constant equal to 1/2ACD . Area is A, density is , and CD is the drag coefficient.
First, define the reference frame. The i and j vectors are in the positive x and y direction,
respectively.

23

Figure 9: The reference frame and forces acting on the ball

dy
dt

Second, write equations of motion (EOMs). The derivative of position is velocity:


= vy . We can write this as a matrix.


dx/dt
dy/dt

dR~/dt

dx
dt

= vx and

 
v
= x
vy

(100)

~
=V

(101)
(102)

To calculate the current velocity, we need an expression for acceleration. Recall that F=ma.
F~drag + F~gravity = m~a

(103)

Drag force is 0.5ACD v 2 v, where v includes the direction components of velocity and v 2 is the
magnitude of velocity squared. Thus,
dV /dt

= ~g 0.5ACD v 2 v

Before putting this into MATLAB, lets figure out how to write v 2 and v.
magnitude = norm ( v )
d i r e c t i o n = v/norm ( v )
magnitude d i r e c t i o n = v
magnitude magnitude d i r e c t i o n = vnorm ( v )
We can now write a function with our equations.
f u n c t i o n d e r i v a t i v e s = d e r i v a t i v e s f u n c t i o n (T, X)
% Unpack i n t o V e c t o r s
%
Note : t h i s a l l o w s you t o do v e c t o r math
R = X( 1 : 2 ) ;
24

(104)

V = X( 3 : 4 ) ;
% Vector Math t o C a l c u l a t e D e r i v a t i v e s i n v e c t o r form
dRdt = V;
dVdt = g + . 5 rho Area Cdnorm (V) V;
% Combine V e c t o r s and Output D e r i v a t i v e s
d e r i v a t i v e s = [ dRdt ; dVdt ] ;
end
We can write the gravitational force vector as [0, 9.81], or 0i + 9.81j.
% Parameters
%
(Any P h y s i c a l C o n s t a n t s )
g = [ 0 ; 9 . 8 1 ] ; % m/ s 2
Cd = 0 . 3 ;
rho = 1 . 3 ; %kg /m3
Area = 4 . 2 e 3; %m2
m = 0 . 1 4 5 ; %kg
% S t a t e V a r i a b l e s : ( Always e x p l i c i t l y s t a t e your
% state variables in order
% for ease of reading )
%
[ x , y , x velocity , y velocity ]
% I n i t i a l Conditions
X0 = [ 0 1 45 c o s d ( 3 5 ) 45 s i n d ( 3 5 ) ] ;
% Time Span ( s e c o n d s )
Tspan = [ 0 4 0 ] ;
Now we can call ode45 and tell it to end when the ball hits the ground.
% Setup Events Function t o End S i m u l a t i o n When B a l l H i t s Ground
o p t i o n s = o d e s e t ( Events , @ e v e n t s f u n c t i o n ) ;
% Run ODE S i m u l a t i o n and Output Time and S t a t e s
[ T, X] = ode45 ( @ d e r i v a t i v e s f u n c t i o n , Tspan , X0 , o p t i o n s ) ;
f u n c t i o n [ e v e n t v a l u e , s t o p c a l c u l a t i o n , d i r e c t i o n ] = e v e n t s f u n c t i o n (T, X)
e v e n t v a l u e = X( 2 ) ; %X( 2 ) = y
stopcalculation = 1;
direction = 0;
end
Eventsvalue gives MATLAB the specific value at which to stop. If we wrote (y-1) for eventsvalue,
the simulation would stop when the ball falls below one meter. A stopcalculation value of 1 tells
MATLAB to stop the simulation when the event occurs. Direction tells MATLAB which direction
the ball is going (rising or falling).
We can also calculate energy. Potential energy is mgh, and kinetic energy is 1/2mv 2 , where v is
the magnitude of velocity. In MATLAB, we write:
25

PE = m g ( 2 ) X ( : , 2 ) ; % P o t e n t i a l Energy = mgh , and X( : , 2 ) = y


KE = . 5 mnorm (X ( : , 3 : 4 ) ) 2 ; % K i n e t i c Energy = 1/2 mv 2
% and X( : , 3 : 4 ) = [ vx , vy ]
We can also use a for loop. Both are acceptable methods.
f o r n=1: l e n g t h (T)
PE( n , 1 ) = m g ( 2 ) X( n , 2 ) ; % P o t e n t i a l Energy = mgh
KE( n , 1 ) = . 5 mnorm (X( n , 3 : 4 ) ) 2 ; % K i n e t i c Energy = 1/2 mv 2
end
TE = PE + KE;
% T o t a l Energy
To make graphs, type plot(x component, y component) into MATLAB. Weve now modeled a
projectile!

6.1

Linear Momentum

One block is sliding to the right on a frictionless surface. It collides with a stationary block. After
the collision, both blocks stick together and move to the right. Assume that the mass of the first
block is equal to the mass of the second. Find the velocity after collision in terms of the initial
velocity of the first block.
Because the surface is frictionless and the normal and gravitational forces cancel, the net force
on the system is 0. This means we have conservation of linear momentum.
pai + pbi = paf + pbf
p = mv
This is an inelastic collision, and so the final velocity will be the same for both blocks.

ma vai + 0 = ma + mb vf
1
vf = vai
2

26

(105)
(106)

(107)
(108)

6.2

Pendulum

Figure 10: Caption


Lets use polar coordinates to model the motion of a pendulum. Recall that ~a = (
r r2 )
er +

(2r + re . First, lets use F=ma in the er direction.


Fr = mar


T + mg cos() = m r r2

(109)
(110)

Because r is the length of the string of the pendulum and it is unchanging, r and r terms are
equal to 0. Also, r = length of string = l.
T + mg cos() = ml2
T = mg cos() + ml2

(111)
(112)

Now, lets use F=ma in the e direction.


F = ma

mg sin() = lm
g sin() = l
g sin()
=
l

(113)
(114)
(115)
(116)

Weve now reached a point where we can have MATLAB solve this for us. Our inputs into
We can say z1 = and z2 = .
Then, z1 = = z2 and z2 = = g sin()/l .
ode45 will be and .
In MATLAB, our code would be:
g = 9.81;
l = 1;
27

m = .3;
%D e c l a r e i n i t i a l c o n d i t i o n s
theta0 = pi /2;
thetadot0 = 0;
initZ = [ theta0 thetadot0 ] ;
tspan = [ 0 : . 0 1 : 1 0 ] ;
o p t i o n s= o d e s e t ( RelTol , 10 4);
[ T, X] = ode45 ( @pendulum , tspan , i n i t Z , o p t i o n s ) ;
f u n c t i o n r e s = pendulum ( t , Z )
tdot = Z ( 2 ) ;
t d d o t = g s i n ( Z ( 1 ) ) / l ;
r e s = [ tdot ; tddot ] ;
end
Note that mass is irrelevant to the equations of motion. Also note that the force equation in
the er direction does not include and thus does not affect the ODE we put in MATLAB.

6.3

Absolute Acceleration of Mass on a Rotating Table

A table rotates at uniform speed, , about its center point. A mass, m, moves at constant speed,
v, in a concentric track, relative to the table. What is the reaction force, R, that the track exerts
on m?

28

O1 is our inertial frame. O2 is fixed to the table, and is the b-frame. O3 is fixed to the mass,
and is in the c-frame. O2 and O3 are located at the center of the table. We can define our reference
frames so that b3 , c3 , and k are pointing in the same direction. Based on the reference frames
~ 02 = b3 =
~ 03 = b3 =
c3 . Note that is NOT equal to .
drawn above, 01
c3 and 02
~rM/01 = ~r02/01 + ~r03/02 + ~rM/03

(117)

Because 02 and 01 are located at the same point, we can write:


~rM/01 = ~r03/01 + ~rM/03
Then, find
frame.

~ 03 .
01

(118)

It will make this problem easier and allow us to ignore the second reference
~ 03
01

~ 02 + 02
~ 03 =
c3
= 01
c3 +

(119)

To find the reaction force, we need to first find the absolute acceleration and then apply F~ = m~a.

d2
~amabs = 2 ~rm/01
(120)
dt 01


d2
d2
~amabs = 2 ~r03/01 + 2 ~rM/03
(121)
dt 01
dt 01






d2
d2
d 01 ~ 03
d
~ 03 01
~ 03 ~rM
~
01
03
~amabs = 2 ~r03/01 + 2 ~rM/03 +
~rM/03 + ~rM/03 + 01
/03
dt 01
dt 03
dt 03
dt 03
(122)



d2
d2
d2
We know that dt
r03/01 , dt
rM/03 , and dt
rM/03 are zero. Removing the terms that go to
2 ~
2 ~
2 ~
01
03
03
0, we get:



d 01 ~ 03
~ 03 01
~ 03 ~rM
~amabs =
~rM/03 + 01
(123)
/03
dt
03


d 01 ~ 03
Next, we need to find dt
and ~r03/01 .
03
~ 03
01

d 01 ~ 03

dt 03

d 01 ~ 03

dt 03
~rM/03

c
=
c3 +
 3

d

c3 +
c3
dt

(124)

c3
=

(126)

= r
c1

(127)

(125)

03

29

d 01 ~ 03
dt 03

into our equation for ~amabs and find the absolute acceleration
Now we can plug ~rM/03 and
of mass m. Use the right hand rule to find that c3 c1 = c2 .

d 01 ~ 03
c2
~rM/03 = r
(128)
dt 03



   
~ 03 01
~ 03 ~rM
01

(129)
+ c3 r
c1
/03 =

~amabs


2

= r + c1

(130)


2

= r
c2 r + c1

(131)

Because m is moving with some velocity v, is related to v.


~v =
~ ~r
|~v | = r
v
=
r

(132)
(133)
(134)

This means = 0 when v is constant. In this problem, v is constant.




v 2
~amabs = 0 r +
c1
r

(135)

Now we can apply F~ = m~a. Note that the reaction force is the only force acting on this system.


v 2
~
c1
R = mr +
r
And now weve found the reaction force.

6.4

Rotating Reference Frames: Propeller Tip

Find ~vabs of a propeller tip.

30

(136)

Figure 11: Reference frames of airplane


We have 3 reference frames:
1. O1 inertial
2. O2 fixed to fuselage
3. (a)

~ 02
01

rotation of plane in the air

4. O3 fixed to propeller
5. (a)

~ 03
02

spins with respect to fuselage

Note that ~r02/01 is wrt O1 , ~rh/02 is wrt O2 , and ~rtip/h is wrt O3 .


Now we can find ~r.
~rtip = ~r02/01 + ~rh/02 + ~rtip/h vtipabs

(137)

We can take the derivative of this to get velocity:



d2
vtipabs = 2 ~rtip/01
(138)
dt 01



d2
d2
d2
= 2 ~r02/01 + 2 ~rh/02 + 2 ~rtip/h
(139)
dt 01
dt 01
dt 01


d2
d2
We need to write dt
r02/01 with respect to the O1 frame. dt
rh/02 needs to be written in
2 ~
2 ~
01
01
2

d
the O2 frame, and dt
rtip/h needs to be written in the O3 frame.
2 ~
01

31


 2
  2

d
d
d2
~
~
01
02
01
02
~rh + ~rh/02 +
~rtip + ~rtip/h
(140)
vtipabs = 2 ~r02/01 +
dt 01
dt2 02 /02
dt2 02 /h

d2
We still have to transform dt
rtip/h into the third reference frame.
2 ~
02


d2
d2
~ 03 ~r
~rtip =
~rtip + 02
(141)
tip/h
dt2 02 /h dt2 03 /h




d2
d2
The term dt2 ~rh/02 is 0 because the hub is fixed with respect to O0 . The term dt2 ~rtip/h is 0
02

because the tip is fixed with respect to the hub in O3 . Note that
~ 03 is the propeller with respect to the fuselage.
ground and 02
vtipabs
vtipabs

~ 02
01

03

is the plane with respect to



 
d2
~
~ 02 ~r
01
02
02 ~ 03 r
= 2 ~r02/01 +
~rh/02 + 01
tip/h + ~
tip/h )
dt 01



d2
~
~
~
01
02
01
02
02
03
+
~rtip/h
= 2 ~r02/01 + ~rh/02 +
dt

(142)
(143)

01

Were done!

6.5

Gyroscope

Lets practice Euler equations. Create a simulation for your gyroscope (axisymmetric top).
1. Use 3 (-precession) -1(-nutation) -3 (-spin) Euler angles and Euler rates.
2. Use Eulers equations to write three first order ODEs in terms of the angular velocity components in the body frame. Use the Euler rate equations to get three first order ODEs in
terms of the Euler angles.
3. For an axisymmetric body, the equations of motion can be simplified by taking (in this case),
= 0. Remember to take all derivatives before substituting = 0; otherwise, terms will be
lost. Identify those terms.
4. Create a simulation for your top. Estimate reasonable system parameters and initial conditions. Plot the Euler angles vs. time. Based on these plots, explain what the top is doing.
Validate your simulation by varying parameters and making comparisons to actual motion.

32

The principal axis are already defined in this problem (see diagram). One way to solve this is
by using moments of inertia to find
~ .


X
Mx = I11 bx I22 I33 by bz
(144)


X
My = I22 by I33 I11 bz bx
(145)


X
Mz = I33 bz I11 I22 bx by
(146)
We are treating the gyroscope as if =0. Then, in the z-direction is equal to 0, and the only
external moment, which well call mglI, is in the x-direction.

I33 
by bz mglI sin()
(147)
bx = 1
I11
I

33
by =
1 bz bx
(148)
I11
bz = 0
(149)
Then we can use the Euler rate equations.

sin()/sin()

=
cos()

cos()
sin()/sin()

cos()/sin()

sin()
cos() cos()/sin()


0 bx
0 by
1
bz

cos()
sin()
b +
b
=
sin() x
sin() y
= cos()b sin()b
x

cos() cos()
cos() sin()
=
bx
by + bz
sin()
sin()

(150)

(151)
(152)
(153)

Setting =0, we get:


= by / sin()
= b

(154)

= by / tan() + bz

(156)

(155)

,
and in terms of , and we have the
We can now put this all into MATLAB. We have ,
derivative of . We can say ZZ(1) = , ZZ(2) = , ZZ(3) = , ZZ(4) = bx , ZZ(5) = by , and
ZZ(6) = bz . Lets use z to represent the derivative of ZZ. We can write the following code:
f u n c t i o n s t a t e s = r o t a t i n g t o p f u n (T, ZZ)
% ZZ ( 1 ) = phi , ZZ ( 2 ) = t h e t a , ZZ ( 3 ) = p s i ,
% ZZ ( 4 ) = omega bx , ZZ ( 5 ) = omega by , ZZ ( 6 ) = omega bz
z1 = ZZ ( 5 ) / s i n (ZZ ( 2 ) ) ;
z2 = ZZ ( 4 ) ;
z3 = ZZ ( 5 ) / tan (ZZ ( 2 ) ) + ZZ ( 6 ) ;
z4 = (1 I 3 / I 1 ) ZZ ( 5 ) ZZ ( 6 ) mglI s i n (ZZ ( 2 ) ) ;
z5 = ( I 3 / I1 1)ZZ ( 6 ) ZZ ( 4 ) ;
33

z6 = 0 ;
s t a t e s = [ z1 ; z2 ; z3 ; z4 ; z5 ; z6 ] ;
end
Then, we have to define our parameters and initial conditions. You can choose any initial
conditions that you would like.
% s p e c i f y mass moment o f i n e r t i a components
I1 = 2 . 5 ;
I3 = 1 . 0 ;
% d e f i n e mass , l e n g t h o f g y r o s c o p e , g r a v i t y ,
% and moment o f i n e r t i a
m = 0.01;
l = 0.02;
g = 9 . 8 1 e 2; % a c c e l o f g r a v i t y i n cm/ s 2
% u s e massmoment o f i n e r t i a handout
% t o c a l c u l a t e t h e q u a n t i t y below
mglI = 1 9 6 / 2 . 5 2 ;
% set i n i t i a l conditions for state variables
% ZZ ( 1 ) = phi , ZZ ( 2 ) = t h e t a , ZZ ( 3 ) = p s i ,
% ZZ ( 4 ) = omega bx , ZZ ( 5 ) = omega by , ZZ ( 6 ) = omega bz
phi0
= 0 pi /180;
phidot0
= 3;
theta0
= 30 p i / 1 8 0 ;
% t h e t a MUST NOT = 0
thetadot0 = 0 . 0 ;
% t h e g y r o s c o p e i s a x i s s y m e t r i c ; f o r a x i s symmetric p s i = 0
psi0
= 0.;
psidot0
= 100;
We must define our initial as well. We can use the 3-1-3
for .

b1
sin() sin() cos()
b2 = sin() cos() sin()
b3
cos()
0

Euler rate equations again to solve


0

1

(157)

~ b1 = sin() sin() + cos() + 0

~ b = sin() cos() sin() + 0

(158)

~ b3 = cos() +

(160)

(159)

We are treating the gyroscope as if =0. Then,

~ b1 =

~ b = sin()

(161)

~ b3 = cos() +

(163)

34

(162)

Therefore, we can write the following matrix to describe :


omegab0 = ( [ 0 1 0 ; s i n ( t h e t a 0 ) 0 0 ; c o s ( t h e t a 0 ) 0 1 ] . . .
[ phidot0 ; thetadot0 ; psidot0 ] )
We can now call the ODE function.
Z 0 = [ phi0 , t h e t a 0 p s i 0 , omegab0 ( 1 ) , omegab0 ( 2 ) , omegab0 ( 3 ) ]
t span = [ 0 : 3 0 ] ;
% max time span f o r s i m u l a t i o n
r e l t o l =10e 6;
o p t i o n s= o d e s e t ( RelTol , r e l t o l ) ;
[ t , z o u t ] = ode113 ( @ r o t a t i n g t o p f u n , t s p a n , Z 0 , o p t i o n s ) ;
Weve now written code that will model a gyroscope! All you have left to do is plot it.

7
7.1

Code for In-Class Problems


Projectile Motion

Code provided by Nick Eyre.


% Baseball Simulation
% Nick Eyre, September 2014
%
% This file can be used as a template for starting simulations with ODE45
function baseball
close all % Close all figures
% Parameters
%
(Any Physical Constants)
g = [0; -9.81]; % m/s^2
Cd = 0.3;
rho = 1.3; %kg/m3
Area = 4.2e-3; %m2
m = 0.145; %kg
% State Variables: (Always explicity state your state variables in order
% for ease of reading)
%
[x, y, x velocity, y velocity]
% Initial Conditions
X0 = [0 1 45*cosd(35) 45*sind(35)];
% Time Span (seconds)
Tspan = [0 40];
% Setup Events Function to End Simulation When Ball Hits Ground
options = odeset(Events, @events_function);
35

% Run ODE Simulation and Output Time and States


[T, X] = ode45(@derivatives_function, Tspan, X0, options);
% Plot Trajectory (X vs Y)
plot(X(:,1), X(:, 2))
% Grab first and second rows from the output states matrix
xlabel(Position X (m))
ylabel(Position Y (m))
title(Trajectory)
print -depsc trajectory.eps % Save as EPS Image to trajectory.eps
% Energy Calculations
KE = [];
PE = [];
for n=1:length(T)
PE(n,1) = -m*g(2) * X(n,2); % Potential Energy = mgh
KE(n,1) = .5*m*norm(X(n,3:4))^2; % Kinetic Energy = 1/2 * m*v^2
end
TE = PE + KE;
% Total Energy
% Plot Energy
figure % Create a new Figure
hold all % Display Multiple Lines in Different Colors
plot(T,PE,k,LineWidth,5);
plot(T,KE,:);
plot(T,TE);
legend(Potential, Kinetic, Total);
xlabel(Time)
ylabel(Energy (J))
title(Energy)
print -depsc energy.eps % Save as EPS Image to energy.eps
% Calculate Derivatives of a given state. Called by ODE45
% Inputs:
%
X is the current state [x y vx vy]
%
T is the current time
% Outputs:
%
Derivatives contains derivatives of state variables [dx/dt dy/dt dvx/dt dvy/dt]
function derivatives = derivatives_function(T, X)
% Unpack into Vectors
%
Note: this allows you to do vector math
R = X(1:2);
V = X(3:4);
% Vector Math to Calculate Derivatives in vector form
dRdt = V;
dVdt = g - .5*rho*Area*Cd*norm(V)*V;

36

% Combine Vectors and Output Derivatives


derivatives = [dRdt; dVdt];
end
% Function to look for points of interest and stop the simulation when appropriate
% Inputs:
%
T: current time
%
X: current state [x y vx vy]
% Outputs:
%
eventvalue: value that we care if hits 0
%
stopcalculation: 1 if you want to stop the simulation when it hits
%
this event
%
direction: 0 if you dont care which way it crosses 0 through, -1
%
or +1 if you do care
function [eventvalue, stopcalculation, direction] = events_function(T, X)
eventvalue = X(2);
stopcalculation = 1;
direction = 0;
end
end

7.2

Gyroscope

Code provided by Chris Lee.


function rotating_top_separate
clear all
close all
clear functions
% specify mass moment of inertia components
I1
= 2.5;
I3
= 1.0;
%
m = 0.01;
l = 0.02;
g = 9.81e-2; % accel of gravity in cm/s^2
mglI = 196/2.52;
% state variables
% ZZ(1) = phi, ZZ(2) = theta, ZZ(3) = psi,
% ZZ(4) = omega_bx, ZZ(5) = omega_by, ZZ(6) = omega_bz
phi0
= 0 *pi/180;
phidot0
= 3;
theta0
= 30 *pi/180;
%** theta must not = 0
thetadot0 = 0.0;
psi0
= 0.;
%** for axissymmetric psi = 0
psidot0
= 100;
37

%
%
%

r313= [cos(phi0) sin(phi0) 0; ...


-cos(theta0)*sin(phi0) cos(theta0)*cos(phi0) sin(theta0); ...
sin(theta0)*sin(phi0) -cos(phi0)*sin(theta0)
cos(theta0) ];

omegab0 = ( [0 1 0; sin(theta0) 0 0; cos(theta0) 0 1]*[ phidot0; thetadot0; psidot0])


%
Z_0 = [phi0, theta0 psi0, omegab0(1), omegab0(2), omegab0(3)];
% Define simulation parameters
t_span = [0:30];
% max time span for simulation
%
reltol=10e-6;
options= odeset(RelTol, reltol);
[t, zout] = ode113(@rotating_top_fun, t_span, Z_0, options);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
subplot(3,1,1)
plot( t, zout(:,1)*180/pi )
%plot angles in degrees
ylabel(\phi (degrees))
title(3-1-3 Euler Angles: separate eoms)
subplot(3,1,2)
plot( t, zout(:,2)*180/pi, r ) %plot angles in degrees
ylabel(\theta (degrees))
subplot(3,1,3)
plot( t, zout(:,3)*180/pi, r ) %plot angles in degrees
ylabel(\psi (degrees))
xlabel(Time (sec))
figure
subplot(3,1,1)
plot( t, zout(:,4)*180/pi )
ylabel(\phi dot (degrees/s))
subplot(3,1,2)
plot( t, zout(:,5)*180/pi, r )
ylabel(\theta dot (degrees/s))
subplot(3,1,3)
plot( t, zout(:,6)*180/pi, r )
ylabel(\psi dot (degrees/s))

%plot angles in degrees

%plot angles in degrees

%plot angles in degrees

xlabel(Time (sec))
title(3-1-3 Euler Rates)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function definition in first order form
function states = rotating_top_fun(T, ZZ)
38

% ZZ(1) = phi, ZZ(2) = theta, ZZ(3) = psi,


% ZZ(4) = omega_bx, ZZ(5) = omega_by, ZZ(6) = omega_bz
z1 = ZZ(5)/sin(ZZ(2));
z2 = ZZ(4);
z3 = -ZZ(5)/tan(ZZ(2)) + ZZ(6);
z4 = (1-I3/I1)*ZZ(5)*ZZ(6)
- mglI*sin(ZZ(2));
z5 = (I3/I1-1)*ZZ(6)*ZZ(4) ;
z6 = 0;
%
states = [z1;z2;z3;z4;z5;z6];
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

39

You might also like