ENGM541 Lab5 Runge Kutta Simulinkstatespace

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ENGM 541 Laboratory #5

Page 1 of 5
University of Alberta
ENGM 541: Modeling and Simulation of Engineering Systems
Laboratory #5

M.G. Lipsett, Updated 2010

Integration Methods with Higher-Order Truncation Errors with MATLAB

MATLAB is capable of a number of integration techniques to simulate the behaviour of
dynamical systems, once they are cast into first-order form.

Solving First-Order ODEs in MATLAB is straightforward. The first step is to define a
function to represent the equation (or equations) of the propagation system. Or example,
if we want to integrate numerically the equation
), cos(t
dt
dy
=
we write a MATLAB function for the equation:
function ydot = eq1(t,y)
ydot = cos(t) ;
To solve this ODE, MATLAB calls the function ode23, which integrates the differential
equation using second- and third-order Runge-Kutta
1
method, using the following syntax:
[t,y] = ode23(function_name, [start_time end_time], y(0))
If we are interested in the behaviour of the system in the time interval 0 t , and our
system has the initial condition y(0) = 2, then the function call will be:
>> [t,y] = ode23(@eq1,[0 pi],2);
The notation @eq1 is a function handle to the function eq1 that we defined above,
which contains the expressions for the derivatives. Note that this method does not use a
defined (fixed) step size, but rather attempts to meet a truncation error specification by
modifying the step size. You can create a function that contains multiple derivatives. In
that case, you need to define a vector for the set of derivatives, and you need a row vector
in the call to the solver for the set of initial conditions. The Mathworks web site has a lot
more information about how to use these solvers; here is an excerpt:
An example of a nonstiff system is the system of equations describing the motion of a rigid body
without external forces.

To simulate this system, create a function rigid containing the equations
function dy = rigid(t,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
In this example we change the error tolerances using the odeset command and solve on a time
interval [0 12] with an initial condition vector [0 1 1] at time 0.
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);

1
These methods were developed about 100 years ago by C. Runge and M.W. Kutta, two German
mathematicians. Runge-Kutta is pronounced run-guh kut-tuh, in case you were wondering.
ENGM 541 Laboratory #5
Page 2 of 5
Relative Error

While the numerical solution y(t) looks right, it would be good to know how accurate
the numerical result actually is. The relative error is
,
) (
) ( ) (
t f
t y t f
=
where f(t) is the analytical solution. In our example, we can easily find the analytical
solution to be
. 2 ) sin( ) ( + = t t f

Exercise 1

For most ordinary differential equations, ode45 will have higher accuracy than ode23,
because ode45 uses fourth-and fifth-order Runge-Kutta solvers. The syntax is essentially
the same.

a) Write an m-file with a for loop that calculates the relative error at each step for the
example above solved using ode23, and verify that the maximum error is 6.907x10
-4
.
b) Solve the example system using ode45, calculate the relative errors, and find the
maximum error.

State Space Representations in SIMULINK

Previously we tried out SIMULINK with simple linear systems, using icons to show the
admissibility equations (such as the sum of forces at a node equaling zero) and employing
integrators to transform a variable to its integral form, and then using the constitutive
relationships to turn the variable into the other variable type to connect back to the
admissibility equation. We also added other features for inputs (such as forces) and
outputs (such as measurements of displacement at different points in time).

SIMULINK can use other types of blocks to incorporate more complex dynamic
behaviours.

Recall the equation of motion for a linear mass-spring-damper system:
t f kx x b x m ( = + + & & & )
which can be simulated in SIMULINK using simple elements as shown in Figure 1.
ENGM 541 Laboratory #5
Page 3 of 5
d2x/dt 2
force
input
spring
force
damper force
ma
x
dx/dt
Note : numbers in blocks are not representative
of the parameters in the exercise !!
This is an example of a force input block ;
other types of input are used in the exercise
this summing block
is like the force balance
at a node
k1
0.1
k
0.1
XY Graph
Integrator 1
1
s
Integrator
1
s
Clock
Chirp Signal
1/m
0.2
xddot xdot

Figure 1

This linear system can also be expressed in state-space form as

Du Cx y
Bu Ax x
+ =
+ = &

where A is an n-by-n state matrix, with n as the number of state variables x, B is an n-by-l
input matrix, where l is the number of inputs u; C is an p-by-n output matrix, where p is
the number of outputs y; and D is a p-by-l direct-transmission matrix.

The state-space representation of the linear spring-mass-damper system can be written in
first-order form as

