Department of Electrical Engineering EE361: Control Systems Lab
Department of Electrical Engineering EE361: Control Systems Lab
Department of Electrical Engineering EE361: Control Systems Lab
1
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. [1]
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.
2
discrete values of t . This process is known as solving a differential equation numerically.
Euler's method is one of the basic methods for finding a numerical solution to a given
differential equation. It requires a differential equation with an initial condition,
dy
=f ( x ,t ) , x ( t 0 )=x 0
dx
Then for numerically solving the differential equation and finding the values of x at specific
time instances, a finite number of points are defined for t through the iterative formula,
t n+1=t n + Δt
and the corresponding values of x are found using,
x n+1=x n + Δ t ∙ f ( x n ,t n )
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.
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 )
3
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 v C 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.
4
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.
5
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.
6
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
v C =∫ ( 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 v C. The
difference is divide by RC . Now this term is equal to the right side of Equation 1-1.
7
This signal is then integrated to get v C. The initial condition, v C 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 v C.
1.4.3 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.
8
Figure 1-9: v C against time for a series RC circuit
( )
−t
v C ( t )=v ¿ 1−e RC
This equation can be plotted in MATLAB to observe the behavior of v C against time. Come
to the MATLAB main window and type the following commands. You can omit the
comments after the %%. Note that you do not need to type the >> symbols, they appear
themselves at the beginning of every new line.
>> Vin = 1;
>> t = linspace(0, 10, 500);%% vary t from 0 to 10 in 500
steps
>> plot(t, Vin*(1-exp(-t))); %% plot Vc against t
>> grid on %% turn on the grid on the graph
>> xlabel(‘time’) %% label the x-axis
>> ylabel(‘Vc’) %% label the y-axis
9
After these commands, you should see a MATLAB Figure similar to the following diagram.
10
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 ( ) dT
dt
and temperature ( T ) in the above differential equation to get
0.03
k =¿__________________
25° C
T a=¿ __________________
90° C
T 0=¿__________________
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. Plot the garaph on the grid in . Label the
values on the horizontal and vertical axes. [5 points]
11
Figure 1-11: Plot of temperature against time
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=10 s
(ii) You chose in part 2 to make the graph look smooth.
(i) 66.45
T ( 15 )=¿___________________
(ii) 66.4469
T ( 15 )=¿___________________
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]
−kt
T ( t )=T a + ( T 0−T a ) e
66.45
T ( 15 )=¿___________________
5. Analysis Compare your two answers to part with the analytical solution in part 4 and state
whether smaller or larger time steps are better for this simulation? [3 points]
Ans: As we can see the results, So, in this case, the Runge-Kutta simulation result is in
close agreement with the corrected analytical solution. This suggests that the chosen time
12
step (Δt=10) is sufficient for this simulation, and smaller time steps may not significantly
improve accuracy in this specific scenario.
1.6 Task 2: Simulation of a Damped Mass-Spring System
Objective: Using Simulink, predict how the motion of horizontal mass-spring changes if we
vary the damping constant.
1.6.1 Spring-Mass System
Consider a mass-spring system with one end fixed. A damper is also attached to the system to
resist the motion of the mass. If a block of mass m is pulled by a force F , the spring extends
by a distance x .
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]
2
d y
m 2 =-kx
dt
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
a direction to oppose the velocity of the mass. Its magnitude can be assumed to be
proportional to the speed of the mass.
13
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.
2
dx dv d 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]
Ans: Fx = -kx
The negative sign indicates that the spring force acts in the opposite direction to the
displacement, reflecting the restoring nature of the force. The magnitude of the force is
directly proportional to the displacement and is determined by the spring constant.
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]
Ans: Fd = -bv where b is damping constant
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]
Ans: F_Total =−kx−bv
Now, we can use Newton's second law (F total=ma), where m is the mass of the block and a
is the acceleration, to write the differential equation in terms of displacement (x) and its
2
d x
derivatives. Assuming the mass m 2 =−kx−bv
dt
14
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]
16.3 k =¿ ¿
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]
−1
0.33 m s
v ( 0 )=¿________________________
-0.2m
x ( 0 )=¿________________________
Implement the differential equation you found in part 3, in Simulink using the parameters evaluated
so far in the previous parts. Assume that there is no damper in the system. 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. Sketch the graph of displacement against time in
15
Set the value of the damping constant, b=0.2 and run the simulation again. Sketch the graph of
displacement against time in
7. . Label the values on the horizontal and vertical axes. Plot all important points on the
graph accurately. [5 points]
8. Analysis Change the damping factor and describe how the behavior of the block changes
with increasing the damping factor. Describe the change in the oscillation frequency, rate
of decay and final value when you increase the damping factor. [3
points]
Ans: As the damping factor is increasing the rate of decay is also decreasing along the x axis
as we can see from our results.
1.7 Task 3: Series RLC Circuit
Objective: Simulate an RLC circuit in Simulink using its differential equation and predict the
behavior of voltage across the capacitor.
Consider a series RLC circuit shown in Figure 1-14.
Simple current-voltage relationships of the ideal resistor, capacitor and inductor are given as
follows.
16
d vc d iL
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 v C in terms of other parameters. [5 points]
v C ( t )=¿_________________________________
2. Pre-Lab Using your knowledge of RLC circuits, write down the steady state value of v C,
i.e. the value of v C as t → ∞ . [2 points]
5V
v C ( ∞ )=¿ ______________________________
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. Plot the waveform of the
capacitor voltage on Figure 1-15. [5 points]
4. Analysis Describe the shape of the curve, its behavior over time and the final value. Does
the final value of the capacitor voltage in simulation matches your prediction in part 2?
[4 points]
17
Figure 1-15: Capacitor Voltage
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 ( α )
18
1. 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 1kg and
XX
release the pendulum from rest, from an angle equal to , where XX is the last 2 digits
50
of your Roll Number. Simulate the pendulum for 20 seconds and plot the graph of α
against time. Sketch 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=¿ _________________________
19
Figure 1-17: Liquid leaking from a tank [5]
d h V ¿ a √2 g h
= −
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.
□
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
3 −1
V ¿ =250 cm s . Explain how the graph of h differs from the previous part. [2
points]
20
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]
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. [6]
du −ue dm 2
=a= −g−C u
dt m dt
21
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]
22
Assessment Rubric
Method:
Lab report evaluation and instructor observation during lab sessions.
23