Simulink_I
Simulink_I
where the term ordinary is used in contrast with partial differential equations (PDE) that
contain multi-variable functions and their partial derivatives. ODEs are solved either in
a closed analytical form or by numerical integration.
is considered, this is known as initial value problems which are solved using linear multi-
step methods or Runge-Kutta methods.
Euler Method and its generalizations By replacing the derivative in equation (2)
by the finite difference approximation, one obtains:
y (x + h) − y (x)
y ′ (x) ≃ (3)
h
where h denotes a step size, rearranging and defining equidistant sample points xn =
x0 + nh, yields one step of the Euler method from xn to xn+1 is
in which yn+1 denotes the approximate solution of (2) at xn+1 . The smaller the step
size h the more accurate the approximation, however resulting in a higher number of
iterations and an increase in the computational effort. Higher order methods provide a
better compromise between accuracy and computational effort. Runge-Kutta methods
are iterative methods, which are used in temporal discretization for the approximation
of solutions of ordinary differential equations. The classical fourth order Runge-Kutta
method is:
h
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 ) xn+1 = xn + h (5)
6
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 2
in which the four gradients k1 , . . . , k4 are calculated based on the derivative of the function
at different locations x. Figure 1 shows the contributions to approximate yn+1 with the
second order RK2 method. Notice, that in the figure the independent variable is denoted
by t rather than x as in equation (5).
Figure 1: Runge-Kutta Method, substitute y(x) → y(t), xn → tn figure labels vs. equati-
ons2
In Matlab the numerical methods such as Runge-Kutta are denoted by the term solver.
Matlab handles the following types of first-order ODEs:
• Linearly implicit ODEs of the form M (t, y)y ′ = f (t, y), where M (t, y) is a matrix
In general ode45 is your first try for numerical solutions of ODEs. It is a one-step solver
based on an explicit Runge-Kutta (4, 5) formula, the Dormand-Prince pair.
[T,Y] = ode(odefun,tspan,y0,options)
requires a function handle odefun that evaluates the right hand side of the first order
differential equation, a vector tspan that specifies the interval of [t0 , tf ] and a vector
y0 of the initial conditions y(t = t0 ). The output is a column vector T of time points
and a solution array Y that contains the corresponding solutions y(t) of the ODE at
the time instances in T. The structure of optional parameters options allows you to
modify the default integration properties such as relative and absolute tolerances of the
approximation. You create or modify the property structure with odeset by specifying
the corresponding name and value pairs.
with initial condition y(1) = 1 in closed analytic form either by hand or with the
help of the symbolic toolbox.
y(t) = . . .
2) mandatory: Solve the same differential equation with the solver ode45 for the
interval t ∈ [1, 2]
3) mandatory: Generate the solution vector of the ODE in 6 with ode45 for two
levels of accuracy with both relative ’RelTol’ and absolute ’AbsTol’ tolerances
of = 1e − 6 (case 1) and 1e − 8 (case 2) respectively (odeset). Plot the numerical
solutions together with the closed form analytical solution. Use a plot style with
markers (e.g. ’r-o’) in order to visualize the individual data items in the solution
vectors T and T the same differential equation. How many intermediate data points
are generated by the approximate solutions of the ODE for the two levels of accuracy.
4) mandatory: Plot the evolution of the absolute error between the analytical and
numerical solutions w.r.t. time in a separate figure.
5) mandatory: Generate the numerical solution of 6 with ode45 and ode23 and com-
pare the absolute error to the analytical solution of both solvers.
The ODE solvers only handle first-order differential equations. In order to solve higher-
order ODEs numerically the explicit ODE equation is reformulated as an equivalent sys-
tem of first-order differential equations of the form y ′ = f (t, y) An ordinary n-th order
differential equation y(n) = f (t, y, y ′ , . . . , y (n−1) ) is equivalent to a system of first-order
equations by making the substitutions y1 = y, y2 = y ′ , . . . , yn = y (n−1) to obtain a system
of n first-order ODEs.
y1′ = y2
y2′ = y3
...
yn′ = f (t, y1 , y2 , ..., yn )
with initial condition y(0) = 1, ẏ(0) = 0 in closed analytic form either by hand or
with the help of the symbolic toolbox.
y(t) = . . .
8) mandatory: Convert the differential equation into a system of two first order dif-
ferential equations and solve it numerically with the solver ode45 for the interval
t ∈ [0, 2π] for ω = 1
9) mandatory: Plot the phase portrait of the analytical and numerical solution in the
state space y(t), ẏ(t).
11) optional: Plot the phase portrait of the numerical solution in the state space
y(t), ẏ(t).
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 5
Simulink Basics
Simulink is a software package (from MATLAB) for modeling, simulating, and analyzing
dynamical systems. It supports linear and nonlinear systems, modeled in continuous time,
sampled time, or a hybrid of the two. Systems can operate at multiple rates, i.e., have
different parts that are sampled or updated at different rates.
For modeling, Simulink provides a graphical user interface (GUI) for building models as
block diagrams, using click-and-drag mouse operations. Simulink includes a comprehensive
block library of sinks, sources, linear and nonlinear components, and connectors. You can
customize and create your own blocks. Models are hierarchical. This approach provides
insight into how a model is organized and how its parts interact. After a model is defined,
it is possible to simulate it, using a selection of numerical integration methods, either from
the Simulink menus or by entering commands in MATLAB’s command window.
The dynamics of the mass spring damper system shown in figure 2 are described by the
second order linear differential equation:
in which x(t) denotes the displacement of the mass from the springs equilibrium position,
ẋ(t) is velocity of the mass and F (t) is the external force applied to the mass. The dyanmics
parameters of the system are the mass m, the damping d and the spring constant k.
k d
In the following exercises you are supposed to implement the Simulink model of the mass
spring damper system, adding blocks in an incremental fashion.
and simulate the model with initial condition x(0) = 1, ẋ(0) = 0 (double click on
second integrator block and specify the property initial condition to 1. Simulate
the system for a time interval t ∈ [0, 20]. Label the blocks with x(t) and ẋ(t)
according to their output. Plot the temporal evolution of the state x(t) with a
Scope block from the library Sinks. The signal x(t) should be sinusoidal as shown
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 7
in figure 4.
2) mandatory: Augment the model by a two static gains 1/m and k with parameters
m = 2, k = 3 to obtain the mass spring system.
k
mẍ(t) + kx(t) = 0 ≡ ẍ(t) = − x(t) (3)
m
5) optional: Plot the temporal evolution of the state vector x(t), ẋ(t) with an XY
Graph block from the library Sinks.
creates a time-series object. data denotes an array of samples and time a vector of time
instances at which the samples have been collected. The code below generates and plots
a sinusoidal signal at frequency ω with a sample time specified in milliseconds and a
duration t ∈ [1 1000] ms.
t=1:1000;
omega=0.05;
x=sin(omega*t);
ts1 = timeseries(x,t);
ts1.Name = ’signal’;
ts1.TimeInfo.Units = ’milliseconds’;
plot(ts1)
6) mandatory: Export the simulation data as a time series object to the workspace
variable x with a block To Workspace from the Sinks library and plot the time
series object in Matlab.
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 8
7) mandatory:
Generate a time series object in the workspace with the command timeseries
according to u = u0 sin(ωt) , t ∈ [0, 20]. Modify your model such that the signal u
is read from the workspace rather than from the signal generator.
8) optional: Simulate the model from the command line with sim rather than from
the Simulink GUI with the setting ’SaveState’ set to ’on’.
9) optional: Plot the two components of the state vector x and ẋ over time by extrac-
ting the corresponding data from the simulation output.
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 9
Previously, the Simulink model is composed as a series of two integrators with block
inputs ẍ(t) and ẋ(t). This exercise models the LTI system in Simulink with the equivalent
transfer function resp. state space model.
X(s)
=
U (s)
with error signal e = r − x. Simulate the closed loop response for a square wave
reference signal r with a frequency of 0.1 Hz (Signal Generator).
4) optional: Tune the controller gains with the PID tool (see assignment Control
System Toolbox II). Note: from the Simulink PID block parameter window you
switch to the PID tool with the button Tune.
5) mandatory: Compute the state space model A, b, c, d = 0 that corresponds to the
second order ordinary differential equation (1) with state vector x = (x1 , x2 ) = (x, ẋ)
ẋ = Ax + bu (3)
y = cx (4)
where the state matrix A, the input vector b, the output vector c are matrices or
vectors of appropriate dimensions,
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 10
u = −kx + wr (5)
h i
with a gain vector k = 10 10 and w = 10. In order to implement a state feedback
controller u = −kr modify the
# state space model to include both states x1 , x2 as
1 0 0
" " #
outputs to matrix C = and the feed-through vector D = . Simulate the
0 1 0
closed loop response for a square wave reference signal with a frequency of 0.1 Hz.