Lab 1: Systems Modeling in Simulink Using Differential Equations
Lab 1: Systems Modeling in Simulink Using Differential Equations
Equations
Learning Outcomes
After completing this lab with the aid of MATLAB and Simulink, the student will be able to
Model the differential equation of a physical system in Simulink.
Numerically solve the differential equation using an appropriate solver in Simulink.
Develop useful insights about a physical system from its numerical solution.
1.1 Modeling and Simulation
Modeling and simulation (M&S) at simple terms is a substitute for physical experimentation,
in which computers are used to calculate the results of some physical phenomenon. As it is
apparent from its name "Modeling and simulation" firstly a computer is used to build a
mathematical model which contains all the parameters of physical model and represent
physical model in virtual form then conditions are applied which we want to experiment on
physical model, then simulation starts i.e. we leave on computer to calculate the results of
those conditions on mathematical model. In this way, actual experimentation can be avoided
which is costly and time-consuming instead of using mathematical knowledge and computer's
computation power to solve real-world problems cheaply and in a time-efficient manner.
[CITATION Wik19 \l 1033 ]
1.2 Introduction to Simulink
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.
Here, Δt is the step size for t, so each point for t has a difference of Δt from the previous
point. There is a detailed mathematical analysis of how this method works, but that is out of
the scope of this course.
Several numerical solvers are available in MATLAB that are quite powerful than the Euler’s
method explained above. We will mostly use ode45 solver to numerically solve the models in
this course. It uses the Runge-Kutta method to solve the differential equations and works well
for most practical purposes.
2
We know the relations between current and voltage for a resistor and a capacitor.
v R(t ) d v (t )
i R ( t )= , i ( t )=C C
R dt
Applying KVL around the loop, and substituting the above relations, we get the following
equation if the initial voltage across the capacitor is zero.
v ¿ (t )=v R ( t ) +v C ( t )
v ¿ (t )=i ( t ) R+ v C ( t )
From here, we perform some mathematical manipulations and get to the following result.
dv C ( t ) 1
= ( v ( t )−v C ( t ) )
dt RC ¿
Now we can use this differential equation to build a block diagram model in Simulink and
solve the model numerically to find out how vC varies over time.
1.4.1 Implementation in Simulink
Now we can use this differential equation to build a block diagram model in Simulink and
solve the model numerically to find out how V C varies over time.
1. Open Simulink by either typing Simulink in the MATLAB command window or
pressing the Simulink button as shown in Figure 1 -2.
2. Select the “Blank model” option from the Simulink startup panel. A blank model looks
like as shown in Figure 1 -3. Save the model by pressing Ctrl+S, in a new folder in your
working directory.
3
Figure 1-3: A blank model in Simulink
3. As shown in Figure 1 -4 open the Library Browser by clicking its button in the top panel.
4. From the Library Browser, you can insert the blocks you need to build a model in
Simulink. The blocks are available in the navigation pane that can be seen on the left side
of the Library Browser window, as shown in Figure 1 -5.
4
Figure 1-5: Library Browser in Simulink
Insert the following blocks in your model by going to the location of each block and then
right-click -> Add block to model. Note that the blocks are arranged in alphabetical order
in the navigation pane. Besides the location given in the following table, you can find some
of these blocks in the “Simulink -> Commonly Used Blocks” section.
5
Scope Simulink -> Sinks
The blocks and signals have been renamed in Figure 1 -6 according to the differential
equation of the RC circuit.
dv C ( t ) 1
= ( v ( t )−v C ( t ) ) STYLEREF 1¿ 1 SEQ Equation ¿ ARABIC ¿ 1 1
dt RC ¿
1
vC =∫ ( v (t )−v C (t ) ) dt +v C 0
RC ¿
The V ¿ block at the left goes to the subtract block, along with the calculated vC . The
difference is divide by RC. Now this term is equal to the right side of Equation 1-1.
6
This signal is then integrated to get vC . The initial condition, vC 0 , needed with this integration
is set in the integrator’s Properties window, which can be opened by double-clicking it. The
resulting signal is now vC .
1.3.2 Configuring the Solver
In the Simulink window, open Simulation -> Model Configuration Parameters. Set the
Type of solver to Fixed-step and Solver to ode4 (Runge-Kutta). Check that the simulation
start and stop times are 0 and 10 respectively. Set the time step to 0.01 s. It is suggested that
you do not change any other settings. Figure 1 -7 shows this configuration in the solver.
The simulation should compile quickly. You can observe its progress at the bottom of the
block diagram window. Wait until it is 100%. Now double-click the Scope to see the plot of
V C against time. Your graph should be similar to the one in Figure 1 -9.
7
Figure 1-9: vC against time for a series RC circuit
8
After these commands, you should see a MATLAB Figure similar to the following diagram.
9
dT
=k ( T a −T )
dt
1. Pre-Lab Consider a cup of hot tea left to dissipate its heat to the environment and its
temperature was measured at regular intervals of 30 seconds. The record of temperature
with time is as follows. Use this data to estimate k, T a and T 0, the initial temperature for
this cup. The initial temperature is clearly evident from the data. To estimate k and T a, you
can plot this data and find its gradient at several points. Then substitute the pairs of the
values of gradient ( dTdt ) and temperature ( T ) in the above differential equation to get
simultaneous equations in k and T a.
[5 points]
k =¿0.01667
T a=¿ 20
T 0=¿80
Time ( s) Temperature (° C)
0 80.0
30 56.4
60 42.1
90 33.4
120 28.1
150 24.9
180 23.0
210 21.8
240 21.1
270 20.7
300 20.4
Table 1-1: Temperature measurements of a hot cup
2. Consider the same cup of tea now initially at a temperature of 90 ° C cooling down in a
surrounding with an ambient temperature of 25 ° C. You will use Simulink to predict the
temperature of the tea as it cools down. Implement the model with a fixed step Runge-
Kutta solver, having a time step ( Δ t) of 10. Run the simulation by gradually reducing the
time step by a factor of 10 so that eventually the graph does not have any visible edges and
the simulation completes within 5 s in real-time. Take a screenshot of the graph and paste
here. [5
points]
10
3. Use your simulation in part 2 to estimate the temperature of tea after 15 s for the following
time steps. You can use the zoom feature to find the exact point on the curve. Give your
answer correct to 2 decimal places. [2 points]
(i) Δt=10s
(ii) You chose in part 2 to make the graph look smooth.
(i) T ( 15 ) =¿75.78
(ii) T ( 15 ) =¿74.90
4. In part 2, you have found the numerical solution of the differential equation. Its general
analytic solution is given as the following equation. Find the temperature of the cup after
15 seconds using this equation. Give your answer correct to 2 decimal places. [2 points]
T ( t )=T a + ( T 0−T a ) e−kt
T ( 15 ) =¿75.62
5. Compare your two answers to part with the analytical solution in part 4 and explain
whether smaller or larger time steps are better for this simulation? [2 points]
11
The smaller time steps are better as the result we obtained is more accurate and is
approximately equal to the calculated temperature after 15 seconds.
As you know, one of the first steps in modeling a mechanical system is to define a coordinate
system. In the above diagram, let's consider the origin to be the position of the right edge of
the mass, and the positive direction of motion to be towards the right.
To build a mathematical model of the mass-spring system, we use Newton's second law. We
sum up all the forces acting on the mass to find the net force and then calculate its
acceleration, which can be integrated twice to calculate the mass's displacement. Several
forces are acting on the block.
Force by the spring: A spring tends to recover its initial shape if deformed within the elastic
limit. So the spring pushes the mass away when compressed and pulls it when extended.
According to Hooke's law, the magnitude of such a restoring force is proportional to the
amount of compression or extension (in this case the displacement of mass), and the force
acts in order to oppose the displacement.
Pre-Lab Write down the equation of Hooke's law in this case. [2 points]
Force due to the damper: Currently, the damper is the only source of friction experienced by
the mass and it tends to slow it down no matter which way it is moving i.e. the friction acts in
12
a direction to oppose the velocity of the mass. Its magnitude can be assumed to be
proportional to the speed of the mass.
Resultant force: The spring and damper forces and a horizontal external force F altogether
sum up to give the net force acting on the mass. This net force then accelerates the mass
according to Newton's second law.
F total=ma=F + F s+ F d
To model this system in Simulink, we must first write its differential equation in terms of
displacement and its derivatives. Then make the highest derivative the subject of the equation
because once we have the highest derivative, we can integrate it an appropriate number of
times to evaluate the displacement of the block. Let's perform this procedure step-by-step as
follows.
dx dv d 2 x
v= , a= = 2
dt dt dt
1. Pre-Lab Using Hooke's law, write down the relationship between the spring force, F S, and
the extension in the spring, x, which is also the displacement of the block. You may use k
for the spring constant. [2 points]
2. Pre-Lab Write down the relationship between the force exerted on the block by the
damper, F D , and the velocity of the spring, v. Use b as the damping constant. [2
points]
F D =−by
3. Pre-Lab Substitute the forces exerted on the block by the spring and the damper in the
expression for F total given above. Write down the differential equation in terms of
displacement and its derivatives, making its highest derivative the subject of the equation.
[3 points]
13
ma=F−kx−bv
❑ ❑
1
x=
m∫❑
❑ (∫ ❑(( F−kx ) dt−bx )dt )
❑
4. Pre-Lab To calculate the spring constant k, a block of mass 0.5 kg is suspended from a
spring and it extends by 0.3 m due to block's weight. Given that the extension is within the
elastic limit, calculate the value of the spring constant k. [2 points]
k =16.33 N /m
5. Pre-Lab The block is pushed left so that it moves 0.2m towards left from its equilibrium
position and released with a velocity of 0.33 m s−1 towards the right. Write down the
values of the following initial conditions, paying special attention to their signs. [4
points]
v ( 0 )=¿0.33m/s
x ( 0 )=¿0.2m
6. Implement in Simulink the differential equation you found in part 3 using the parameters
evaluated so far in the previous parts. Assume that for now there is no damper in the
system. Connect a scope to the plot the displacement of the block. Paste the screenshot of
the model here. [5
points]
7. From the Model Configuration Parameters, set the solver to fixed step Runge-Kutta solver
and set the step size to 0.001 (it can also be written in MATLAB as 1e-3). Simulate the
system for 10 seconds and plot the displacement of the block on a scope. Capture a
screenshot of the graph and paste it here. [5 points]
14
8. Set the value of the damping constant, b=0.2 and run the simulation again. Describe the
difference in the behavior of the system from the previous part. [5 points]
The system loses energy and the wave damping is more due to addition of damping
constant.
15
Figure 1-12 Series RLC circuit
Simple current-voltage relationships of the ideal resistor, capacitor and inductor are given as
follows.
d vc diL
v R=Ri R , i c =C , v L =L
dt dt
1. Pre-Lab Use KVL and the above relations to write down the differential equations for the
given circuit. Express vC in terms of other parameters. [5 points]
v ¿=v C + v R +v L
t
1 di
v ¿= ∫ i ( t ) dt + R i R + L L
C 0 dt
2
d v C (t) d v C ( t)
v ¿=LC 2
+ RC + v C (t)
dt dt
1 R
vC ( t )=∫ ∫( LC
( )
v¿ −v C ( t ) ) dt − v C ( t ) dt
L
2. Pre-Lab Using your knowledge of RLC circuits, write down the steady state value of vC ,
i.e. the value of vC as t → ∞. [2 points]
vC ( ∞ )=¿ 5V
3. Model the differential equation in Simulink. Run the Simulink model for 60 seconds and
observe the voltage waveform across the capacitor on the scope. Describe the shape of the
curve and explain whether the simulation matches your prediction in part 2. [5 points]
16
4. Describe the shape of the curve and explain whether the simulation matches your
prediction in part 2.
[2 points]
After a long interval of time, the charge is stored in the capacitor due to the applied voltage,
from the graph it is clear from the decrease of the slope and at 5V it is costant.
17
Figure 1-13: Simple pendulum
In the above diagram, the pendulum has a mass m and length L. The angle of the string, in
radians, from the vertical is α and the acceleration due to gravity is g. Using Newton’s
second law, we can write the differential equation for the pendulum as follows.
mL α̈=−mgsin ( α )
1. Checkpoint Implement the model of the pendulum in Simulink. Set the solver to fixed step
Runge-Kutta with a step size of 0.001. Set the length of the pendulum to 1m, the mass to 1
XX
kg and release the pendulum from rest, from an angle equal to , where XX is the last 2
50
digits of your Roll Number. Simulate the pendulum for 20 seconds and plot the graph of α
against time. Paste a screenshot of the graph here. Convert the angle to degrees before
plotting it. [5 points]
2. Use the waveform in the previous part to calculate the time period of the pendulum correct
to 1 decimal place. [2 points]
T =¿ __________________________
3. Make appropriate changes to your simulation to model the pendulum as it would behave
on the Moon. Find the time period of the pendulum on the Moon. You may need to
simulate for a longer time. [2
marks]
T moon=¿ _________________________
18
1.8 Task 5: Liquid in a Leaky Tank [Extra Credit]
Objective: Using Simulink, simulate the level of fluid in a leaking tank with and without
inflow.
The rate at which a liquid flows out of a tank depends on the height of the liquid in the tank.
The exact relation between the liquid’s exit velocity and the height in the tank was found by
Torricelli. It is necessary to assume that the outflow of the liquid from the tank’s faucet is
laminar and the effects of viscosity and friction are negligible.
For a tank with a cross-sectional area A, faucet area a, the height of liquid h and the outflow
velocity u, as shown in Figure 1 -14, the height of the liquid is determined by the following
differential equation. The rate of flow of the liquid into the tank is V ¿ [ CITATION Haf02 \l
1033 ]
dh V ¿ a √ 2 gh
= −
dt A A
In this takes, we analyze the time it takes to empty a rooftop water tank supplying water to a
household.
1. Checkpoint Consider a water tank with a cross-sectional area 4000 cm 2 and a faucet at the
bottom with an area 1 cm2. The initial height of the liquid in the tank is 45 cm. Assume
that initially, no water is flowing into the tank, i.e. V ¿ =0. Simulate the height of the liquid
in this tank in Simulink. Choose a suitable time step and simulation time to plot a smooth
curve of h until it is less than 1.5 cm. Show the graph to the lab instructor.
19
□
2. How much time does it take for the tank to empty, i.e., the water level falls below 1 cm?
[2 points]
3. Simulate the tank again with the same parameters except with the inflow rate
V ¿ =250 cm3 s−1. Explain how the graph of h differs from the previous part. [2
points]
4. Adjust the value of V ¿ such that the height of the water in the tank approaches 20 cm as
t → ∞. Write the value of V ¿ below. [3 points]
20
If we consider ue to be constant and the rocket to be starting from rest, we get the following
differential equation describing the velocity of the rocket. The drag coefficient for this rocket,
represented by C in the following equation, has the value 2 ×10−4. [ CITATION MIT19 \l
1033 ]
du −ue dm 2
=a= −g−C u
dt m dt
Let's first investigate what is the maximum velocity a rocket can achieve from a certain
amount of fuel stored in it. A small rocket with a dry mass of 1.5 kg and 15 kg of fuel stored
in it initially.
1. Checkpoint Construct a model of the rocket in Simulink, considering that it expels mass at
a rate of 0.8 kg s−1 at a velocity of 250 m s−1. Include a check in your simulation so that the
rocket stops expelling the mass as soon as the fuel runs out, i.e. the mass of the rocket is
1.5 kg. Plot the velocity of the rocket on a graph and show it to the lab instructor.
□
2. What is the maximum velocity of the rocket after it runs out of fuel and how much time
does it take to achieve this velocity? [2
points]
3. Make suitable changes in the model to calculate the height of the rocket from the initial
position and plot it on a separate graph. What is the maximum height that the rocket
achieves after it runs out of fuel and how much time does it take to reach this height? [2
points]
4. By adjusting the initial amount of fuel in your model, you can control the maximum height
that the rocket can achieve. Find out how much fuel should be loaded in the rocket so that
it reaches a maximum height of 3500 m after it runs out of fuel. [3 points]
Assessment Rubrics
EE361: Control Systems – Lab 1
Method: Lab reports and instructor observation during lab sessions
Outcome Assessed:
a. Ability to conduct experiments, as well as to analyze and interpret data (P).
b. Ability to function on multi-disciplinary teams (A).
c. Ability to use the techniques, skills, and modern engineering tools necessary for
engineering practice (P).
21
Exceeds expectation Meets expectation Does not meet
Performance Marks
(4-5) (3-2) expectation (1)
Selects relevant Needs guidance to Incapable of
equipment to the select relevant selecting relevant
experiment, develops equipment to the equipment to
1. Realization of setup diagrams of experiment and to conduct the
Experiment [a, c] equipment develop equipment experiment,
connections or wiring. connection or wiring equipment
diagrams. connection or wiring
diagrams are
Actively engages and Cooperates with other Distracts or
cooperates with other group members in a discourages other
2. Teamwork [b] group members in an reasonable manner. group members
effective manner. from conducting the
experiment.
Does proper Calibrates equipment, Unable to calibrate
calibration of examines equipment appropriate
equipment, carefully moving parts, and equipment, and
3. Conducting examines equipment operates the equipment operation
Experiment [a, c] moving parts, and equipment with minor is substantially
ensures smooth error. wrong.
operation and process.
d.
22
Exceeds expectation Meets expectation Does not meet
Performance Marks
(5-4) (3-2) expectation (1)
Plans data collection to Plans data collection Does not know how to
achieve experimental to achieve plan data collection to
5. Data objectives, and experimental achieve experimental
conducts an orderly objectives, and goals; data collected is
Collection [a] and a complete data collects complete data incomplete and
collection. with minor error. contain errors.
Total
Signature: Signature:
Date: Date:
23