0% found this document useful (0 votes)
22 views20 pages

AE 315 Week 2 Tutorial Notes

This document provides an introduction to mathematical modeling and simulation techniques. It discusses numerical integration methods like Euler integration and MATLAB's ode45 solver. It also covers modeling electrical systems using differential equations and simulating RL and RLC circuits. Additionally, it explores modeling a mechanical mass-spring-damper system using differential equations.

Uploaded by

derfd
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)
22 views20 pages

AE 315 Week 2 Tutorial Notes

This document provides an introduction to mathematical modeling and simulation techniques. It discusses numerical integration methods like Euler integration and MATLAB's ode45 solver. It also covers modeling electrical systems using differential equations and simulating RL and RLC circuits. Additionally, it explores modeling a mechanical mass-spring-damper system using differential equations.

Uploaded by

derfd
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/ 20

Week 2 Tutorial Notes: Mathematical Modeling

and Simulation

Zeyad Manaa, Mohammed Elbalshy


Sep 4, 2023

Supplementary document for AE 315


Systems and Control course
Fall 2023

Aerospace Department
King Fahd University of Petroleum and Minerals
Dhahran, Saudi Arabia
Abstract
This document provides an introduction to mathematical modeling, specifically focusing
on differential equations and their numerical integration schemes. The Euler method and
MATLAB’s ode45 solver are discussed as techniques for approximating solutions to differ-
ential equations. Reduction of order is explored as a method for simplifying differential
equations. The application of mathematical modeling in electrical systems is also covered,
with a focus on RL circuits and RLC circuits. Simulation techniques using different state
variables are demonstrated. Additionally, the document delves into the modeling of me-
chanical systems, specifically the mass-spring-damper system. The content includes figures
illustrating numerical integration, circuit simulations, and mechanical system simulations.
This resource serves as a comprehensive guide to mathematical modeling and simulation
techniques and their application in electrical and mechanical systems.

Contents
1 Introduction to Mathematical Modeling 3

2 Differential Equations 3
2.1 Numerical Integration Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.1 Euler method - RK1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.2 MATLAB ode45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Reduction of Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1 Reduction of order example . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Modeling Electrical Systems 9


3.1 RL Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1.1 Simulating the model using the current as the state variable . . . . . . . . 10
3.1.2 Simulating the model using the charge as the state variable . . . . . . . . 11
3.2 RLC Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4 Modeling Mechanical Systems 16


4.1 Mass-Spring-Damper Mechanical System . . . . . . . . . . . . . . . . . . . . . . . 16

List of Figures
1 Euler method integration using a step size of 0.05 . . . . . . . . . . . . . . . . . . 5
2 Euler, ode45, and analytical solution comparison . . . . . . . . . . . . . . . . . . 7
3 Solution of the system of first order ODEs . . . . . . . . . . . . . . . . . . . . . . 9
4 RL circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 RL Circuit Simulation - Current as State Variable . . . . . . . . . . . . . . . . . 11
6 RL circuit modelling on SIMULINK . . . . . . . . . . . . . . . . . . . . . . . . . 11
7 RL Circuit Simulation - Charge as State Variable . . . . . . . . . . . . . . . . . . 13
8 RL circuit modelling on SIMULINK . . . . . . . . . . . . . . . . . . . . . . . . . 13
9 RLC circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
10 RLC circuit modelling on SIMULINK . . . . . . . . . . . . . . . . . . . . . . . . 15
12 Mass-spring-damper system free body diagram . . . . . . . . . . . . . . . . . . . 16
11 Mass-spring-damper system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
13 Mass-spring-damper system simulation . . . . . . . . . . . . . . . . . . . . . . . . 18
14 Mass spring damper system modelling on SIMULINK . . . . . . . . . . . . . . . 18

