[go: up one dir, main page]

0% found this document useful (0 votes)
127 views12 pages

Lab 03 Simulink 2018 Part 2

Uploaded by

Reem Gheith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views12 pages

Lab 03 Simulink 2018 Part 2

Uploaded by

Reem Gheith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

AIN SHAMS UNIVERSITY

FACULTY OF ENGINEERING
MECHATRONICS ENGINEERING DEPARTMENT
Credit Hours Programs
Fall Semester, 2018

MCT 456: Dynamic Modeling and Simulation


Lab. 3: Modeling with SIMULINK Part II

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)

Finding mass-spring-damper system response using integrators and system states.


Page 1 of 12
2. Using Transfer function block
The transfer function is used to describe the physical system model in S-domain based on the
relation between input and output
Using the mass spring damper example 𝑚𝑥 ̈ + 𝑏𝑥 ̇ + 𝑘𝑥 = 𝑓(𝑡), the transfer function is:
𝑋(𝑠) 1
𝐺(𝑠) = = 2
𝐹(𝑠) 𝑚𝑠 + 𝑏𝑠 + 𝑘
In this form, the transfer function numerator is 1 and the denominator is ms 2+bs+k. The transfer
function block in Simulink Continuous Library can be used to solve the system response.
o When you double click on the transfer function block, you will find the following function
block parameters.

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 𝒔𝟐+𝟑𝒔+𝟐

Then, the Numerator coefficients should be [1 1]

And, the Denominator coefficients should be [1 3 2]

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

𝒙̇ 𝑨 𝒙 𝑩 𝒖

This matrix can be represented in this standard from


𝒙̇ = 𝑨𝒙 + 𝑩𝒖
 From 𝑥1 = 𝑦, the system response can be represented in this form
𝑥1 0 𝑢1
𝑦 = [1 0 0] [𝑥2 ] + [0] [𝑢2 ]
𝑥3 0 𝑢3

𝒚 𝑪 𝒙 𝑫 𝒖

This matrix can be represented in this standard from


𝒚 = 𝑪𝒙 + 𝑫𝒖
The matrices A, B, C and D are used to describe the system model in the state-space representation.

 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:
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
………………………………………………………………………………………………………....

Using Transfer function:

Page 4 of 12
…………………………………………………………………………………………………………
…………………………………………………………………………………………………………

Using state-space block:


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………

Step Force Input

Differential
equation response

Transfer function State-space


response model 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

2.1 The frequency response of the system below


x
m
k c y

1- The transfer function for the system shown in Figure must be found :
Mathematical model for this system as following:

𝒎𝒙̈ = −𝒄(𝒙̇ − 𝒚̇ ) − 𝒌(𝒙 − 𝒚)


𝒎𝒙̈ + 𝒄𝒙̇ + 𝒌𝒙 = 𝒄𝒚̇ − 𝒌𝒚
Take Laplace for equation (1) assuming zero initial condition
mS 2 X ( S )  cSX ( S )  kX ( S )  cSY ( S )  kY ( S )
c k c k
S 2 X (S )  SX ( S )  X ( S )  SY ( S )  Y ( S )
m m m m
S X ( S )  2 n SX ( S )   n X ( S )  2 n SY ( S )   n Y ( S )
2 2 2

X ( S )[S 2  2 n S   n ]  Y ( S )[2 n   n ]


2 2

[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

amplitude_ ratio    (3)


Y ( j ) ( n  2 ) 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:

f) Put the values of numerator and denominator coefficient as following:-


Numerator coefficient:[ 2 *  * n  n ]
2

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

2.2 Spectrum Analyzer


a) Open New Simulink model.
b) Drag digital spectrum analyzer. Simulink>DSP System Toolbox>Sinks>spectrum
Analyzer.
c) Drag a sinusoidal input into the model. Simulink->sources->sine wave. Set the sine wave
amplitude to 1, frequency to 10Hz and 0.01 sample time.
d) Run the model and the frequency spectral for the signal

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

What are the differences and comment on the results?


…………………………………………………………………………………………………………
…………………………………………………………………………………………………………
……………………………………………………………………………………………………........

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

3. Create and mask subsystems and model referencing

 Create and Mask subsystem:

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
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………

4. Difference between simulation modes (Normal, Accelerator, External, Software in-the-


loop, etc.)
In general, you must trade off simulation speed against flexibility when choosing either Accelerator mode,
Rapid Accelerator mode or Normal mode.
You can switch the simulation mode by clicking on the NORMAL arrow and choose the required mode.
Normal mode offers the greatest flexibility for making model adjustments and displaying results, but it runs
the slowest.
Accelerator mode lies between Normal and Rapid Accelerator modes in performance and in interaction with
your model. If your model has 3-D signals, use Normal or Accelerator mode. Accelerator mode does not
support runtime diagnostics.
Rapid Accelerator mode runs the fastest, but this mode does not support the debugger or profiler, and works
only with those models for which C code is available for all of the blocks in the model. In addition, Rapid
Accelerator mode does not support 3-D parameters and sinks.
External mode is used to connect your Simulink block diagram to your application that runs the model on the
target hardware. The block diagram becomes a user interface to the real-time application. By changing
parameters in the Simulink blocks, you also change parameters in the real-time application.
Hardware-in-the-loop (HIL) simulation is a type of real-time simulation. You use HIL simulation to test your
controller design. HIL simulation shows how your controller responds, in real time, to realistic virtual stimuli.
You can also use HIL to determine if your physical system (plant) model is valid.
In HIL simulation, you use a real-time computer as a virtual representation of your plant model and a real
version of your controller. The figure shows a typical HIL simulation setup. The desktop computer
(development hardware) contains the real-time capable model of the controller and plant. The development
hardware also contains an interface with which to control the virtual input to the plant. The controller hardware
contains the controller software that is generated from the controller model. The real-time processor (target
hardware) contains code for the physical system that is generated from the plant model.
There are two other simulation modes which are: Software-In the-Loop (SIL) and Processor-In the-Loop
(PIL) simulations

Page 12 of 12

You might also like