Lab 03 Simulink 2018 Part 2
Lab 03 Simulink 2018 Part 2
FACULTY OF ENGINEERING
MECHATRONICS ENGINEERING DEPARTMENT
Credit Hours Programs
Fall Semester, 2018
Contents
1- Modeling with Simulink: Solving differential equations of engineering models, State-Space and
transfer function blocks.
2- Spectrum Analyzer and Frequency response
3- Create and mask subsystems and model referencing
4- Difference between simulation modes (Normal, Accelerator, External, Software in-the-loop, etc.)
Background:
Simulink is a block diagram environment for multi-domain simulation and Model-Based Design. It
supports system-level design, simulation, automatic code generation, and continuous test and verification
of embedded systems. Simulink provides a graphical editor, customizable block libraries, and solvers for
modeling and simulating dynamic systems. It is integrated with MATLAB, enabling you to incorporate
MATLAB algorithms into models and export simulation results to MATLAB for further analysis.
1. Modeling with Simulink: Solving differential equations of engineering models, State-Space and
transfer function blocks.
Integrators and derivatives, Transfer function and state-space blocks are available in (continuous) library.
Physical mathematical modeling is the process of writing a differential equation to describe a physical
situation and behavior of the real system. e.g: The behavior of mas-spring-damper system can be described
based on this differential equation:
𝑚𝑥̈ + 𝑏𝑥̇ + 𝑘𝑥 = 𝑓(𝑡)
Where m: mass in kg, b: damping coefficient, k: spring stiffness and f(t): applied input force to the system
In order to solve this differential equation and find the system response there are different methods either
using integrators and derivatives, transfer functions or state space:
1. Using integrators and derivatives to integrate or derivative the internal states of the system such
as acceleration, velocity, and position:
You need to reformulate the equation to be:
1
𝑥̈ = (−𝑏𝑥̇ − 𝑘𝑥 + 𝑓(𝑡))
𝑚
Then integrate the 𝑥̈ twice to get the system response x(t) as shown in the following figure (Figure
4.1)
o The coefficients should be in descending order according to the order of the s power. For
example, the Numerator coefficients in previous example should be [1] and the Denominator
coefficients should be [m b k]
o If you want to represent the second order model of the mass-spring-damper in the standard
form as follows:
𝑋(𝑠) 𝑘𝜔𝑛2
𝐺(𝑠) = = 2
𝐹(𝑠) 𝑠 + 2𝜁𝜔𝑛 𝑠 + 𝜔𝑛2
Where: 𝜁: is the damping ratio, and 𝜔𝑛 : system nature frequency
In this case, the Numerator coefficients in previous example should be [𝑘𝜔𝑛2 ] and the
Denominator coefficients should be [1 2𝜁𝜔𝑛 𝜔𝑛2 ]
Note you need to write it in vector form by leaving space between each parameter.
𝒔+𝟏
Example 1: A transfer function is given by 𝒔𝟐+𝟑𝒔+𝟐
Page 2 of 12
3. State Space representation
To understand the state space representation, we will go through this example:
Consider the differential equation with no derivatives on the right hand side (the input
doesn’t have derivative components). For explanation, we'll use a third order equation,
thought it generalizes to nth order in the obvious way.
𝑦⃛ = −𝑎3 𝑥1 − 𝑎2 𝑥2 − 𝑎1 𝑥3 + 𝑏0 𝑢
For such systems (no derivatives of the input) we can choose as our n state variables
the variable y and its first n-1 derivatives (in the case of third order differential equation,
the system has three states: y and the first two derivatives)
𝑥1 = 𝑦, 𝑥2 = 𝑦̇ , 𝑥3 = 𝑦̈
Taking the derivatives of the states, we can develop our state space model
𝑥̇ 1 = 𝑥2 = 𝑦̇
𝑥̇ 2 = 𝑥3 = 𝑦̈
𝑥̇ 3 = 𝑦⃛ = −𝑎3 𝑥1 − 𝑎2 𝑥2 − 𝑎1 𝑥3 + 𝑏0 𝑢
These derivatives of the system states can be represented in the following form using
matrices:
𝑥̇ 1 0 1 0 𝑥1 0 𝑢1
[𝑥̇ 2 ] = [ 0 0 1 ] [𝑥2 ] + [ 0 ] [𝑢2 ]
𝑥̇ 3 −a3 −a2 −a1 𝑥3 b0 𝑢3
𝒙̇ 𝑨 𝒙 𝑩 𝒖
𝒚 𝑪 𝒙 𝑫 𝒖
Drag state space block from library to SIMULINK desktop and double click on it.
Page 3 of 12
Using previous example, the state space parameters will be presented as follows:
A=[0 1 0; 0 0 1; -a3 –a2 –a1] B=[0; 0; bo] C=[1 0 0] D=[0; 0; 0]
In case of mass-spring-damper system, 𝑚𝑥 ̈ + 𝑏𝑥 ̇ + 𝑘𝑥 = 𝑓(𝑡), second order system:
0 1 𝑥 0
𝑥̇ 𝑏 ] [ 1 ] + [ 1 ] 𝑓(𝑡) = 𝑨𝒙 + 𝑩𝒖
𝒙̇ = [ 1 ] = [ 𝑘 𝑥2
𝑥̇ 2 − −
𝑚 𝑚 𝑚
𝑥1
[ ]
𝒚 = 𝑥 = 1 0 [𝑥 ] = 𝑪𝒙 + 𝑫𝒖
2
The system states are: 𝑥1 = 𝑥, 𝑥2 = 𝑥̇ where is x is displacement and 𝑥̇ is linear velocity. The
system matrices will be presented as following:
In case of second order mass-spring-damper, the A, B, C and D matrix for state-space model, will be
as follows:
A=[0 1; -k/m –b/m] B=[0; 1/m] C=[1 0] D=0
Example:
Write down the equation of 1-DOF mass-spring damper system and model it using MATLAB-
SIMULINK; then, calculate the mass response (x) if a unit step-input was applied to the system. (The
mass = 250kg, spring stiffness=20 N/mm and damping coefficient=0.5 N.s/mm, assume the mass is at
rest initially).
Note: All the units should be converted to SI units (kg, m, N, seconds, etc.)
Show the system response using scope and:
Using integrators and derivatives:
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
………………………………………………………………………………………………………....
Page 4 of 12
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
Differential
equation response
Comment on the results and explain if the system is under or over damping. Then calculate the over
critical damping coefficient (critical damping=2mωn =2√𝑘𝑚)
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........
Test the system at different damping values (two values overdamping, two values underdamping and
at the critical damping value. Comment on the results.
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........
Add coulomb friction to the previous model and compare the results.
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........
If the step-input was replaced with sinusoidal excitation of 10 mm amplitude and 5 rad/sec frequency,
what will be the response?
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........
Page 5 of 12
Differential equation
Finding mass-spring-damper system response under step response or Sine wave execution using integrators
and system states, transfer function and state-space.
Exercise 1:
Write down the differential equation that describe the following systems. Then assume values for the system
parameters and use Simulink (Using integrators and derivatives and/or Transfer function blocks) to show the
system response in case of step and sinusoidal inputs. Use information you studied in sheet 2 and in control
course to solve these problems.
Page 6 of 12
2. Spectrum Analyzer and Frequency response
1- The transfer function for the system shown in Figure must be found :
Mathematical model for this system as following:
[2 n S n ]
2
X (S )
2 (2)
Y ( S ) [ S 2 n S n 2 ]
Frequency domain (replace each S with jw)
X ( j ) ( n ) 2 (2 n ) 2
2
X ( j ) 2 2
phase_ angle ( ) tan 1 tan 1 2 n2 (4)
Y ( j ) n n
To find the values of n and , we use identification technique to measure the response of x and y
(where y is a sinusoidal input) in time domain or in frequency domain. Then, we find the amplitude ratio
and phase angle value at the frequency value ( ) of y. then, we use these values in solving equation 3
and 4 to find n and .
2- After we found the values of n and , we can found the model transfer function values.
3- To check the model on simulink, we follow these steps:-
a) open simulink
>> simulink
b) open a new model
File-> New-> Model.
Page 7 of 12
c) Open a new model
d) Drag a transfer function into Model window
Simulink->continous->transfer function
e) Specify the order of the transfer function and its value
[2 n S n ]
2
X (S )
2
Y ( S ) [ S 2 n S n 2 ]
By click a left double click on transfer function block the following window will open:
Denominator coefficient:[1 2 * * n n ]
2
g) Drag a sinusoidal input into the model and adjust its value with amplitude and
frequency.
Simulink->sources->sine wave
With Amplitude=1 and 10rad/sec frequency
h) Drag a spectrum analyzer for frequency response
Simulink->Simulink Extras->Additional sinks-> spectram Analyzer.
i) Connect spectram analyzer with with sin wave and transfer function output as shown
in figure, then press on start simulation icon.
Page 8 of 12
Before running the program, define the following variables for mass, spring stiffness and
damping coefficient and calculate the damping ratio and natural frequency.
m = 0.1; % kg
k = 1500; % N/m
c = 1; % N/(m/s)
Change the sine wave with chirp signal from sources and then set the frequency ranges of the
chirp signal from 0.01Hz to 100Hz and increase simulation time to 1000 seconds and
comment on the change happens in the model results
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………........
e) Replace the sine wave with square pulse generator from sources. Change the pulse
Generator settings from time based to sample based and set the sample time to 0.01sec
Page 9 of 12
2.3 Frequency Analysis and Transmissibility of example in page 7 (2.1)
Use zero order hold to convert the continuous output of the transfer function to discrete and set the
sampling time to 0.01 sec and the simulation time to 1000 seconds.
Calculate the nature frequency in hertz and compare it with the peak amplitude frequency you
get it from the spectrum analyser
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........
Find the spectrum and frequency response for the models in Exercise 1 using their transfer functions
Subsystems allow you to create a hierarchical model comprising many layers. A subsystem is a set of blocks
that you replace with a single Subsystem block. As your model increases in size and complexity, you can
simplify it by grouping blocks into subsystems.
You can create a subsystem using one of these approaches:
Add a Subsystem block to your model, and then open the block and add blocks to the subsystem window.
Select the blocks that you want in the subsystem, and from the right-click context menu, select Create
Subsystem from Selection.
Select the blocks that you want to group them in the subsystem, select Diagram > Subsystems & Model
Reference > Create Subsystem from Selection.
Example: In the previous example (Figure4.2), create a subsystem for the differential equation part:
…………………………………………………………………………………………………………………
………………………………………………………………………………………………………………….
Page 10 of 12
If you double click on the created subsystem, you will see the following diagram
Masking is the ability to create custom blocks that have the same look and feel as built-in blocks. You can
mask subsystem by right click on the subsystem, select Mask > create Mask.
Also, you can add mask icon image by right click on the subsystem, select Mask>Add Icon Image
Explorer the mask editor of the system (by right click on the subsystem, select Mask > create Mask) and
navigate around the different options. Click on Help button in the Mask Editor window and know about these
options.
…………………………………………………………………………………………………………………
………………………………………………………………………………………………………………….
Add an icon image to your subsystem
…………………………………………………………………………………………………………………
………………………………………………………………………………………………………………….
Model referencing
You can include one model in another by using a Model block, which represents a reference to another
model, called a referenced model. For simulation and code generation, the referenced model effectively
replaces the Model block that references it. The model that contains a referenced model is its parent model.
To create a reference to a model (referenced model) in another model (parent model):
1- If the folder containing the referenced model you want to reference is not on the MATLAB® path,
add the folder to the MATLAB path.
2- In the referenced model:
Set Configuration Parameters > Model Referencing > Total number of instances allowed
per top model to:
i. One, if the hierarchy uses the model at most once
ii. Multiple, to use the model more than once per top model. To reduce overhead,
specify Multiple only when necessary.
iii. Zero, which precludes referencing the model
3- Create an instance of the Model block in the parent model by dragging a Model block instance
from the Ports & Subsystems library to the parent model. The new block is initially unresolved
(specifies no referenced model).
4- Open the new Model block's parameter dialog box by double-clicking the Model block.
5- Enter the name of the referenced model in the Model name field. This name must contain fewer
than 60 characters.
6- Click OK or Apply.
Create a referenced model for equation: x=3sin 5t and insert the referenced model in parents model where
y=5x+3
Page 11 of 12
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
Page 12 of 12