You are on page 1of 33

Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.

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

3.1 The Lorenz Attractor


One of the more interesting uses of simulation is the investigation of chaos. Chapter 7
of Cleve Moler’s Numerical Computing with MATLAB [29] describes the Lorenz chaotic
attractor problem developed in 1963 by Edward Lorenz at MIT. The differential equation is
a simple nonlinear model that describes the behavior of the earth’s atmosphere.
Cleve analyzes the differential equation’s “strange attractors”—the values at which
the differential equation tries to stop but cannot because the solution, although bounded, is
neither convergent nor periodic. The model tracks three variables: a term that tracks the

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 .

Alternatively, in state-space form (even though the equations are nonlinear),


 
−β 0 y2
dx(t) 
= 0 −σ σ  x(t).
dt −y2 ρ −1

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)

We created the nonlinear equation in Simulink by building each of the differential


equations around the integrator block in the Continuous library, and at the same time, we
use a new block to plot the resulting solution on the two axes (y1 versus y2 and y1 versus
y3 ). The model is in the NCS library, and it opens using the command Lorenz_1. The
model is in Figure 3.1 along with a typical set of two-dimensional plots.
This model uses a few new tricks that you should explore carefully. First, the two
dimensional plots are from the Simulink Sinks library. The scales on these plots are set
by double clicking on the icon. (Review the settings when the model is opened.) In
order to have a new initial condition every time the model runs, we have added a Gaussian
random perturbation to each of the initial conditions in the integrator blocks. The MATLAB
command randn is used in the “Constant” block, and each integrator has its initial condition
set from the “external” source (so they actually appear in the diagram; this is a good practice
when the models are simple because the initial conditions are visible, but when the model
is very busy, this approach can make the diagram too busy). To make the initial conditions
external, you need to open the integrator dialog by double clicking the integrator block, and
then select external as the source of the initial conditions.
3.1. The Lorenz Attractor 83
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.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).

3.1.1 Linear Operating Points: Why the Lorenz Attractor Is Chaotic


Since Release 7 of Simulink, the Control System Toolbox link has a very robust linearization
algorithm. Two blocks appearing in the Simulink Model-Wide Utilities library can be used
to get a linear model of a nonlinear system. The first of these blocks is the time-based
linearization, and the second is trigger-based linearization. The first will create a complete
state-space model at the times specified in the dialog that opens when you double click the
block. Unlike the Simulink Scope block, this block does not require a connection to the
model. The block causes the simulation to stop at either the times or the triggers, and then
a program from the Control System Toolbox, called linmod (for a continuous time model)
or dlinmod (for a discrete time model), creates the linear state-space matrices A, B, C, and
D. We changed the model Lorenz_1 to create the linearizations using the trigger block.
The triggers come from the Pulse Generator in the Sources library. In this new model,
called Lorenz_2 in the NCS library (Figure 3.2), the diagram uses the “GoTo” and “From”
blocks in the Simulink Signal Routing library to make it easier to follow. These blocks
allow connections without a line, thereby simplifying the diagram. The model was also
redrawn a little to emphasize the state-space form of the Lorenz differential equations. The
state-space form of the equations is in the text block in the model (created using the “Enable
Tex Commands” option in the Format menu). When this model runs (slowly because
the linearization is taking place every 0.01 sec of simulation time), the linear models are
created and stored in a structure in MATLAB. We use the structure created by linmod—
called Lorenz_2_Trigger_Based_Linearization_—to investigate the stability of the
84 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

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

El em ent 3 of the ma trix Ay


Pl ot
y3
Subsystem
Trig ger-Based (Uses Data from GOTO s)
Li nearizatio n.
y2 Rho 1
y3
xo s y3 Th is set of bl ocks creates
a li near mo del at the time s
y1 *y 2
trig gered by the pulse generator.
y1
sqrt(Beta*(Rho-1))+20*randn Th e results are stored in a structure
Nonlin earity: in the MATL AB Workspace.
Prod of
y1 and y2

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

Eigenvalues of the Lorenz System


(Every 0.01 sec. of the 20 sec. simulation)
30

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

Figure 3.3. Eigenvalues of the linearized Lorenz attractor as a function of time.

From the eigenval-


ues plot above we can see
that the initial condition
was unstable; when the
simulation starts there are
a complex pair of eigen-
values that have a real part
of 2.91. The eigenval-
ues are almost in the left
half plane, and during the
next four times they move
toward the left half com-
plex plane. From the sixth
eigenvalue on, they have
moved into the left half
complex plane, indicating
that the solution now is
86 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

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.

