0% found this document useful (0 votes)
2 views

Simulink_I

The document outlines mandatory exercises for scientific programming in Matlab, focusing on the numerical solution of ordinary differential equations (ODEs) using methods like Euler and Runge-Kutta. It includes instructions for implementing these methods in Matlab, using the ode45 solver, and creating Simulink models for dynamic systems. Additionally, it covers the use of time series objects and feedback control systems in Simulink.

Uploaded by

mytv.clientes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Simulink_I

The document outlines mandatory exercises for scientific programming in Matlab, focusing on the numerical solution of ordinary differential equations (ODEs) using methods like Euler and Runge-Kutta. It includes instructions for implementing these methods in Matlab, using the ode45 solver, and creating Simulink models for dynamic systems. Additionally, it covers the use of time series objects and feedback control systems in Simulink.

Uploaded by

mytv.clientes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Scientific Programming in Matlab

Version 1.2 Scientific Programming: Simulink Page 1

Reminder: MANDATORY exercises have to be completed and PRESENTED during the


class. It is your obligation to notify a teaching assistant once you completed the code and
to present it to him. The code has to be shown during the class, later submissions by
email or in person are not accepted.

Numerical Solution of Ordinary Differential Equations


Ordinary differential equations (ODE) contain a function of a single independent variable
and its derivative. In its implicit form an ODE is written as:

F (x, y, y ′ , ..., y n ) = 0 (1)

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.

Numerical integration numerically approximates the solution of ODEs which is in the


absence of exact analytical solutions are often sufficient for applications such as the simu-
lation of dynamic systems. Without loss of generality, a first order differential equation
of the form

y ′ (x) = f (x, y (x)) , y (x0 ) = y0 (2)

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

yn+1 ≃ yn + hf (xn , yn ) (4)

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:

• Explicit ODEs of the form y ′ = f (t, y)

• Linearly implicit ODEs of the form M (t, y)y ′ = f (t, y), where M (t, y) is a matrix

• Fully implicit ODEs of the form f (t, y, y ′ ) = 0

In the following we restrict the exercise to explicit ODEs.


2
Source: By HilberTraum - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.
php?curid=64366870
Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 3

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.

1) mandatory: Solve the differential equation

ẏ(t) = t · y(t) (6)

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.

6) optional: Solve the differential equation

ẏ(t) = t · y(t) + cos(t) (7)

and initial condition y(1) = 1 with a numerical approximation method (ode45).


Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 4

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 )

7) mandatory: Solve the second order differential equation

ÿ(t) + ωy(t) = 0 (8)

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

10) optional: Solve the second order differential equation

ÿ(t) + ωy(t) = cos(ωr t) (9)

with initial condition y(0) = 1, ẏ(0) = 0 and ω = 2 and ωr = 1.5 numerically.

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:

mẍ(t) + dẋ(t) + kx(t) = F (t) (1)

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

Figure 2: Mass spring damper system

In the following exercises you are supposed to implement the Simulink model of the mass
spring damper system, adding blocks in an incremental fashion.

1) mandatory: Implement the Simulink model mass_spring_damper.slx shown in


figure 3 with two integrators connected in series and a negative feedback connection
(Gain) to simulate the second order differential equation

ẍ(t) + x(t) = 0 (2)


Scientific Programming in Matlab
Version 1.2 Scientific Programming: Simulink Page 6

Figure 3: Simulink model for ODE

Figure 4: Scope of signal x(t) for x(0) = 1

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

3) mandatory: Augment the model by a friction term dẋ(t) with d = 1


d k
mẍ(t) + dẋ(t) + kx(t) = 0 ≡ ẍ(t) = − ẋ(t) − x(t) (4)
m m

4) mandatory: Augment the model by a driving force u = u0 sin(ωt) generated by a


signal generator block located in the library sources block with u0 = Fm0 = 1
and ω = 0.3 ∗ 2π. Set the initial state x(0) = 0, ẋ(0) = 0 to zero. What is the
ulitmate amplitude of the forced motion for t → ∞ after the decay of the initial
transient response?
d k
mẍ(t)+dẋ(t)+kx(t) = F0 sin(ωt) ≡ ẍ(t) = − ẋ(t)− x(t)+u0 sin(ωt) (5)
m m

5) optional: Plot the temporal evolution of the state vector x(t), ẋ(t) with an XY
Graph block from the library Sinks.

Time Series Objects


A timeseries object contains data and time information within its properties that describ
signals of dynamic processes.
ts = timeseries(data,time)

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

Simulink Blocks and Feedback Control


This exercise is still concerned with the mass spring damper system of the previous exercise
described by:
mẍ(t) + dẋ(t) + kx(t) = F (t). (1)

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.

1) mandatory: Compute the transfer function X(s) U (s)


that corresponds to the second
order ordinary differential equation (1). X(s) denotes the Laplace transform of x(t)
and U (s)denotes the Laplace transform of Fm(t)

X(s)
=
U (s)

2) mandatory: Open the Simulink model mass_spring_damper.slx with two inte-


grators connected in series from the previous exercise and save it by the filename
mass_spring_damper_tf.slx. Replace the two integrators by a single equivalent
transfer function block Transfer Fcn from the library Continuous. Simulate the
model with parameters m = 2, d = 1, k = 3 and initial conditions x(0) = 0, ẋ(0) = 0
and input u(t) = u0 cos(ωt) t ∈ [0, 20].
3) mandatory: Generate an error feedback control system by adding a PID controller
with gains Kp = 10, Ki = 5 and Kd = 3 and a unit feedback loop.
Z
u = −Kp e − Ki edt − Kd ė (2)

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

6) mandatory: Open the Simulink model mass_spring_damper_tf.slx save it as


mass_spring_damper_ss.slx and replace the transfer function by the equivalent
state space model block State Space from the library Continuous. Simulate the
model with parameters m = 2, d = 1, k = 3 and initial conditions x(0) = 0, ẋ(0) = 0
and input u(t) = u0 cos(ωt) t ∈ [0, 20].

7) optional: Generate a state feedback control system by adding a state feed-back


loop

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.

You might also like