Professional Documents
Culture Documents
php
Chapter 3
Nonlinear Differential
Equations
We have already seen some nonlinear differential equations (for example, the clock and the
Foucault pendulum dynamics in Chapter 1). In this chapter, we will delve into some of the
more interesting aspects of nonlinear equations, including a simple example of chaos, the
dynamics of a rotating body, and the modeling of a satellite in orbit. We will also explore
an interesting way of modeling motions in three dimensions using a four-dimensional vec-
tor called a quaternion. This chapter illustrates some advanced Simulink modeling and a
major feature of Simulink, namely the ability to create reusable models. For the beginning
Simulink user these models might not be too useful, but users that need to model complex
mechanical systems will find a great many uses for many of the examples presented here
(and therefore could cut and paste the models in the NCS library directly into the new models
that they might want to build).
In addition, we illustrate the following:
• how to make subsystem models that can be stored in a library for later use;
• how to annotate a model so the simulated equations are written out in the annotation;
• how to create a subsystem and then layer a mask on top of it, the mask providing
the user with a dialog that allows parameters in the model to be transferred into the
model (just as a subroutine’s parameters are transferred into the subroutine during
execution).
81
82 Chapter 3. Nonlinear Differential Equations
convection of the atmospheric flow (we call it y1 ), the second (y2 ) related to the horizontal
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
temperature of the atmosphere, and a last (y3 ) related to the vertical temperature of the
atmosphere.
The Lorenz differential equation has three parameters, σ, ρ, and β; the most popular
values for these parameters are 10, 28, and 8/3, respectively. The differential equation is
three coupled first order differential equations given by
ẏ1 = −βy1 + y2 y3 ,
ẏ2 = −σy2 + σy3 ,
ẏ3 = −y2 y1 + ρy2 − y3 .
We will not duplicate the analysis from Moler’s book; suffice it to say that the equation has
two “fixed points” where, if the equation has an initial condition at t = 0 equal to one of
these solutions, the derivatives would all be zero and the solution would stay at these points
(hence the name fixed points). However, both of these points are unstable in the sense that
any small perturbation away from them will cause the solution to rapidly move away and
never return. (If the solution gets close again to one of these points, it will rapidly move
away again.) The two points are determined from
ρ−1 ρ−1
√ √
y = β(ρ − 1) and y = − β(ρ − 1) .
√ √
β(ρ − 1) − β(ρ − 1)
Figure 3.1. Simulink model and results for the Lorenz attractor.
The two x-y plots show the two fixed points for the Lorenz system. To change the
values of Rho, Sigma, and Beta, simply type in new values at the MATLAB command line
(see [29] for some suggestions).
Lorenz Attractor
El em ent 1 of the ma trix Ay
y1
Beta
y2
y2 *y 3 Simulink model for the Lorenz Attractor, whose
1 nonlinear diff ernetial equation is give n by:
y1
y3 Nonlin earity: xo s y1
Prod of dy/dt = A y
y2 and y3 Rho-1+10*rand n
where: A =
El em ent 2 of the ma trix Ay
| -β 0 y |
2
y2
| 0 -σ σ |
| - y ρ -1 |
2
y3 Si gm a 1
y2
xo s y2
.
sqrt(Beta*(Rho-1))+20*randn Pulse
Generator
Figure 3.2. Exploring the Lorenz attractor using the linearization tool from the
control toolbox.
operating points and at the same time understand why the Lorenz attractor response is
chaotic. A small MATLAB program, called Lorenz_eigs.m in the NCS library, plots the
first 40 eigenvalues (corresponding to the first 0.4 sec of the time history). The code in this
M-file is
axis([-25 15 -25 25]);
axis(’square’)
grid
hold on
nlins = tstop/0.01
for i = 1:nlins/50
Lorenz(i).eig=eig(Lorenz_2_Trigger_Based_Linearization_(i).a);
plot(real(Lorenz(i).eig),imag(Lorenz(i).eig),’.’)
drawnow
end
hold off
Figure 3.3 shows the eigenvalue plot. Let us compare this plot with the solutions
generated when the simulation ran. The plot below the eigenvalue figure is the simulation
results for the state variable y1 plotted against the state variable y3 . The initial position in
this plane was about (10, 20).
3.1. The Lorenz Attractor 85
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
20
t=0
Imaginary part of the Eigenvalues
10
t=0
-10
t=0
-20
-30
-20 -15 -10 -5 0 5 10
Real part of the Eigenvalues
stable. The complex pair indicates that the solutions are oscillatory, but since they are in the
left half plane, it also indicates that the amplitude is decreasing, so the solution is spiraling
in toward the attractor at the bottom of the time-history plot. However, as the solution
approaches this attractor, the complex pair of eigenvalues again moves into the right half
plane (the eigenvalue plot ends before this happens), indicating that the solution is again
locally unstable, and it begins to diverge away from the attractor. This behavior is consistent
with the fact that the attractors
√ are both unstable. (As an exercise, set the value of y2 in the
A matrix to the values ± β(ρ − 1), and compute the eigenvalues of the matrix.)
It is quite instructive to play with this model and compare the eigenvalues with the
solutions. Some of the questions you might want to ask: Do the three eigenvalues always
appear as one complex pair and one real? What is the importance of the magnitude of the
real part of the complex pair? What is the importance of the imaginary part of the complex
pair?
This technique of analyzing a system by linearization about the current operating point
is very useful as systems get complex. This approach is particularly useful when systems
are so large that creating linear models analytically is virtually impossible.
Table 3.1. Computation time for the Lorenz model with different solvers and tolerances.
model from the MATLAB command line. Type in the following code at the MATLAB
command line and then use the arrow keys to rerun the same code segment:
tic;sim(’Lorenz_2’);te = toc;
te
In this set of MATLAB instructions, the sim command causes MATLAB to run the
Simulink model (the argument of the command tells MATLAB what model to run—it is a
string). The value of the elapsed time for the simulation is te. In between each execution
of the simulation using the sim command, open the Configuration Parameters dialog in
Simulink; use the Simulation pull-down menu or click on the model window and type
Ctrl + e. Then change, in turn, the solver or the relative tolerances, and—this is very
important—save the model and re-execute the commands above. Table 3.1 shows the result
of my experiment; I used a Dell dual Pentium 3.192 GHz computer with 1 gigabyte of RAM.
With the understanding that you have obtained from what we have done so far in this
book, and the discussion in NCM, it should be clear that there are no hard and fast rules
that apply to selecting a solver, a tolerance, or any of the other parameters in the Simulink
Configuration Parameters pull-down dialog. The best way to understand the simulation
accuracy for any model you will produce is to try different solvers with different tolerances.
If you think that the solvers are jumping through time steps that are too big, you can set the
maximum step size, and if you think that the solver is taking too long, you can try increasing
the minimum step size. In addition, if you are interested in the solution of a system that has
some dynamics that change very fast, but you do not really care about the details of these
fast states, then the “stiff” solvers are best (see Section 7.9 of NCM). We will see some
examples in later chapters that require stiff solvers, and when we do, we will be able to
explain why they are used and how to use them.
the input data, simplifies the tables. The “PreLookup Index Search” block that goes along
with this block is also in the library, but since this block works in conjunction with the
PreLookup block, it is not a stand-alone block. The last block in the library is the “Lookup
Table Dynamic” block. We will investigate each of these block types and illustrate when to
use them in the following examples.
How do you insert this temperature variation into the model? Open the model by
typing Thermo_NCS at the MATLAB command line. Find the blocks with the constant 50
and the sinusoid that models the external temperature variation along with the summation
block that adds the two, and remove them from the model. Open the Simulink library
browser and find the Lookup Tables library. From this library, select the block called
“Lookup Table” and insert it into the model. The input to this block will be the simulation
time that is the “Clock” block in the Simulink Sources library. Get one of these blocks
and insert it into the model so that it is the input to the lookup table. Then, double click
on the Lookup Table block and enter the values of the data using the MATLAB commands
above. The model that results is in Figure 3.5, and the Lookup Table dialog should look
like Figure 3.6.
The result will now show the home heating system operating with the outdoor temper-
ature given by the tabular values in the lookup table. The figure also shows the dialog that
opens when you double click the Lookup Table block. It allows editing of the data (using
the “Edit” button). You can also enter the data as a MATLAB vector using MATLAB’s
vector notation or pasting a vector of numbers into the dialog box. If, for example, the data
is in an Excel spread sheet, it can be pasted into the vector of output values window and
then surrounded by the square brackets “[” and “]” to make the data into a MATLAB vector.
Notice also that the dialog box allows you to select the interpolation method you want
to use. The default is interpolation and extrapolation. This means that the data interpolation
is linear between time values, and the last two data points in the table project the data beyond
the final time. The other options are interpolation with end values used past the last data
point in the table, the value nearest to the input value, or the values below or above the
input value. The block will also provide a value at fixed times by setting the sample time.
In the simulation the value of this is set to −1, which causes the value to be computed at
3.3. Tables, Interpolation, and Curve Fitting in Simulink 89
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Figure 3.4. Pseudotabulated data for the external temperature in the home heating
model.
deg F to Blower
deg C Heat Output
blower
deg C to
Terr cmd
70 F2C Mdot*ha deg F
Tset
Set Point Indoor v s. Plots
C2F Outdoor Temp.
Thermostat Thouse
F2C
Clock deg F
Lookup Table
for Outdoor Temp. to deg C House
Figure 3.5. Home heating system Simulink model with tabulated outside temper-
ature added.
90 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
every simulation step when the input changes. (Since the input to the block is the clock,
this is every integration step used by the solver.) The Help button on the dialog opens the
MATLAB help and provides a very good description of how this block works.
If you have not created the model yourself during the above discussion, it is available
in the NCS library using the command Thermo_table.
The 2-D Lookup Table block computes an approximation to z = f (x, y) given some
x, y, and z data points. The dialog block for this lookup table has an edit box that contains
the “Row index input values” (a 1 × m vector corresponding to the x data points) and an
edit box with the “Column index input values” (a 1 × n vector of y data points). The values
for z are in the edit box labeled “Matrix of output values” (an m × n matrix).
Both the row and column vectors must be monotonically increasing. These vectors
must be strictly monotonically increasing in some specific cases. (See the Help dialog for
more details on what these cases are.)
By default, the output is determined from the input values using interpolation-
extrapolation—that is, linear interpolation and extrapolation of the inputs past the ends.
The alternatives available are “Interpolation-Use End Values,” “Use Input Nearest,” “Input
Below,” or “Input Above,” with the same meaning as for the 1-D Lookup Table.
It is important to use the proper end extrapolation method when the simulation could
cause the input to the lookup table to be outside the range of the tabulated values. For
example, the heating simulation is set up to run for 2 days, which is also the extent of the
time data loaded into the lookup table dialog. The selected extrapolation was “Interpolation-
Extrapolation,” so running the simulation linearly extrapolates past 48 hours, using the last
two data points in the table. This means that the temperature will continue to drop, going
down at the rate of about 10 deg per hour (240 deg per day), which is clearly not correct.
Even selecting the option of keeping the end-point values constant (“Interpolation-Use End
Values”) is not a good approximation to what actually happens, but it is the better choice. In
this case the user has clear control (via the simulation time) over whether or not the lookup
table data will be exceeded; in many applications the independent variable in the table is
a simulation variable whose value cannot be easily determined. In this case, a plot of the
simulation results and the independent variable will allow you to verify that the simulation
handles the end-point properly.
3.3. Tables, Interpolation, and Curve Fitting in Simulink 91
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
The 2-D and n-D Lookup Tables are set up in a very similar way to the 1-D table we
just reviewed. To ensure that you understand the way these are used, use the Simulink help
(from the 2-D or n-D block dialog) to review the way these tables are used.
When the plot appears, select “Basic Fitting” under the “Tools” menu to open the Basic
Fitting dialog shown above. In this dialog, we selected the sixth order polynomial option.
The tool will overplot the curve fit on the graph of the data. Try different polynomial degree
options and navigate around the dialog. To view the polynomial coefficients as shown in
the figure, click the right arrow at the bottom of the dialog. To get these values into the
92 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
28
Dry and Wet Bulb Temperature
27
Readings (Deg. F)
26
25
24
23
22
21
20
0 5 10 15 20 25
Time (Hours from Midnight)
Figure 3.7. Real data must be in a MATLAB array for use in the “From Workspace”
block.
Simulink polynomial block, click the button in the Basic Fitting tool that says “Save to
workspace.” MATLAB will save the curve fit in a MATLAB structure called “fit.” The
coefficients are in the field called coeff (i.e., the coefficients are the MATLAB variable
fit.coeff). In the Thermo_table model in the NCS library, replace the Lookup Table block
with the Polynomial block, and then open this blocks dialog and type “fit.coeff” in the area
designated “Polynomial coefficients.” Run the simulation and verify that the simulation is
using the curve fit you created instead of the data. (A version of this model is in the NCS
library, but you still need to create the polynomial fit as described above before you use it;
the model is called Thermo_polynomial.)
The reason you might want to use this approach is to smooth the data and to improve
the simulation run time. In general, it is faster to use a low order fit than it is to use the table
lookup with interpolation. (It is not always the case that curve fit calculations are faster, so
if improving the simulation time is your goal this assumption should be verified.)
3.3.3 Using Real Data in the Model: From Workspace and File
A web site gives access to weather data from volunteers in your local area. We used
data for Norfolk, Massachusetts on January 1, 2006, to create an M-file that can then
import the actual temperature data into the home heating model. This data is in the M-file
weather_data.m, and Figure 3.7 is its plot. The web site that the data comes from is
http://www.wunderground.com/weatherstation.
3.3. Tables, Interpolation, and Curve Fitting in Simulink 93
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Thouse
deg F
to deg C
blower Thermo
Terr cmd Indoor v s. Plots
70 F2C Mdot*ha
Outdoor Temp.
Set C2F
Heater
Point (F) Thermostat
Blower deg C to
House deg F
Tdata(:,1:2) F2C Measured Outdoor
Temperature
Measured Data deg F
From to deg C
Workspace
Figure 3.8. Simulink model of the home heating system using measured outdoor
temperatures.
Figure 3.9. From workspace dialog block that uses the measured outdoor temper-
ature array.
Running the M-file creates the plot in Figure 3.7 and the data in the MATLAB
workspace. The data is stored in a 288 × 3 array called Tdata, where the first column
is the time (in hours), the second is the measured dry bulb temperature, and the last column
is the wet bulb temperature (both in degrees F).
To use data in MATLAB as an input to a Simulink model, the From Workspace block
is used. (An equivalent block called From File gets the data from matrices that were saved
using a mat file; since the two blocks work in the same way, we will illustrate only the
Workspace block.) We have modified the Thermo_table model to allow input to the
model to come from a data file in MATLAB. This is Thermo_real_data in the NCS
library (Figure 3.8).
The data we want is in the array called Tdata, where the first column is the time and the
second is the outside temperature. The From Workspace block dialog in Figure 3.9 opens
when you double click on the block. We have entered the values for the data into this dialog
94 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Indoor vs.
Outdoor Temp.
74
72
70
68
66
0 1 2 3 4 5 6 7 8 9
4
x 10
Measured Temperature
30
28
26
24
22
0 1 2 3 4 5 6 7 8 9
Time (Sec.) 4
x 10
Figure 3.10. Using real data from measurements in the Simulink model.
(calling the data “Tdata”). Since the array had three columns (the last column is the wet
bulb temperature, which we do not need), the dialog box Data has Tdata(:,1:2). This
truncates the tabulated data down to two columns. The sample time is every 5 minutes, or
every 300 sec. For completeness, we show the result of simulating the data in Figure 3.10).
y New
y
x New
For a given value of the angle θ, the matrix T is orthogonal. (As an exercise, prove this by
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Notice that the transformation is always a 2×2 matrix, but when the axis of rotation changes
the 2 × 2 matrix appears in different rows and columns of the full 3 × 3-rotation matrix.
The three transformation matrices can be multiplied together to give a single direction
cosine matrix for the entire rotation, but in most applications this is not done. Note that
since each of the transformations by themselves is orthogonal, the combined direction cosine
matrix product is also orthogonal. (The proof of this is an exercise.)
When an object is rotating, each axis of the body has its own rotational rate. It is usual
to denote these rotations with the letters p, q, and r for the x-, y-, and z-axes attached to
the body, respectively. When this is the case, the Euler angles are functions of time, so we
need to know what happens to them as the object rotates.
We saw in Section 1.4.1 of Chapter 1 that a scalar rotation around a single axis gives
rise to rotations in the two coordinates that are orthogonal to the rotation axis. Generalizing
this to three dimensions gives the operator equation (where a is an arbitrary vector in the
“Body” frame that is rotating at the vector rate ω)
da da
= + ω × a.
dt Inertial dt Body
A rotating body has accelerations induced whenever the angular momentum vector changes.
If we let the rotational rates around the body axes be
p
ω = q ,
r
the angular momentum of the body around its center of mass is the result of calculating the
angular momentum of every mass particle in the rigid body and then summing over all of
the particles. If the rigid body is homogeneous, the summation is an integral. In either case,
the result is that the angular momentum is Jω, where the inertia matrix is
Jxx −Jxy −Jxz
J = −Jyx Jyy −Jyz .
−Jzx −Jzy Jzz
(Since this result might be unfamiliar, see [17] for more details.)
From Newton’s laws applied to rotations, the torques applied to the body and the rate
of change of the angular momentum balance, so (using the derivative of a vector in inertial
3.4. Rotations in Three Dimensions 97
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
coordinates above)
dJω dω dJ
=J + ω + ω × Jω = T.
dt dt dt
When the inertia matrix is constant over time, J factors to give
d
(ω) = J−1 (T − ω × (Jω)) .
dt
This equation is the starting point for any simulation that involves the rotation of a rigid
body. (We will build a simulation model in Simulink shortly.) If this equation is integrated,
the result is the angular velocity at any time t. Integrating the angular velocity gives the
angular position. The angular position that results from integrating the angular rate is not a
record of the Euler angle history. This is because the angular rate ω is always with respect
to the body, and the Euler angles are always with respect to the original orientation of the
body.
Using the transformation from the body axes to the Euler axes (and the Euler rotations
about x-, y-, and the new x-axes) developed above, the instantaneous angular rate in the
body coordinates (with respect to the Euler angle rates) is
dϕ 0
p 1 0 0 dθ
q = dt + 0 cos ϕ sin ϕ
0 dt
r 0 − sin ϕ cos ϕ
0 0
0
1 0 0 cos θ 0 − sin θ
+ 0 cos ϕ sin ϕ 0 1 0 0 .
dψ
0 − sin ϕ cos ϕ sin θ 0 cos θ
dt
Multiplying the matrices in this expression gives
p 1 0 − sin θ φ
q = 0 cos φ d
sin φ cos θ θ .
r 0 − sin φ cos φ cos θ dt ψ
The Euler rates in terms of the body rates come from inverting the matrix in this expression
as follows (noting that this matrix is not orthogonal; see Exercise 3.1):
−1
φ 1 0 − sin θ p
d θ = 0 cos φ sin φ cos θ q
dt
ψ 0 − sin φ cos φ cos θ r
1 sin φ tan θ cos φ tan θ
0 p
cos φ − sin φ
=
q .
sin φ cos φ r
0
cos θ cos θ
Now there are some interesting and potentially difficult numerical issues associated with this
equation. Whenever the Euler angle θ becomes a multiple of π2 , this matrix becomes singular
98 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
(its inverse becomes infinite), so keeping track of the orientation using this representation can
have numeric problems whenever the Euler angles are near 90 degrees or 270 degrees. Even
if there were a nice way of overcoming the numerical issues, the fact that the transformation
matrix is not orthogonal would create numerical stability issues. What can we do about this?
As we pointed out when we were talking about the Euler angles, this matrix is the “direction
cosine matrix,” and it defines the cosines of the angles between the new axis (after the
rotation) and the old (before the rotation). Thus the 1,1 element of the matrix is the cosine
of the angle between the x-axis after rotation and the x-axis before rotation, the 1,2 element
is the cosine of the angle between the x-axis after and the y-axis before, etc. If we denote
this matrix by C, then when the object is rotating about its body axes with rates p, q, and
r, the direction cosine matrix satisfies the differential equation
0 r −q 0 −r q
dC
= −r 0 p C = − r 0 −p C = −$C.
dt q −p 0 −q p 0
This equation comes from the fact that the rate of angular rotation is the cross product of
the vector rate with the body axis vector. (In Exercise 3.2 you will show that the matrix
0 −r q
= r 0 −p ,
−q p 0
multiplying, on the left, any column ci of C, gives a result that is the same as ω × ci .)
This equation can be the calculation used to find the orientation of an object as it rotates,
but because there are nine elements in C and there are only six independent variables, we
would be calculating nine values when only six are required. For this reason, direction
cosine formulations are not preferred for calculating rotations. The next section describes
the preferred method. It is instructive to build a Simulink model that uses this equation to
keep track of the orientation of an object and to compare the computation time required
with that required using the quaternion formulation (see Exercise 3.3).
3.4. Rotations in Three Dimensions 99
Euler also proved that any three-dimensional rotation can be represented using a single
rotation about some axis (Euler’s theorem). The “axis-angle” form of a rotation uses this
observation. For now let us not worry about how we determine the axis; assume that the
vector n specifies it. The desired rotation is defined to be (using the right-hand rule) a
rotation about this axis by some angle. The vector n is a unit vector (i.e., nT n = 1).
Most of the blockset tools in Simulink (the Aerospace Block Set and SimMechanics)
do not make direct use of the axis-angle form, but this representation is the starting point
for deriving other forms. The axis-angle approach forms the basis of the quaternion repre-
sentation that is finding more applications in many diverse fields (for example, mechanical
applications, computer-aided design, and robotics).
We need four pieces of information to describe the axis-angle transformation, so we
T
denote it by the four-dimensional vector [ n θ]T = nx ny nz θ . However,
because nT n = 1 (n specifying direction of the axis that we are rotating about, not its
length), only three of these four numbers are independent. When there is a rotation about
the body axis, the rotational rate around the axis n is
dn
= nxω = −ωxn.
dt
Because of the sin(θ/2) multiplying the vector n in this definition, the rotational rate
of q1 (first three components of the quaternion) is
dq1 1
= q1 × ω.
dt 2
By using sin and cos of the rotation angle instead of the angle itself (as was done in
the axis-angle representation), the quaternion is numerically easier to manipulate. In fact,
T
the norm of q is qT q = q1 s q1 s = q1 q1T + s 2 = 1. (As an exercise, show
that this is true.) Since q must be a unit vector, we can continuously normalize it as we
compute it (which ensures an accurate representation of the axis of rotation).
The other attribute of the quaternion representation that makes it more robust nu-
merically is the way the body rates determine the quaternion rates. The derivative of the
100 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Scalar multiplication
s omega / ||q||
3omega
1
3
omega_b2i 3
4 s/||q||
Select s from q
||q||
(q_1 cross omega) / ||q||
3 3
4 3 q_1/||q||
3 Cross 3
Select q_1 from q omega Product
Normalize the
3 3
quaternion[q_1 s]
Initial_q
Let the quaternion q (a 4 vector) be partitioned into a 3 vector and a scalar as follows:
T
q = [ q1 s ]
The differential equations for the partitioned components of the quaternion vector are:
dq
/dt = 1/2 [ q 1 X ω + s ω ]
1
and
ds T
/dt = -1/2 ω q 1
To insure that the quaternion vector has a unit norm, ||q|| is calculated at each
step and the derivatives are divided by this norm before they are integrated (insuring that the
vector has unit norm at the time t, just prior to the next integration step).
Figure 3.12. Using quaternions to compute the attitude from three simultaneous
body axis rotations.
dq d d sin(θ/2) + cos(θ/2)n
=
q1
=
n sin(θ/2)
= dt 2 dt
dt dt s dt cos(θ/2) 1 dθ
− sin(θ/2)
2 dt
1
q1 and second to extract s. The other blocks that are used are the dot product and cross
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
product blocks in the Math Operations library. We need these because of the cross product
in dq
dt
and the calculation of ωT q1 .
The cross product block in Simulink
implements the definition of the cross prod-
uct using a masked subsystem. Since we 1
From the definition of the quaternion, the Euler angles are determined from the fol-
lowing (where the components of the vector q1 are denoted q1 , q2 , and q3 ):
2(q2 q3 + sq1 )
ϕ = tan−1 ,
s − q12 − q22 + q32
2
A Simulink block to do these transformations is in Figure 3.13 (this is the model Quater-
nion2DCM in the NCS library). Notice that this model implements the direction cosine
matrix from the following definition:
2 0 −q3 q2
DCM = s − |q1 |2 I3x3 + 2q1 q1T − 2s q3 0 −q1 .
−q2 q1 0
(In Exercise 3.5, you are to verify that this calculation gives the same direction cosine matrix
as that shown above.)
When this discussion started, we noted that the rotational acceleration comes from
the Euler equation
d
(ω) = J−1 (T − ωx(Jω)) .
dt
We have assumed that the inertia matrix is constant; otherwise, its derivative needs to
be included since the left-hand side of this equation is really the derivative of the total
momentum given by Jω.
We combine the body-axis angular acceleration term above with the quaternion calcu-
lation shown in Figure 3.12 to give the Simulink model shown in Figure 3.14. (This model,
called Quaternion_acceleration, is in the NCS library.) Also, note that the block im-
plements a time varying inertia matrix using the input port number 2 for the derivative of
the inertia; you can either remove the port if the inertia is constant or put the 3 × 3 zero
matrix as the input.
Once again, some new Simulink blocks are in this model. The multiplication block
in the Math Operators library computes the inverse of the matrix J . To do this, drag the
multiplication block into the model and then double click to open its block dialog. In the
dialog, change the multiplication type to Matrix (*) in the pull-down menu, and then put the
symbols “/*” in the “Number of inputs” box. The icon will change to the matrix multiply, and
“Inv” will denote the top input (because of the division symbol you entered in the dialog).
3.4. Rotations in Three Dimensions 103
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Matrix
uT Multiply
1
1 em
Direction
Quaternion Cosine
Vector Matrix
square s eye(3) 3x3 Identity Multiply by q4
U1 -> Y
Y
U2 -> Y(E)
-1
Select q3
U1 -> Y
-1 Y
U2 -> Y(E)
Select q2
U1 -> Y
U2 -> Y(E) Y
-1
Select q1
2 2 T
[A(q)] = ( s -- |q| ) I + 2 q q -- 2 s Q
in Yellow in Cyan in Blue
| 0 --q 3 q2 |
Where Q = | q3 0 --q 1 |
| --q 2 q1 0 |
We have already encountered the text block with TeX when we explored the Lorenz
attractor in Section 3.1. This model uses this feature, and it is a good habit to do so. Locate
the text block in the Simulink model at any desired location by double clicking at the
desired location. This approach should always be used to annotate the model by showing
the equations that the model implements. If you open the model later, the combination of
the Simulink model and the equations make it very easy to understand and reuse the model.
After you double click on a location in the model for the equations, simply type the text. You
have control over the font selected, the font size, the font style, the alignment of the font, and
finally the ability to turn on TeX commands in the block. These are selectable (after the text
is typed) using the “Format” menu in the Simulink window. For the TeX commands, the
options are the subset of TeX that MATLAB recognizes. Thus, for example, the annotation
of the equation at the bottom of the quaternion model in Figure 3.12 above uses the following
text (the text appears centered in the block because the alignment is set to “center”):
104 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Cross
1 Product omega
Matrix
J cross H
Multiply J omega
( angular momentum H)
5
q Cross
Normalize the Product
x ||x||
quaternion[q s] 1
Magnitude of q1 q cross omega1
0.5 2
dq/dt
-1
q (dot) omega1
To insure that the quaternion vector has a unit norm,||q|| is calculated at each
step and the derivatives are divided by this norm before they are integrated (insuring that the
vector has unit norm at the time t, just prior to the next integration step).
Figure 3.14. Combining the quaternion block with the body rotational acceleration.
The command \bf{ } causes the text inside the braces to appear in boldface. The
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
equation that defines the quaternion vector is {\bfq} = {[\it {\bfq_1} } {s ]ˆT}, where the
\it invokes italics, and the ˆ means superscript. Since the purpose of Simulink is to model
the equations in a signal flow form, it is always good practice to include annotation for the
actual equation. In fact, this practice will ensure that the next user of the block understands
both the block flow and the equations that the block is implementing. To familiarize you
with the TeX notation, open the quaternion model and review the two text blocks.
Motor Voltage
Back emf
Back emf = K b ω
where K b is the Back emf Gain
the coil will see a torque that stops it from rotating, and it will stop. To overcome this,
the current in the coil must change direction at least every half revolution. The device that
switches the direction of the current at the half revolution point, if it is mechanical, is a
commutator.
Most modern motors achieve this switching using electronics where, as the rotor
turns, a device on the shaft lets the driving electronics know where the rotor is relative to
the fixed magnet (the stator). The torque applied to the motor is proportional to the current
flowing in the rotating coil, and as long as the gap between the rotating coil and the fixed
magnet is small, the torque is nearly linear. Thus, for the first part of the model, the torque
is directly proportional to the magnetic field induced in the rotor by the rotor current; since
the magnetic field is also nearly linear, the torque is proportional to the current flowing in
the rotor. Thus, the torque is Tm = Ki iR . The rotor electrical circuit consists of the rotor
resistance RR , the rotor inductance LR , the voltage applied to the rotor Vin , and the voltage
induced across the rotor because of its motion in the magnetic field of the stator, Vb . (This
voltage is the back electromotive force [emf ].)
A simple circuit diagram (see Figure 3.15) can represent the electromechanical equa-
tions for the motor. In this diagram, the motor torque Tm is the controlled source Ki i, and
the back emf, Vb , is the controlled voltage source whose voltage is Kb ωM .
It is a simple matter to use the fact that the sum of the voltages around the loop must
be zero to get the equation for the current in the rotor (iR ):
diR
LR = −RR iR + Vin − Vb .
dt
The back emf is a nonlinear function of how fast the motor is turning. For well-designed
motors, however, the voltage Vb is proportional to the motor angular velocity (ωM ). Thus,
the motor electrical circuit is the differential equation
diR RR Kb 1
=− iR − ωM + Vin .
dt LR LR LR
3.5. Modeling the Motion of a Satellite in Orbit 107
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Because electrical energy, when converted into mechanical energy, is conserved, the con-
stants Km and Kb are dependent. Thus the electrical power (the product of the back emf eb
b iR
and the rotor current) is PElectical = e746 (where in the English unit system the 746 converts
iR
watts into horsepower [hp]. Using the linear back emf voltage this term becomes Kb ωM 746 .
In a similar way, the mechanical power is the torque produced times the motor angular
velocity: Tm ωM = Ki550iR ωM
(where the 550 in the English unit system converts foot-pounds
into hp). Equating these gives Kb = 746 550
Ki = 1.3564Ki (in the English unit system).
where the rotations are about the z-, y-, and x-axes, are in the sequence φ, θ, ψ, and we
have explicitly indicated with the subscript BI that the rotations are with respect to some
inertial (i.e., fixed) axis to the principal axes of the body that is rotating.
When the body is rotating with angular velocity
p
ω= q
r
about these principal axes, the matrix C satisfies the differential equation
0 r −q
dC
= −r 0 p C
dt q −p 0
= − c1 c2 c3 .
We can also write this matrix differential equation in another form by creating a 9 × 1
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
vector that consists of the columns of C concatenated one on top of the other. That is, we
let the vector c be
T
c= c11 c21 c31 c12 c22 c23 c31 c32 c33 .
We also define the cross product matrix for C as (where the subscript i denotes the ith
column of the matrix C)
0 −c3i c2i
Cxi = c3i 0 −c1i .
−c2i c1i 0
In terms of this cross product matrix, the differential equation for the columns of C is
dci
= Cxi ω.
dt
Applying this equation to the vector c, we get a vector matrix differential equation for the 9-
vector c in terms of the three vectors ω as (where CCross is the 9 × 3 matrix defined explicitly
by this equation)
Cx1
d
c = Cx2 ω = CCross ω.
dt
Cx3
You can deduce some interesting attributes of CCross using Maple. Develop a Maple program
(from MATLAB) to show, remarkably, the two products below:
Maple facilitates the calculations (all of the algebra and trigonometric identities involved
in manipulating these matrices; they are quite extensive and are done automatically) and
saves an immense amount of work.
If you have not built the Maple program yourself, you can open the MATLAB program
Maple_CCt_identity in the NCS library to do the computations. This program sets up
the direction cosine matrix in Maple and computes CTCross CCross and CTCross c. (It computes
the vector q and the matrix Q used in the following discussion.)
From these identities, we can create a nice error signal for a rotation.
Multiply the equation dtd c = CCross ω by CTCross on both sides to give
d
CTCross c = CTCross CCross ω = 2ω.
dt
So ω = 12 CTCross dtd c, or ω is the rotational rate needed to make the direction cosine matrix
C. Therefore, if we want a particular direction cosine matrix whose elements are the 9-
vector c, all we need to do is calculate the value of ω from this equation and use it to drive
3.5. Modeling the Motion of a Satellite in Orbit 109
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
the three axes of the spacecraft. From the second identity we proved in the Maple program,
when the direction cosine matrix is at the desired value, the command becomes zero, as we
require. Notice how neatly this error signal works.
Finally, the differential equation for the quaternion vector that we developed above (in terms
of the matrix Q) is
q1
d
q2 .
ω = 2QT
dt q 3
s
This last equation becomes the defining equation for the quaternion command (again fol-
lowing the steps we used for the direction cosine); the commanded rate to achieve a desired
quaternion value is
ηC = 2QT qDesired .
We use this in the spacecraft attitude-control model. All of the pieces we need are now in
place, so we can create the Simulink model for the simulation. You might want to try to do
this yourself before you open the model Spacecraft_Attitude_Control in the NCS
library.
Spacecraft
Wheel Momentum J
Inertia Matrix
Quaternion and Spacecraft
[3x3] Rotational Dynamics
PID [3x1]
Controller Convert (rad) Wheel Inertia J
Inertia [3x3]
to Volts 4
Altitude Error dq/dt
[3x1] [3x1] [3x1] [3x1] [3x1]
Command 1 V_in Wheel Speeds Jw Input External Momentum (M)
[3x1] [3x1] omega_wheel (r/s) [3x1] Momentum
Rate Error
3 of Wheels
Reaction Wheels omega
3 3
Body Angular Accel
q
4
Integrate
omega dot
3
omega 1
2E' q_desired
omega 3 s xo
3
Initial 4
ones(3,1)*0.01
omega
Quater nions
Integrate
Normalize the
qdot
quaternion[q s] 1
4
E' q_desired 4 1
4 s xo 4
[3x4] 4
Matrix Matrix E' Quaternions 4
2 [3x4] 4 ||x|| x
[3x1] [3x1] Multiply [4x1]
Mult by 2 Magnitude of q
Form quatern ion
propagation matrix 4
[4x1]
Desired Initial
[0.5 0.5 0.5 sqrt(3)/3 ]'/1.048
q_desired/norm(q_desired) altitude q
(q_desired) In1
[3x4]
Calculate
and display
E'*E (Should be I_3)
In addition, there are blocks that compute the command (ηC = 2QT qDesired ) and a
block that checks the identity QT Q = I3×3 . (This is used to verify the accuracy of the
computations and that the model is accurate.)
It is a good idea to familiarize yourself with each of the subsystems and the overall
simulation before you run it. The data is all contained in the model; the inertia of the wheels
is 50 slug-ft2 and the vehicle inertia matrix is diagonal, with the diagonal values of 1000,
700, and 500. The reaction-wheel-dynamics model uses the same parameters as we used
in the model above. The model uses as initial conditions for q the value [0.4771 0.4771
0.4771 0.5509], and the desired final value is set to q_desired/norm(q_desired) in the block
that is labeled Desired Attitude. The value for q_ desired is [1 2 3 4]. You can change this
value at the MATLAB command line to any value you would like and see the result.
The PID controller is set up with only PD. (The Integral gain is set to zero; look inside
the PID block to see the values.) The Proportional gain is set to 10, and the Derivative
gains (on the three angular velocities p, q, and r) are 70. We have not attempted to make
these values optimum in any way; they simply make the response reasonably fast with a
minimum overshoot in the quaternion values at the end of the command.
When you run the model using these parameter values, you should see the quaternion
values plotted in the figure at the right below. Note that these quaternion values change
smoothly over the time interval, and they do indeed go to the desired final values from the
initial value we used.
3.5. Modeling the Motion of a Satellite in Orbit 111
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
Minus
q1
-u s
-q1 Horiz Cat
q3
[1x4]
-q2
-q1
Minus1
q2
-u -q3
-q2 Horiz Cat Vert Cat Matrix Q'
s [1x4]
Quaternions [1x4] [3x4]
q1 1
4
1 -q2
[1x4]
q3 q2 Matrix
-q1 Horiz Cat Concatenation
Minus2 [1x4]
s
-q3
-u
Figure 3.17. Matrix concatenation blocks create the matrix Q used to achieve a
desired value for q.
One aspect of
this model is new,
namely the way we
create the matrix
QT . We use two
steps to form the el-
ements of the ma-
trix (which has 3
rows and 4 columns).
First, we compute
the 3 rows (each 1 ×
4) one at a time,
and then we con-
catenate them verti-
cally to create the
matrix.
The block that
creates the rows and
columns is the Ma-
trix Concatenation
block in the Math
Operations library in
Simulink. This
block takes scalars
or one-dimensional vectors and places them into the columns (called horizontal concate-
nation) or the rows (called vertical concatenation) of a matrix. The initial construction of
the rows places the various scalar values from the quaternion vector q into the appropriate
locations to build the three rows of the matrix (using the horizontal version of the block),
and then the rows are stored into a matrix using the vertical version of the block. Figure 3.17
112 Chapter 3. Nonlinear Differential Equations
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
shows how we used the block “Horizontal Concatenation” in the subsystem called “Form
quaternion propagation matrix.”
The quaternion block is applicable to more complex dynamics than a spacecraft.
Examples are the rotational dynamics of an automobile or any other vehicle in motion, a
robot, a linkage, or an inertial platform. Since the quaternion formulation can model these,
it is a good idea to keep this model as part of a user library with the other models in the
Simulink browser.
Exercises
3.1 Show that the matrix product
dϕ
p dt
1 0 0 0
q = 0 + 0 cos ϕ sin ϕ dθ
dt
r 0 0 − sin ϕ cos ϕ 0
1 0 0 cos θ 0 − sin θ 0
+ 0 cos ϕ sin ϕ 0 1 0 0
dψ
0 − sin ϕ cos ϕ sin θ 0 cos θ dt
gives
p 1 0 − sin θ φ
q = 0 d
cos φ sin φ cos θ θ .
r 0 − sin φ cos φ cos θ dt ψ
Exercises 113
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php
3.2 Show that the direction cosine matrix satisfies the differential equation
0 r −q
dC
= −r 0 p C
dt q −p 0
= − c1 c2 c3 .
3.3 Start with the Simulink model for the quaternion representation of rotations. Next,
create a model that does the same thing using direction cosine matrices and, last, a
model that creates a rotation with Euler angles. Compare the computational efficiency
of the three models. In particular, see what happens when the Euler angles approach
the points where the transformations become infinite. Reference [24] has a good
discussion of why the quaternion approach is better.
3.6 Try adding a simple controller to the reaction wheel model that will cause the reaction
wheel speed to decrease whenever it gets to 1000 rpm. Use an external torque applied
by a reaction jet. (The jet will create a force, so you need to decide on where it is
relative to the center of gravity to determine the torque.) If you apply a torque using
only a single jet with the force not through the center of gravity, then rotations will
occur about multiple axes. How do you prevent this? How many jets will you need
to unload the reaction wheels in all three axes?
3.7 Verify the quaternion will move toward the desired quaternion qDesired if the rate body
rate is
ηC = 2QT qDesired .
In this equation, the matrix
s −q3 q2
q3 s −q1
Q=
−q2
.
q1 s
−q1 −q2 −q3
Also, verify that the matrix concatenation blocks in Figure 3.17 create this matrix.