3.2 Differential Equation Solvers in MATLAB and


Simulink
Simulink uses the same solvers that are in the MATLAB ode suite (although they are C-
coded in an independent way and automatically linked by the Simulink engine to solve the
differential equations modeled in the diagram). Chapter 7 of NCM describes these and how
they work, so we will not go into the details here.
When you create a new
Simulink model, the default solver
is the variable step ode45, and the
default simulation time is 10 sec. In
addition, the “Relative tolerance” is
set to 1e-3 (or approximately 0.1%
accuracy), and the “Absolute toler-
ance” is set to “auto.” The solver
also allows the user to force a Max,
Min, and Initial step size if de-
sired, but these are all set initially
to “auto.” The solver options allow the “Zero crossing control” to be set so that zero cross-
ing detection occurs for all blocks that allow it (or, optionally, none of the blocks). The
default option for zero crossing is “Use local settings,” where the solver checks to see if a
zero crossing has occurred for those blocks that this option is set to “on.” For example, the
absolute value block has the dialog shown above. If the “Enable zero crossing detection”
check box is not checked, then in the “local settings” mode, this block’s zero crossing is
disabled. Note that if the zero crossing control is set to “Enable all,” this check box selection
does not appear.
Some simple numerical experiments were performed (in the spirit of Section 7.14 in
NCM) using the Lorenz model above. To do these experiments, we execute the Simulink
3.3. Tables, Interpolation, and Curve Fitting in Simulink 87
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php

Table 3.1. Computation time for the Lorenz model with different solvers and tolerances.

Solver Type Ode45 Ode23 Ode113


Tolerance 0.001 0.0370 0.1445 0.1802
Tolerance 0.000001 0.3670 0.4680 0.3749

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.

3.3 Tables, Interpolation, and Curve Fitting in Simulink


There are times when a simulation requires tabulated data for a function. Examples of this
are systems that are operating in flowing air or water (aircraft, rockets, submarines, balloons,
parachutes, etc.). In many of these applications, the tabular data represents the gradient of
a nonlinear function of one or more variables. The gradient measurement comes from wind
tunnels, water tanks, or actual measurements of the system in its environment (a full-scale
model of an airplane or automobile). How do you use such data in Simulink?
Simulink implements most of these curve-fitting tools in a set of blocks that are in
the Lookup Tables library. This library has a variety of tables. The simplest is the “Lookup
Table”; the versions that are more complex are the 2-D and n-D (for functions that give two
outputs or more). A block that does interpolation (n-D), using what is called a PreLookup of
88 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

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.

3.3.1 The Simple Lookup Table


To illustrate the simple lookup table, let us return to the home heating system model we
introduced in Section 2.4.1. The original model used an outdoor temperature variation of 50
deg F with a 24-hour sinusoidal variation of 15 deg. This might be a typical diurnal variation
in the outdoor temperature, but to compute the fuel usage for a specific winter’s day we
want to use the actual outdoor temperature. Assume that you have made measurements of
the temperature during a particular cold snap when the temperature plummets in a 48-hour
period from an average of 40 deg to about 10 deg. Figure 3.4 shows the temperature over
a 24-hour day (sampled every 15 minutes).
The data were actually generated from MATLAB using the following code:
Time = (0:.25:48)*3600;
Temp = 50-0.75*Time/3600 –
5*sin(2*pi*Time/(24*3600)+pi/4)+randn(size(Time))

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

Heat Cost ($)

1/s Mdot*ha cost

Heat Output Cal/Hr. Cost of Fuel


$/Cal/Hr

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

Figure 3.6. Dialog to add the tabulated input.

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.

3.3.2 Interpolation: Fitting a Polynomial to the Data and Using the


Result in Simulink
There is another option in Simulink that in some applications may be more accurate and easier
to use. This option is the curve fitting block, “Polynomial,” in the Math Operations Simulink
library. The use of this block requires you to do some preliminary work in MATLAB to
compute the coefficients of the polynomial that will interpolate and extrapolate your data.
The process of creating a curve fit to any data uses the “Basic Fitting” tool in MATLAB.
To open this tool, create a plot of the data, using the MATLAB commands that we used
to create the 48-hour outdoor temperature profile above or running the MATLAB M-file
simulated_temp_data in the NCS library.

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