1
Listings
1 Numerical Euler method vs. the analytical solution . . . . . . . . . . . . . . . . . 4
2 Comparison between Euler and ode45 solvers with different step sizes . . . . . . 5
3 The solution of the system of first-order ODE reduced from a second order ODE 8
4 RL Circuit Simulation - Current as State Variable . . . . . . . . . . . . . . . . . 10
5 RL Circuit Simulation - Charge as State Variable . . . . . . . . . . . . . . . . . . 12
6 RLC Circuit Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7 Mass-spring-damper system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2
1 Introduction to Mathematical Modeling
The developed mathematical models of physical systems are a key factor for control systems
design and analysis. Mathematical models are typically ordinary differential equations [1, 2].
Most physical systems in reality exhibits high nonlinearity. The vast and most completed body
of the control theory has been developed for linear systems. But, the fact that most physical
systems are nonlinear is kind of limitation to this complete theory [3, 4]. However, one can
employ any linearization technique to linearize the nonlinear dynamics (mathematical model)
in order to leverage the very solid linear control theory. This introduces the trade of between
the accuracy of the mathematical model used in control systems. One can accept a linear less
accurate linear model in favour of the applicable linear control technique, other may weigh the
accuracy over the simplicity.
For most of of this course’s material, we will be focusing on the linear control strategies as
one pillar, more advanced techniques may be covered as we go on.

2 Differential Equations
Differential equations play a crucial role in describing the behavior of various physical, bio-
logical, and engineering systems. They are mathematical equations that involve derivatives and
represent the rate of change of a function with respect to one or more independent variables.

2.1 Numerical Integration Schemes


The aim is to integrate a first-order ordinary differential equation (ODE) on the form

dx(t)
= f (t, x(t)) (1)
dt
over a finite time interval ∆t, to convert them to a difference equation,
Z t+∆t
x(t + ∆t) = x(t) + f (τ, x(τ ))dτ (2)
t

or alternatively, if we assume that tn = n∆t and xn ≜ xn (tn ),


Z t+∆t
xn+1 = xn + f (τ, x(τ ))dτ (3)
t

Here, dx(t)
dt represents the derivative of the unknown function x with respect to the indepen-
dent variable t, and f (t, x) is a known function that determines the rate of change. Analytically
solving such equations involves finding an expression for x in terms of t by integrating both
sides. For instance, if
f (t, x) = 2t (4)
the solution becomes
x(t) = t2 + C (5)
where C is the constant of integration. The solution is obtained by the separation of variables
method. In a discrete manner, we can write xn
As discussed earlier, most of the differential equations governing a real physical systems are
nonlinear. Obtaining an analytical solution for nonlinear differential equation can be tricky in
most of the cases. Instead, we tend to utilize numerical integration methods.

3
2.1.1 Euler method - RK1
Numerically solving differential equations involves approximating the solution at discrete
points. The simplest scheme is the first-order Runge-Kutta method (RK1), also known as the
Euler method. Utilizing the discrete formulation of the derivative we can write,

dx(t) x(t + ∆t) − x(t)


≈ ≈ f (t, x(t)) (6)
dt ∆t
The goal is to solve for the next step. So,

x(t + ∆t) ≈ x(t) + ∆tf (t, x(t)) (7)

In a discrete format we can approximate the next step by using the forward difference
formula:

xn+1 = xn + ∆tf (tn , xn ) (8)


Here, ∆t is the step size, xn represents the numerical approximation of x(tn ) at the nth step,
and tn denotes the nth time point. MATLAB code for this scheme would look like:

Listing 1: Numerical Euler method vs. the analytical solution