m
t f
x
m
k
x
m
b
x
x x
) (
;
1 2 2
2 1
+ =
=
&
&

where x
1
= x. The input is u = f(t). Lets assume that there is a linear potentiometer that
measures the displacement of the mass, with a linear relationship for the output
measurement y
), ( ) ( t qx t y =
where q is a constant coefficient. With our definitions for x
1
, x
12
, u, and y, we can express
this set of equations in state-space matrix form as

[ ] [ ] . u
x
x
q y
; u
m
x
x
m
b
m
k
x
x
0 0
1
0 1 0
2
1
2
1
2
1
+
)
`

+
)
`

(
(


=
)
`

&
&
.

So we can build a SIMULINK block diagram of this system using a state space block,
rather than having to put together a bunch of integrators and other elements.

The real advantage of a state-space representation is that it is not limited to linear
systems. As weve seen when we write a set of governing equations of the form
ENGM 541 Laboratory #5
Page 4 of 5
), t , x ,..., x , x ( f
dt
dx
n i
i
2 1
= i = 1, , n,
there is no restriction that the functions f
i
all have to be linear functions of the state
variables x
i
and t. There are a number of different forms that state-space representations
can take. The most common forms are described in the text. The matrix form is limited to
linear functions.

Exercise 2

Build a SIMULINK model for the linear mass-spring-damper system using a state-space
block and the following values for the parameters: m = 1, k = 3, b = 4, q = 10. Simulate
the response of x(t) for three different inputs u = f(t):
a unit step input,
f(t)= sin(0.2t), and
f(t) = t.
Use x(0) = 0 and x& (0) = 0 for the initial conditions, and run the simulation for a few
seconds, displaying the output y.

Linearisation of a System

For the nonlinear mass-spring-damper system the equations of motion can be written
) t ( f ) x ( f x b x m
K
= + + & & &
where f
K
(x) represents the nonlinear spring force at displacement x. To linearise the
system, we first have to find the operating point. We do this by replacing the forcing
input f(t) by its average value (or by an equilibrium value, if that has been specified), and
x by x :
f ) x ( f x b x m
K
= + + & & &
Because x is a constant, that means that 0 = = x x & & & . For this example, we assume that
f has been specified to be zero, which means that the governing equation reduces to:

f = f
K
(0) = 0
The operating point for the spring is at x =0, f
K
= 0. If the forcing input is nonzero at the
operating point, then the element operating point will be different. The next step is to
rewrite the linear terms in the governing equation in terms of the incremental variables
for the state variables and input functions, which in this case are:

x = x x and

f = f f , giving the governing equation in terms of the operating point and the
incremental variables:

). t ( f

f ) x ( f ) x

x ( b ) x

x ( m
K
+ = + + + + & & & & & &
Well deal with the nonlinear spring force term in a minute by linearising it, but first lets
simplify the governing equation a bit by eliminating x& and x& & , which are both zero
terms:
). t ( f

f ) x ( f x

b x

m
K
+ = + + & & &
Now we use the Taylor series expansion of the nonlinear spring force
ENGM 541 Laboratory #5
Page 5 of 5

x ) ( k ) ( f
x
dt
df
) ( f ) x ( f
K
x
K
K K
0 0
0
0
+
+ + =
=
K


where k(0) is the slope at the operating point, and substitute the first two terms into the
governing equation in terms of incremental variables to get the linearised form of the
governing equation:
). t ( f

f x ) ( k ) ( f x

b x

m
K
+ = + + + 0 0 & & &
Since f = f
K
(x ) = f
K
(0) = 0, the linearised model is finally written as
) t ( f

x ) ( k ) ( f x

b x

m
K
= + + + 0 0 & & & ,
which is a linear ODE in terms of the incremental variable x and the incremental input

f (t). To solve this system, we need initial values for x (0) and ) ( x

0 & , which we find from


x(0) and ) ( x 0 & :

x ) ( x ) ( x

x ) ( x ) ( x
& & & =
=
0 0
0 0

which for this example has 0 = = x x & & & .
Once we have solved the linearised system model, we find the approximate solution for
the nonlinear model by adding the operating point x to the incremental solution:
x(t) = x + x (t),
which is only an approximate solution of the nonlinear model.
To write this second-order system in state-variable form, we need an incremental velocity
v = v v , where v = 0. With x

v & = , we get the equations in first-order form as



[ ], ) t ( f

v b x ) ( k
m
v

v x

+ =
=
0
1
&
&

And since 0 = = x x & , the initial conditions are

. ) ( v
) ( x
0 0
0 0
=
=

(This example came from Close & Frederick Chapter 9.)

References:
C.M. Close, C.E. Frederick. Modeling and Analysis of Dynamic Systems, 2
nd
Ed.
Houghton-Mifflin 1993.
D. Etter, Engineering Problem Solving with MATLAB. Prentice-Hall 1997.
D. McMahon, Matlab Demystified. McGraw-Hill 2007.
www.mathworks.com

You might also like