Temperature Data for Norfolk MA, Jan. 1,2006


30
Temperature (Dry Bulb)
29 Temperature (Wet Bulb)

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

3.4 Rotations in Three Dimensions: Euler Rotations,


Axis-Angle Representations, Direction Cosines, and
the Quaternion
We saw in Chapter 1 that the Foucault pendulum has forces that are a consequence of the
rotation of the coordinate system where the pendulum is mounted. When we extend these
ideas to a rigid object rotating in three-dimensional space, it is not easy to keep track of
the new orientation. This is because even when doing the rotations about a single axis at a
time, the order they are done will determine the result. The simplest way to illustrate this
is to think about what happens if you were standing, facing north, and you then turned 90
degrees to the left followed by a 90-degree rotation to lie down on your back. You would be
laying face up with your head to the east. If you did this sequence in the reverse order—i.e.,
you first lay down on your back and then you turned left—you would be lying on your left
side with your head to the north, clearly not in the same orientation as the first sequence.
Mathematically, rotations do not commute.
The way we keep track of the rotation of a body is with the angles of rotation about
each of the three orthogonal axes. Mathematically, rotations are transformation matrices
that describe what the rotation does to the x, y, and z coordinates after the rotation. These
matrices have unique representations and manipulations.
3.4. Rotations in Three Dimensions 95
Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php

y New
y

x New

Figure 3.11. Single axis rotation in three dimensions.

3.4.1 Euler angles


Euler proved that any orientation requires at most three rotations about arbitrary axes on a
rigid body. (The rigidness of the body ensures that the complete body is a single mass as
well as inertia; we discuss what happens when the body is not rigid in Chapter 6.) The angles
that define the rotation are Euler angles, and they are not unique. In most applications that
use Euler angles, it is conventional for the rotations first to be about the x-axis, followed
by a rotation about the new y-axis and then another rotation about the new z-axis. (This
is abbreviated “123 rotation”.) This is not unique since there are 12 possible orderings
of the rotation axes (namely 123, 132, 213, 232, 312, 321, 121, 131, 212, 232, 313, and
323). To compute the orientation of the new axes after such a sequence of rotations, we
need to investigate what happens with each of them alone. Only coordinates in the plane
perpendicular to the axis of rotation change when a single axis rotation is used. We can
think of the three Euler angle rotations as a sequence of three independent planar rotations,
each plane redefined by the previous rotation. Figure 3.11 shows a rotation about the z-axis
that causes the entire x-y plane to rotate.
For the single axis rotation of angle θ about the z-axis shown above, the coordinates
(x, y) of a point in the plane has a new set of coordinates (xN ew , yN ew ) given by

xNew = x cos(θ ) + y sin(θ ),


yNew = −x sin(θ ) + y cos(θ ).

This transformation, using the vector-matrix form, is


      
xNew cos(θ ) sin(θ ) 0 x x
 yNew  =  − sin(θ ) cos(θ ) 0   y  = T(θ )  y  .
zNew 0 0 1 z z
96 Chapter 3. Nonlinear Differential Equations

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

showing that TTT is the identity matrix.)


As an example, if we use this result to compute the transformation of the coordinates
after a standard Euler angle rotation using the axes 121 (i.e., the three rotation angles are
about the original x-axis, then the new y-axis, and, last, about the new x-axis) the new
coordinates are given by
      
xNew 1 0 0 cos(θ) 0 sin(θ) 1 0 0 x
yNew  = 0 cos(ψ) sin(ψ)   0 1 0  0 cos(φ) sin(φ)  y  .
zNew 0 − sin(ψ) cos(ψ) − sin(θ) 0 cos(θ) 0 − sin(φ) cos(φ) z

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?

3.4.2 Direction Cosines


Assume that three rotations that lead to a general rotation are sequential about the axes z,
y, and x (axes 321 as defined above). The rotation matrices above give the final orientation
as the product of three matrices as follows (starting with a rotation of angle ψ about z,
followed by a rotation of θ about y, and, last, a rotation of ϕ around x):
   
1 0 0 cos θ 0 − sin θ cos ψ sin ψ 0
 0 cos φ sin ϕ   0 1 0   − sin ψ cos ψ 0 
0 − sin φ cos ϕ sin θ 0 cos θ 0 0 1
 
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 θ

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

3.4.3 Axis-Angle Rotations