1 % Define the step size and time points
2 stepSize = 0.05; % Step size
3 timePoints = 0: stepSize :1; % Time points
4
5 % Initialize the solution arrays
6 numerical Soluti on = zeros ( size ( timePoints ) ) ; % Initialize
numerical solution array
7 analytical So lu ti on = timePoints .^2 + 1; % Analytical solution
8
9 % Solve the differential equation using RK1 ( Euler ' s method )
10 numerical Soluti on (1) = 1; % Initial condition
11 for n = 1: length ( timePoints ) -1
12 numericalS oluti on ( n +1) = num erical Soluti on ( n ) + ...
13 stepSize * (2 * timePoints ( n ) ) ; % Update solution using RK1
14 end
15
16 % Plot the numerical and analytical solutions
17 fig = figure () ; % Initialize a figure
18 set ( fig , ' color ' , 'w ') % Set the background color to be white
19 plot ( timePoints , numericalSolution , ...
20 'b - ' , ' LineWidth ' , 1.5) ; % Plot numerical solution
21 hold on ;
22 plot ( timePoints , analyticalSolution , ...
23 'r - - ' , ' LineWidth ' , 1.5) ; % Plot analytical solution
24 hold off ;
25
26 % Set plot properties
27 xlabel ( '$ t $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
28 ylabel ( '$ y $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
29 legend ( ' Numerical Solution ( RK1 ) ' , ' Analytical Solution ' , ...
30 ' Interpreter ' , ' latex ' , ' FontSize ' , 16 , ' location ' , ' best ') ;
31 set ( gca , ' FontSize ' , 16 , ' T i c k L a b e l I n t e r p r e t e r ' , ' latex ') ;

4
And the code result can be found in figure 1.

Figure 1: Euler method integration using a step size of 0.05

2.1.2 MATLAB ode45


It is worth noting that the Euler method accuracy and stability are step size dependent.
MATLAB has a robust and accurate ODE solver, called ode45 that is built based on the Dor-
mand and Prince paper [5]. Listing 2 provides a MATLAB code that compares the Euler method
against ode45 solver. The code result in figure 2 shows how ode45 solver is kind of robust against
the step size as compared to the Euler method.

Listing 2: Comparison between Euler and ode45 solvers with different step sizes
1 % Define the analytical solution
2 analytical So lu ti on = @( t ) t .^2 + 1;
3
4 % Define the step sizes to be compared
5 stepSizes = [0.1 , 0.05 , 0.01];
6
7 % Initialize the figure
8 fig = figure ( ' Position ' , [100 , 100 , 800 , 600]) ;
9 set ( fig , ' color ' , 'w ')
10
11 % Loop over the step sizes
12 for i = 1: length ( stepSizes )
13 stepSize = stepSizes ( i ) ;
14
15 % Solve the differential equation using Euler ' s method
16 tEuler = 0: stepSize :1;
17 yEuler = zeros ( size ( tEuler ) ) ;
18 yEuler (1) = 1;
19 for n = 1: length ( tEuler ) -1

5
20 yEuler ( n +1) = yEuler ( n ) + stepSize * (2 * tEuler ( n ) ) ;
21 end
22
23 % Solve the differential equation using ode45
24 [ tODE45 , yODE45 ] = ode45 (@ myODE , [0: stepSize :1] , 1) ;
25
26 % Plot the numerical solutions
27 subplot ( length ( stepSizes ) , 1 , i ) ;
28 plot ( tEuler , yEuler , 'b - ' , ' LineWidth ' , 1.5) ;
29 hold on ;
30 plot ( tODE45 , yODE45 , 'r - ' , ' LineWidth ' , 1.5) ;
31 hold on ;
32 % Plot the analytical solution
33 tAnalytical = 0:0.001:1;
34 yAnalytical = a na ly ti ca lSo lu ti on ( tAnalytical ) ;
35 plot ( tAnalytical , yAnalytical , 'k - - ' , ' LineWidth ' , 1.5) ;
36 hold off ;
37
38 % Set plot properties
39 xlabel ( '$ t $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
40 ylabel ( '$ y $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
41 title ( sprintf ( ' Step Size = %.2 f ' , stepSize ) , ...
42 ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
43 legend ( ' Euler Method ' , ' ode45 ' , ' Analytical ' , ...
44 ' Interpreter ' , ' latex ' , ' FontSize ' , 16 , ...
45 ' location ' , ' west ') ;
46 set ( gca , ' FontSize ' , 16 , ' T i c k L a b e l I n t e r p r e t e r ' , ' latex ') ;
47 end
48
49
50 % Define the differential equation as a separate function
51 function dydt = myODE (t , y )
52 dydt = 2 * t ; % Define the derivative of y with respect to t
53 end

There are a number of extensions available for numerically solving ODEs, the Euler tech-
nique being just one of them. In fact, this is an independent area of study focused on identifying
numerical approximations to ODE solutions. However, we will utilize practical MATLAB li-
braries like ode45 and other variations like ode11, ode4, and so on. These libraries offer reliable
and effective ODE-solving methods. Interested readers may refer to the paper by Sola et al. [6].
The exploration of various numerical systems is discussed in a good way in this paper, and also
provides insightful analysis.

6
Figure 2: Euler, ode45, and analytical solution comparison

2.2 Reduction of Order


Most of the cases in real world employ higher order differential equations. In the previous
sub-section we introduced how a first order differential equation can be solved using different
numerical techniques. A higher-order ODE can be reduced in order to create a system of first-
order ODEs. This allows us to use numerical techniques developed for first-order ODE systems
to solve higher-order ODEs. When utilizing numerical solvers designed to handle systems of
first-order ODEs, such as MATLAB’s ode45 function, this strategy is especially helpful.
Let’s use a second-order ODE as an example to demonstrate reduction of order:

ÿ(t) + p(t)ẏ(t) + q(t)y(t) = r(t) (9)

For short, we will drop the (t) argument and only write ẏ, where the (˙) is a symbol for the time
derivative. To reduce the order we introduce an auxiliary variable x1 = y, and another variable
x2 = ẏ, to convert this second-order ODE to a system of first-order ODEs. As a result, we can
reformat the equation as follows:

x1 = y −→ ẋ1 = ẏ = x2
(10)
x2 = ẏ −→ ẋ2 = ÿ = r − px2 − qx1

In terms of x1 and x2 , a system of two first-order ODEs has been established. The system can
then be solved using numerical techniques created for first-order ODE systems.

7
2.2.1 Reduction of order example
Let’s use MATLAB to numerically solve a specific scenario to illustrate reduction of order.
Think about the subsequent second-order ODE:
ÿ(t) + 4ẏ(t) + 3y(t) = cos t
We can rewrite this equation as a system of first-order ODEs:
y ′ (t) = v(t)
v ′ (t) = cos(t) − 4v(t) − 3y(t)
Now, we can solve this system numerically in MATLAB using the ode45 function. Here’s the
MATLAB code:
Listing 3: The solution of the system of first-order ODE reduced from a second order ODE
1 % Init . timeSpan
2 timeSpan = [0 , 5];
3
4 % Init . initial conditions
5 ICs = [0 , 0];
6
7 % Solve the system of ODEs using the ode45 solver
8 [t , y ] = ode45 (@ myODE , timeSpan , ICs ) ;
9
10 % Plot the numerical solution
11 fig = figure () ; % Initialize a figure
12 set ( fig , ' color ' , 'w ') % Set the background color to be white
13 plot (t , y (: , 1) , 'b - ' , ' LineWidth ' , 1.5) ;
14 hold on
15 plot (t , y (: , 2) , 'r - ' , ' LineWidth ' , 1.5) ;
16 xlabel ( '$ t $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ; % Label
for the x - axis
17 ylabel ( '$ y ( t ) $ ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ; %
Label for the y - axis
18 legend ( '$ y $ ' , '$ v $ ' , ...
19 ' Interpreter ' , ' latex ' , ' FontSize ' , 16 , ...
20 ' location ' , ' best ')
21 set ( gca , ' FontSize ' , 16 , ' T i c k L a b e l I n t e r p r e t e r ' , ' latex ') ;
22
23
24 function dydt = myODE (t , y )
25 % Function representing the system of first - order ODEs
26 % Inputs :
27 % t : time
28 % y : vector containing the variables y ( t ) and v ( t )
29 % Output :
30 % dydt : vector of derivatives of y ( t ) and v ( t )
31
32 dydt = zeros (2 , 1) ;
33 dydt (1) = y (2) ; % Derivative of y ( t ) is v ( t )
34 dydt (2) = cos ( t ) - 4 * y (2) - 3 * y (1) ; % Derivative of v ( t )
is given by the second - order ODE
35 end

8
The code result can be found in figure 3.

Figure 3: Solution of the system of first order ODEs

3 Modeling Electrical Systems


By using one or both of Kirchhoff’s rules, it is possible to obtain a mathematical representa-
tion for an electrical circuit. Our main concern in this section is the analysis of straightforward
electrical circuits after developing a governing mathematical modelling.

3.1 RL Circuit
It is frequently required to use numerical simulations to evaluate and comprehend the behav-
ior of circuits. We will examine two distinct methods for simulating RL circuits in this section:
one using current i as the state variable and the other using charge q. We will walk through the
mathematical derivations and the process of executing these simulations in MATLAB using the
well-known numerical solver ode45. We will be able to examine the time-dependent reactions of
RL circuits through these simulations, gaining important knowledge about their behavior and
assisting in the study of real-world applications.
Consider the circuit in figure 4, along with Kirchhoff’s voltage law (KVL) to derive the
differential equation for the current.
di(t)
V = Ri(t) + L (11)
dt
Another way to look at the problem in hand is to introduce the modeling using the charge q
instead of the current i. The goal here is to get used to the reduction of order, as the modeling
using q will result in second order ODE instead of the first order ODE produced from the current
i model. Knowing that,
dq(t)
i(t) = (12)
dt
Substituting by equation 12 in equation 11:
dq(t) d2 q(t)
V =R +L (13)
dt dt2

9
R

V
L
i

Figure 4: RL circuit

3.1.1 Simulating the model using the current as the state variable
Recalling equation 11 and rearranging,
d(i) 1
= (V − Ri(t)) (14)
dt L
Fortunately, we have a first order ODE, so we can directly use ode45 and get the evolution of
the current as a function of time. The MATLAB code in listing 4 simulate the behaviour of the
circuit with the current being a state variable.
Listing 4: RL Circuit Simulation - Current as State Variable
1 % Initial condition
2 i0 = 0; % Initial current ( in Amperes )
3
4 % Time span
5 tstart = 0; % Start time ( in seconds )
6 tend = 1; % End time ( in seconds )
7
8 % Solve the differential equation
9 [t , i ] = ode45 (@ rl_circuit_current , [ tstart , tend ] , i0 ) ;
10
11 % Plotting
12 fig = figure () ; % Initialize a figure
13 set ( fig , ' color ' , 'w ') % Set the background color to be white
14 plot (t , i , ' -b ' , ' LineWidth ' , 1.5) ;
15 xlabel ( ' Time ( s ) ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
16 ylabel ( ' Current ( A ) ' , ' Interpreter ' , ' latex ' , ' FontSize ' , 18) ;
17 title ( ' RL Circuit Simulation - Current as State Variable ' , '
Interpreter ' , ' latex ' , ' FontSize ' , 16) ;
18 set ( gca , ' FontSize ' , 16 , ' T i c k L a b e l I n t e r p r e t e r ' , ' latex ') ;
19
20 function di_dt = r l_ ci rc uit_c ur re nt (t , i )
21 R = 10; % Resistance ( in Ohms )
22 L = 1; % Inductance ( in Henrys )
23 V = 2;

10
24 di_dt = (1/ L ) *( V - R / L * i ) ;
25 end

Figure 5: RL Circuit Simulation - Current as State Variable

Figure 6: RL circuit modelling on SIMULINK

Figure 6 shows how to model the RL circuit on SIMULINK using the current as the state
variable.

3.1.2 Simulating the model using the charge as the state variable
Recalling equation 13,
dq d2 q
V =R +L 2 (15)
dt dt
Let x1 = q and x2 = q̇, then
ẋ1 = q̇ = x2
V R (16)
ẋ2 = q̈ = − ẋ1
L L
We now have a system of first order ODEs that can be solved using ode45 directly as in listing
5.

11
Listing 5: RL Circuit Simulation - Charge as State Variable
1 % Define the parameters
2 resistance = 10; % R
3 inductance = 1; % L
4 appliedVoltage = 2; % V
5
6 % Define the initial conditions
7 initialCo nditio ns = [0; 0]; % q (0) = 0 , dq (0) / dt = 0
8
9 % Define the time span
10 timeSpan = [0 1]; % Simulation time from 0 to 10 seconds
11
12 % Solve the integro - differential equation using ode45
13 [ time , state ] = ode45 (@( t , y ) rlcEquation (t , y , ...
14 resistance , inductance , appliedVoltage ) , timeSpan ,
ini tialCo nditio ns ) ;
15
16 % Plot the charge on the capacitor as a function of time
17 fig = figure ;
18 set ( fig , ' Color ' , 'w ') ; % Set figure background to white
19 plot ( time , state (: , 1) , 'b - ' , ' LineWidth ' , 1.5) ;
20 hold on
21 plot ( time , state (: , 2) , 'r - ' , ' LineWidth ' , 1.5) ;
22 xlabel ( ' Time $ s $ ' , ' Interpreter ' , ...
23 ' latex ' , ' FontSize ' , 18) ; % Use LaTeX for x - axis label
24 legend ( '$ q $ ' , '$ i $ ' , ...
25 ' Interpreter ' , ' latex ' , ' FontSize ' , 16 , ...
26 ' location ' , ' best ') ;
27 set ( gca , ' T i c k L a b e l I n t e r p r e t e r ' , ...
28 ' latex ' , ' FontSize ' , 16) ; % Use LaTeX for axis ticks and
increase font size
29
30 function dstates = rlcEquation (t , x , R , L , V )
31 % RLC circuit integro - differential equation
32
33 % Variables
34 q = x (1) ; % Charge on capacitor
35 dqdt = x (2) ; % Derivative of charge
36
37 % Compute derivatives
38 ddqdt = (1 / ( L ) ) * ( V - ( R * dqdt ) ) ;
39
40 % Return derivatives
41 dstates = [ dqdt ; ddqdt ];
42 end

The code output can be found in figure 7.

12
Figure 7: RL Circuit Simulation - Charge as State Variable

Figure 8: RL circuit modelling on SIMULINK

Figure 8 shows how to model the RL circuit on SIMULINK using the charge as the state
variable.

3.2 RLC Circuit


Here, in this section, we will examine the time-dependent reactions of RLC circuits through
MATLAB simulations. Consider the circuit in figure 9.

13
R

V
L
i
C

Figure 9: RLC circuit

The RLC circuit mathematical model with current as the main variable can be represented
as follows
Z
di 1
V = Ri + L + i dt (17)
dt C
Here we end up with what so called the integro-differential equation. In order to avoid compli-
cated solution type, we can just utilize the fact stated in equation 12 and the equation can be
written as,
d2 q dq q
V =L 2 +R + (18)
dt dt C
After reducing the order of equation 18, we get

ẋ1 = q̇ = x2
1 x1  (19)
ẋ2 = q̈ = V − Rẋ1 −
L C
The system can be simulated via MATLAB code as following,

Listing 6: RLC Circuit Simulation


1 % Define the parameters
2 resistance = 10; % R
3 inductance = 1e -3; % L
4 capacitance = 1e -6; % C
5 appliedVoltage = 10; % V
6
7 % Define the initial conditions
8 initialCo nditio ns = [0; 1]; % q (0) = 1 , dq (0) / dt = 3
9
10 % Define the time span
11 timeSpan = [0 1e -3]; % Simulation time from 0 to 1 ms
12
13 % Solve the integro - differential equation using ode45
14 [ time , state ] = ode45 (@( t , y ) rlcEquation (t , y , ...
15 resistance , inductance , capacitance , appliedVoltage ) , ...

14
16 timeSpan , in itialC onditi ons ) ;
17
18 % Plot the charge on the capacitor as a function of time
19 fig = figure ;
20 set ( fig , ' Color ' , 'w ') ; % Set figure background to white
21 subplot (211) ;
22 plot ( time , state (: , 1) , 'b - ' , ' LineWidth ' , 1.5) ;
23 xlabel ( ' Time $ s $ ' , ' Interpreter ' , ...
24 ' latex ' , ' FontSize ' , 18) ;
25 ylabel ( ' Charge $ c $ ' , ' Interpreter ' , ...
26 ' latex ' , ' FontSize ' , 18) ;
27 subplot (212) ;
28 plot ( time , state (: , 2) , 'r - ' , ' LineWidth ' , 1.5) ;
29 xlabel ( ' Time $ s $ ' , ' Interpreter ' , ...
30 ' latex ' , ' FontSize ' , 18) ;
31 ylabel ( ' Current $ A $ ' , ' Interpreter ' , ...
32 ' latex ' , ' FontSize ' , 18) ;
33
34 set ( gca , ' T i c k L a b e l I n t e r p r e t e r ' , ...
35 ' latex ' , ' FontSize ' , 16) ; % Use LaTeX for axis ticks and
increase font size
36
37 function dstates = rlcEquation (t , x , R , L , C , V )
38 % RLC circuit integro - differential equation
39 % x = [ x1 = q
40 % x2 = dqdt ]
41
42 dqdt = x (2) ; % Charge on capacitor
43
44 % Compute derivatives
45 ddqdt = (1 / ( L ) ) * ( V - ( R * x (2) ) - ( x (1) / C ) ) ;
46
47 % Return derivatives
48 dstates = [ dqdt ; ddqdt ]; % (q , i ) after integration
49 end

Figure 10: RLC circuit modelling on SIMULINK

Figure 10 shows how to model the RLC circuit on SIMULINK.

15
Figure 12: Mass-spring-damper system free body diagram

4 Modeling Mechanical Systems


By applying Newoton’s second law, we can also obtain a mathematical representa- tion for
a mass-spring-damper mechanical system. Our main concern in this section is the analysis of
straightforward this mechanical system after developing a governing mathematical modelling.

4.1 Mass-Spring-Damper Mechanical System


From Newton’s second law of motion in the dynamical systems, the acceleration of a mass
caused by an external force acting on the system is directly proportional to the magnitude of
the net force, in the same direction as the net force, and inversely proportional to the mass of
the object. This can be expressed mathematically in equation 20
X
F = ma = mẍ (20)
P
Where m is the mass of the body, F is the net force and a is the acceleration.
Consider the mass spring damper system in figure 11, by applying Newoton’s Laws to get
the differential equations for the displacement.

Figure 11: Mass-spring-damper system

The equation of motion of the system can be represented as equation 21


X
F = Fe − Fs − Fd = mẍ (21)

Where Fe , Fs , and Fd are applied or external force, spring force and damping force respectively.
The spring and damping force is represented mathematically as equation 22

Fs = kx
(22)
Fd = cẋ

Where k and d is the spring and damping coefficient respectively. Figure 12 represents the free
body diagram for the system. The differential equation for the system can be represented as
shown in equation 23 assuming that there is no external force.

mẍ + kx + cẋ = 0 (23)

This can be rearranged into


1
ẍ = (−kx − cẍ) (24)
m

16
By applying reduction of order, let z1 = x and z2 = ẋ, then
z˙1 = ẋ = z2
1 (25)
z˙2 = ẍ = (−kx − cẍ)
m
We now have a system of first order ODEs that can be solved using ode45 directly as in
listing 7.
Listing 7: Mass-spring-damper system
1 clear all ; clc ; close all ;
2
3
4 % Define system parameters
5 m = 1; % Mass ( kg )
6 k = 10; % Spring constant ( N / m )
7 c = 0.5; % Damping coefficient ( Ns / m )
8
9 % Define initial conditions
10 x0 = 0; % Initial displacement ( m )
11 v0 = 1; % Initial velocity ( m / s )
12
13 % Define simulation time span
14 tspan = [0 10]; % Simulation time from 0 to 10 seconds
15
16 % Solve the differential equation using ode45
17 [t , y ] = ode45 (@( t , y ) m a s s _ s p r i n g _ d a m p e r _ e q u a t i o n (t , y , m , k , c ) ,
...
18 tspan , [ x0 ; v0 ]) ;
19
20 % Extract displacement and velocity from the solution
21 x = y (: , 1) ;
22 v = y (: , 2) ;
23
24 % Plot the charge on the capacitor as a function of time
25 fig = figure ;
26 set ( fig , ' Color ' , 'w ') ; % Set figure background to white
27 subplot (2 , 1 , 1) ;
28 plot (t , x , 'b - ' , ' LineWidth ' , 1.5) ;
29 xlabel ( ' Time $ s $ ' , ' Interpreter ' , ...
30 ' latex ' , ' FontSize ' , 18) ;
31 ylabel ( ' Displacement $ m $ ' , ' Interpreter ' , ...
32 ' latex ' , ' FontSize ' , 18) ;
33 subplot (2 , 1 , 2) ;
34 plot (t , v , 'r - ' , ' LineWidth ' , 1.5) ;
35 xlabel ( ' Time $ s $ ' , ' Interpreter ' , ...
36 ' latex ' , ' FontSize ' , 18) ;
37 ylabel ( ' Velocity $ m / s $ ' , ' Interpreter ' , ...
38 ' latex ' , ' FontSize ' , 18) ;
39
40 set ( gca , ' T i c k L a b e l I n t e r p r e t e r ' , ...
41 ' latex ' , ' FontSize ' , 16) ;
42

17
43
44
45
46 function dydt = m a s s _ s p r i n g _ d a m p e r _ e q u a t i o n (t , y , m , k , c )
47 % Extract state variables
48 z1 = y (1) ;
49 z2 = y (2) ;
50
51 % Define the differential equations
52 z1_dot = z2 ;
53 z2_dot = ( - k * z1 - c * z2 ) / m ;
54
55 % Pack the derivatives into a column vector
56 dydt = [ z1_dot ; z2_dot ];
57 end

0.3

0.2

0.1

-0.1

-0.2

-0.3
0 1 2 3 4 5 6 7 8 9 10

Figure 13: Mass-spring-damper system simulation

Figure 14: Mass spring damper system modelling on SIMULINK

Figure 14 shows how to model the mass spring damper system on SIMULINK.

18
References
[1] R. Dorf and R. Bishop, Modern control Systems. 2017.

[2] K. Ogata, Modern control engineering fifth edition. 2010.

[3] H. Kwakernaak and R. Sivan, Linear optimal control systems, vol. 1. Wiley-interscience New
York, 1972.

[4] K. J. Åström and R. M. Murray, Feedback systems: an introduction for scientists and engi-
neers. Princeton university press, 2021.

[5] J. R. Dormand and P. J. Prince, “A family of embedded runge-kutta formulae,” Journal of


computational and applied mathematics, vol. 6, no. 1, pp. 19–26, 1980.

[6] J. Sola, “Quaternion kinematics for the error-state kalman filter,” arXiv preprint
arXiv:1711.02508, 2017.

19

You might also like