Downloaded 09/09/15 to 128.6.218.72. Redistribution subject to SIAM license or copyright; see http://www.siam.org/journals/ojsa.php

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

3.4.4 The Quaternion Representation


There is a nice computationally compact method for computing rotations using the axis-angle
form. The method uses the “quaternion representation,” invented by William Hamilton in
1843. A quaternion is a modification of the angle-axis 4-vector that represents the rotation,
defined as
 T  
q= q1 s = nx sin(θ/2) ny sin(θ/2) nz sin(θ/2) cos(θ/2) .

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]

q_1 x omega + s omega


x||x||
4
Form the norm of q omega' q_1
q_1/||q|| 4 4 qdot
using a subsystem 0.5 1
3 -1 4 4
qdot_i2b
omega3
Integrate qdot
4
1
4 s x
o
4

Initial_q

This block implements the quaternion equations.

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.

quaternion, using the partition above, is


 

dn 1 dθ

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

 nxω sin(θ/2) + 2 cos(θ/2)ω  1 sω + q1 xω


= dθ  = .
1 2 −ωT q1
− sin(θ/2)
2 dt
Thus, calculating the quaternion rate is simply a matter of some algebra involving the cross
product of the quaternion with the body rate (for q1 ) and an inner product with the body
rate (for s).
We have created a subsystem model for these equations (see Figure 3.12). It uses some
Simulink blocks that we have not encountered before. The first is the block that extracts a
component from a vector (the “Selector” block). We use this block twice: first to extract
3.4. Rotations in Three Dimensions 101

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

have not thoroughly explored the masking of In1


Selector1
subsystems yet, let us take a little detour here
to do so. The idea behind a mask is to cre-
ate a new Simulink block (i.e., a user-defined
Selector2 1
block where the data needed to execute the Out1
block appear in a dialog that the user selects
2
by double clicking on the block, as we have In2
seen many times for the built in blocks in the Selector3
Simulink browser). The mask hides the in-
ternal mathematics from the user so it has the
look and feel of a Simulink built-in block. To
Selector4
build such a block, you first create a subsys-
tem in the usual way and then invoke the mask. For the cross product, the subsystem uses
two multiplication blocks and four Selector blocks (to select the appropriate components of
the vector for the cross product). The subsystem is in the figure above.
If you were creating this block as a masked subsystem, the next step in masking the
subsystem would be to right click the subsystem block and select “Edit Mask” from the
menu. This opens the “Edit mask” dialog that allows you to specify the mask properties.
For the cross product block, no input parameters are required, so the only thing you might
want to do is provide documentation for the block using the “Documentation” tab in the
dialog. We will see other masked subsystems as we start to build models that are more
complex. Therefore, we will defer further discussion on building a mask until we need
to use them in these models. Using the equations above and the cross product block, the
Simulink model that will compute the quaternions is shown below. (This model is called
Quaternion_block in the NCS library.) The input to this block is the angular velocity in
body coordinates of the rigid body, and the output is the quaternion rate. To complete the
quaternion calculations we must integrate the quaternion rate. We will do this outside the
quaternion subsystem after we show how to extract the Euler angles and direction cosine
matrix from q.
We use the definition of q to find the Euler angles and direction cosine matrix. Re-
fer back to the definition of the direction cosine matrix: the Euler angles come from the
appropriate angle in the matrix and the inverse trigonometric functions. For example, in
the 121 representation we developed above, the 3,2 element of the direction cosine matrix
is sin φ cos θ , and the 3,3 element is cos φ cos θ , so if we divide the 3,2 element by the 3,3
element we get tan φ, so the Euler angle φ is given by φ = tan−1 ( cc3,2 3,3
). In a similar fashion,
the other angles are
 
  c1,2
θ = sin−1 c1,3 and ψ = tan−1 .
c1,1
102 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

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

θ = sin−1 (−2(q1 q3 − sq2 )) ,


 
2(q1 q2 + sq3 )
ψ = tan−1 2 .
s + q12 − q22 − q32

Again, using the same reasoning, the direction cosine matrix is


 
s 2 + q12 − q22 − q32 2(q1 q2 − sq3 ) 2(q1 q3 + sq2 )
 
 2(q1 q2 + sq3 ) s 2 + q22 − q12 − q32 2(q2 q3 − sq1 ) .
2(q1 q3 − sq2 ) 2(q2 q3 + sq1 ) s 2 + q32 − q22 − q12

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

Transpose q Form q q^T


Reshape 2
Gain
Get |q|^2
Form (s^2-q dot q) I

1
1 em
Direction
Quaternion Cosine
Vector Matrix
square s eye(3) 3x3 Identity Multiply by q4

col vector Horiz Cat Form skew Sym


zeros(3,1)
of zeros cross product
matrix Q

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

This block implements the conversion from


Quaternions to the Direction Cosine Matrix.

The Equation implemented is:

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 |

Figure 3.13. Simulink block to convert quaternions into a direction cosine.

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

This section of the block implements the equation:



/ = J -1 [ - ω X J ω - dJ/ ω +m ]
dt dt total

Inv Body Angular


External
Momentum (M) Accel
dJ/dt 3 U( : ) 1
2 Matrix Make into a 4x1
Multiply column vector
4
omega

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

This section implements the quarternion equations.


dq
/
1 = 1/2 [ q X ω + s ω ]
dt 1
and
ds T
/ = -1/2 ω q
dt 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.14. Combining the quaternion block with the body rotational acceleration.

{\bf{\it This block implements the quaternion equations.}}


Let the quaternion {\bfq} (a 4 vector) be partitioned into a 3 vector and a scalar as
follows:
{\bfq} = {[\it {\bfq_1} } {s ]ˆT}.
The differential equations for the partitioned components of the quaternion vector are
{{ˆ{d{\bf\itq_1}}}}/_{\itdt} = 1/2 [ {\bf{\itq_1} X {\omega}} + s {\bf {\omega}} ]
and
ˆ{ds}/_{dt} = -1/2 {\bf{\omega}ˆT} {\it{\bfq_1}}.
To ensure that the quaternion vector has a unit norm, {\bf||q||} is calculated at each
step and the derivatives are divided by this norm before they are integrated (ensuring
that the vector has unit norm at the time t, just prior to the next integration step).
3.5. Modeling the Motion of a Satellite in Orbit 105

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.

3.5 Modeling the Motion of a Satellite in Orbit


With the blocks we created in Section 3.4, we can now build a simulation for the rotational
dynamics of a satellite in orbit. This really is the first Simulink model that we will build
that is close to a real problem. As such, you can use it for models that you might build in
the future.
We start by listing the specifications for the model and for what we hope to achieve.
We assume we are trying to build a control system for a satellite that will orbit the earth.
We also assume that the satellite’s control system will consist of a set of reaction wheels—
devices that are essentially large wheels attached to a motor. The motor accelerates the
reaction wheel and in the process creates a torque on the vehicle through Newton’s third
law. (The spacecraft reacts to the accelerating inertia because the torque produced is equal
and the opposite of the torque on the wheel.) We assume that there are three wheels mounted
on the spacecraft along the three orthogonal body axes. (For the exercise we will assume
that the wheels are along the axes of the body, and it is symmetric so the inertia matrix is
diagonal.)
The first part of the model uses the quaternion and acceleration model from Section 3.4.
For the second part of the model, we will need to create models for the electric motors that
drive each of the wheels and the torque that they create. Once we have the model, we can
create a feedback controller for the reaction wheels that will cause them to move the satellite
from one orientation to another. In the process of creating these models, we will end up
with a simulation for the rotational motion of a satellite in orbit. We also will confront
a problem that always exists with reaction wheels, namely that they can provide a torque
only as long as the wheels can be accelerated. Because a motor cannot accelerate once it
reached its maximum speed, a method is required to allow the motor to decelerate back to
zero speed. This uses reaction jets to “unload” the wheel (the jets, by firing in the opposite
direction from the torque created by the deceleration, allow the reduction of the wheel’s
speed without affecting the spacecraft). We will be modeling only the reaction wheel in the
following, not the unloading of the wheel, and the unloading is left as an exercise.
First, we build a model for the reaction wheels. A DC motor converts an electric
current flowing through an iron core coil into a magnetic field that interacts with a stationary
magnetic field to create a torque that causes the coil to spin. Current flowing through a coil
of wire creates a north pole on one side of the coil and a south pole on the opposite side.
The interaction of the magnet with the stationary magnetic field will cause the coil to spin
as long as the stationary poles are opposite to the spinning coil (i.e., as long as a north pole
of the spinning coil is near a north pole of the stationary coil). As soon as the spin of the
coil cause the north pole of the coil to align with the south pole of the stationary magnet,
106 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

Armature Armature Mechanical Side of the Motor:


Inductance
iR Resistance
J dω/dt= K i i
where K i is the Torque Constant

Motor Voltage
Back emf

Back emf = K b ω
where K b is the Back emf Gain

Figure 3.15. Electrical circuit of a motor with the mechanical equations.

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

3.5.1 Creating an Attitude Error When Using Direction Cosines


We need to describe how one creates an “attitude error” for the 3-axis rotation of a spacecraft.
None of the descriptions (Euler, direction cosine, or quaternion) lends themselves to forming
the difference between a desired and an actual value. We need a way to create an error that
goes to zero at the desired attitude. The direction cosine matrix difference C − Cdesired , or
some similar error for either the quaternion or the Euler angle representation, is not usable.
(The property that causes the rotation to the desired command is that the error is zero when
the command is achieved.) This is because the necessary rotational commands about the
three body axes are a nonlinear function of the angles (in any of the representations).
Let us perform some algebra with Maple to see what we can do to get a representation
for the commands in terms of the direction cosine matrix and the quaternion approach. We
will work with the direction cosine matrix first. The direction cosine matrix we worked
with earlier is
 
cos θ cos ψ cos θ sin ψ − sin θ
CBI =  sin φ sin θ cos ψ − cos φ sin ψ sin φ sin θ sin ψ + cos φ cos ψ sin φ cos θ  ,
cos φ sin θ cos ψ + sin φ sin ψ cos φ sin θ sin ψ − sin φ cos ψ cos φ cos θ

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 .

The value of is explicitly defined by this equation.


108 Chapter 3. Nonlinear Differential Equations

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:

CTCross CCross = 2I3×3 and CTCross c = 09×1 .

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.

3.5.2 Creating an Attitude Error Using Quaternion Representations


There is a complete analogy to this result for the quaternion representation. We will sum-
marize it here and ask you to reproduce the results in Exercise 3.7 at the end of this chapter.
The first part of the quaternion result requires the formation of the 4 × 3 matrix Q as
follows:  
s −q3 q2
 q3 s −q1 
Q=  −q2 q1
.
s 
−q1 −q2 −q3
Then, following similar steps used in the direction cosine matrix derivation, the following
two identities are true:
 
q1
 q2 
QT Q = I3×3 and QT  
 q 3  = 04 .
s

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.

3.5.3 The Complete Spacecraft Model


The model of the complete rotational dynamics of the spacecraft is in Figure 3.16. It has
four subsystems to compute the following:

• the spacecraft rotational motion—this is a variation of the quaternion block we created


in Section 3.4.4;
• the Reaction Wheel dynamics that we created above;
110 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

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)

Figure 3.16. Controlling the rotation of a spacecraft using reaction wheels.

• a block that has a PID controller in it;

• a block that forms the matrix Q from the quaternion vector.

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.

3.6 Further Reading


Reference [40] (Chapter 4 in particular) has a good description of the Euler and quaternion
representations of the rotational motion of a spacecraft.
In his book Unknown Quantity: A Real and Imaginary History of Algebra [7], John
Derbyshire describes the development of the quaternion representation as the logical leap
into the fourth dimension after the two-dimensional representation of complex numbers.
He notes that in 1827 Hamilton began investigating complex numbers in a purely algebraic
way. He found it extremely difficult to come up with a scheme that would make the algebra
distributive and would maintain the property of complex numbers that the modulus of the
product of two numbers is the product of their respective moduli. Hamilton’s insight into the
fact that you could not satisfy this modulus rule with triplets but could do so with quadruplets
led to the invention of quaternion algebra. Hamilton was so pleased with this result that he
inscribed it with a knife on a stone of Brougham Bridge near Dublin. Derbyshire’s book
is about the algebraic properties of the quaternion, which are not a major consideration for
portraying rotations. Despite this, his book is fun to read for its mathematical discussions
and its look at the personalities behind the math.
There is an excellent discussion of the computational efficiency of the quaternion
representation in [11], and you should consult this after you complete Exercise 3.3.

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 

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.4 Show that the norm of the quaternion vector is one.


3.5 Verify that the direction cosine matrix from the quaternion vector is
 
 2  0 −q3 q2
DCM = s − |q1 |2 I3×3 + 2q1 q1T − 2s  q3 0 −q1  .
−q2 q1 0

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.

You might also like