0% found this document useful (0 votes)
69 views37 pages

Process Control-Lecture 09

This document discusses solving ordinary differential equations (ODEs) numerically in MATLAB. It covers four methods: built-in ODE solvers, symbolic solution using dsolve, Laplace transforms, and Simulink. It focuses on using ODE solvers like ode23 and ode45, which are based on Runge-Kutta methods. It provides an example of transforming a higher-order ODE into first-order state variable equations and using ode45 to solve the system. Code examples are given to define the system and solve it using ode45.

Uploaded by

Tenson Sichone
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)
69 views37 pages

Process Control-Lecture 09

This document discusses solving ordinary differential equations (ODEs) numerically in MATLAB. It covers four methods: built-in ODE solvers, symbolic solution using dsolve, Laplace transforms, and Simulink. It focuses on using ODE solvers like ode23 and ode45, which are based on Runge-Kutta methods. It provides an example of transforming a higher-order ODE into first-order state variable equations and using ode45 to solve the system. Code examples are given to define the system and solve it using ode45.

Uploaded by

Tenson Sichone
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/ 37

THE COPPERBELT UNIVERSITY

School of mines and minerals sciences

CE 560 / MT 580
Process control

Lecture 10
Controller tuning

C. Botha (Mr.)
Contacts: Chemical Engineering Department
Email: [email protected]
Alt. Email: [email protected]

January, 2023
Solution of ordinary differential equations in MATLAB

o Four methods are available in MATLAB.


1) Using MATLAB built-in functions known as ODE solvers.
2) Symbolic solution using dsolve command,
3) Symbolic solution using Laplace transforms, and
4) Numerical solution using Simulink – in MATLAB.
ODE solvers are
 Designed to solve first-order initial value problems (IVP) and include;
 ode23, ode45, ode113 for non-stiff equations, and Ode15 for stiff equations.
Solution of ordinary differential equations in MATLAB

Fig. 9.1 MATLAB Environment


Solution of ordinary differential equations in MATLAB

o ODE solvers are based on the Runge-Kutta methods.


o In the Runge-Kutta methods, solution estimates are generated in an iterative
manner.
o Accuracy of the Taylor series method is used but without having to evaluate the
derivates.
o Taylor series without higher-order terms is
f ( x)  f (0)  ( x - x0 ) f ( xi , yi ) .........................................(9.0)

o 𝑓 ′ (𝑥𝑖 , 𝑦𝑖 ) is the suitable slope over the interval 𝑥𝑖 to 𝑥𝑖+1 used for extrapolating
𝑦𝑖+1 from 𝑦𝑖 .
o Number of points used in the interval [𝑥𝑖 , 𝑥𝑖+1 ] to determine the slope determines
the order of the Runge-Kutta method.
Solution of ordinary differential equations in MATLAB

o Thus, second-order Runge-Kutta methods uses two points to estimate the gradient.
o Fifth-order uses five points to estimate the slope.
o The more points are used to estimate the slope, the better the solution accuracy.
o Ode45 is based on fifth-order Runge-Kutta method.
o Ode23 is based on second- and third-order Runge-Kutta methods.
o E.g., the classical third-order Runge-Kutta method is

1
yi 1  yi  h(k1  4k2  k3 ) ...........................................(9.0)
6
o Where
h 1
k1  f ( xi , yi ), k2  f ( xi ,  , yi  k1h) and k3  f ( xi ,  h, yi  k1h  2k 2h)....(9.0)
2 2
Solution of ordinary differential equations in MATLAB
o Stiff differential equations contain terms that vary at considerably different rates.
 E.g., solution containing the terms 𝑒 −𝑎𝑡 and 𝑒 −𝑏𝑡 with 𝑎 much larger that 𝑏.
 Then the first term decays to zero much faster than the second term.
 Some numerical methods such as ode45 in this case becomes unstable, and
 require an unreasonably small time step for the method to be stable.
Using ode45
o Can be used for both first-order and higher-order differential equations.
o Higher-order differential equations requires transformation to a system of first-order
ODEs.
o Done using state variables and resulting equations are state-variable equations.
Solution of ordinary differential equations in MATLAB

o E.g., consider a higher-order differential equation given below

3 y  y  5 y  3 y  e  x /2 .........................................(9.1)


o With initial conditions
y (0)  0, y(0)  1 and y(0)  1.................................(9.2)

o Number of state variables is the same as the number of initial conditions required or
specified.
o Hence, state variables are selected as those variables for which the initial conditions
are required.
o In example above, three initial conditions are specified, thus there must be three state
variables.
o Let these variables be u1  y, u2  y, and u3  y.................................(9.3)
Solution of ordinary differential equations in MATLAB

o State variables are naturally selected in this manner.


o Derivates are assigned after all non-derivates are selected.
o The state-variable equations must be three and are
u1  y  u2 .....................................................(9.4)

u2  y  u3 .....................................................(9.5)


1 1
u3  y  [ y  5 y  3 y  e ]  [u3  5u2  3u1  e  x /2 ]..........(9.6)
 x /2

3 3
o The equation can also be written in vector matrix form
 u1   y 
   
u  f ( x, u ), u  u2    y .....................................(9.7)
u   y
 3  
Solution of ordinary differential equations in MATLAB
o And  
 u2   0   u1 (0) 
     
f  u3  and u 0   1 
   2  ..............................(9.8)
u (0)
1   1  u (0) 
 [u3  5u2  3u1  e ]
 x /2    3 
3 
o Where 𝑢0 is a matrix vector containing initial conditions, since 𝑢1 0 = 𝑦 0 = 0,
𝑢2 0 = 𝑦 ′ (0) = 0 and 𝑢3 0 = 𝑦 ′′ (0) = 1
Using ode45 or other ODE solvers to solve above equation.
o MATLAB code for the ODE(s) can be written using a script file or
o Function file and a script file to run the code in the function file.
o Either way creates a .mfile, alternately code can be executed in command window.
o Inline method may be used in either case.
Solution of ordinary differential equations in MATLAB

o For inline method, the MATLAB code for Eq. 9.8 is as below

Fig. 9.2 MATLAB code for inline method

o First line specifies the initial conditions and must include all initial conditions for the
state variables.
o The second line specifies the time span over which the differential equations must
be solved (integrated).
o The third line is the function definition, f is a function handle.
o The procedure for defining a function in MATLAB using inline method is as follows:
Solution of ordinary differential equations in MATLAB

o Given a differential equation of the general form.


dy
 A  Bf ( x)..................................................(9.9)
dt
o Define the function handle to represent the function on RHS.
o Any letter or combination of letters can be chosen for function handle, e.g.,
dy dy
F or odeFun = ..................................................(9.10)
dt dt
o The code is then written to define the function, syntax for this is
F  @(t , y )[expression on RHS of ode]; ............................(9.11)

o For Eq. 9.9, the function definition becomes


F  @(t , y )[ A  Bf ( x)]; or odeFun  @(t , y )[ A  Bf ( x)]; ......(9.12)
Solution of ordinary differential equations in MATLAB

o 𝒕 is the independent variable and 𝒚 is the dependent variable.


o Both are inputs to the function F.
o Function must take a value of t and an initial value of y to compute the new value of
y in an iterative manner.
o Thus, for the system given by Eq. 9.8, the inline code is
f  @( x, u )[u (2); u (3); (u (3)  5* u (2)  3* u (1)  exp(- x / 2)) / 3];....(9.13)

o Where f is the function handle.


o ode45 can then be used to solve the ode, of which the syntax is
[t , y ]  ode45(Function, t-span, intial conditions); .................................(9.14)
Solution of ordinary differential equations in MATLAB

o Ode45 requires the three inputs in this case: the function, time span and the initial
conditions.
o Time span is the provides the evaluation points for the numerical method.
o The time step size is defined by the time interval used.
o The ode45 returns t as a column matrix (n x 1).
o 𝑦 is the numerical solution array returned as a column vector for each state variable.
o Thus, if there are 𝑚 state variables, then the solution array is an 𝑛 × 𝑚 array.
o Each row in 𝑦 is a solution at a corresponding row in 𝑡.
Solution of ordinary differential equations in MATLAB

o For Eq. 9.14, the exact ode45 code is

[ x, u ]  ode45( f , x, u 0); ............................................(9.15)


o The last line is for displaying the values of 𝑢1 which are values of 𝑦 and is the
required solution.
o The solution can also be plotted by addition the code
plot ( x, u (:,1); .............................................................(9.16)

o The complete code in Fig. 9.2 can be typed in the command window to execute it.
o It can also be created as a .mfile.
o Done by using “new – scrip” below the home tab.
Effect of integral time on response
Example
o Consider the system of differential equations for the stirred tank heater
dx 1 1 y dy K
 (Ti  Ti ,s )  x  and   C x...................(9.17)
dt   Vc p dt I
o Where
x  T  Ts ,   Ts - T , x   and y  Q  Qs ..............(9.18)

o So that at 𝑡 = 0, the system is at steady state and initial conditions are

x(0)  0 and y (0)  0...................................................(9.19)


o State variables 𝑥 and 𝑦 can be represented by 𝑢1 and 𝑢2 so that 9.17 becomes
du1 1 1 u du2 K
 (Ti  Ti ,s )  u1  2 and   C u1...................(9.20)
dt   Vc p dt I
Effect of integral time on response
o With the initial conditions
u1 (0)  0 and u2 (0)  0...................................................(9.21)

o The following constants and parameters can be defined

1 1 1 KC
a  (Ti  Ti ,s ), b , c and d ............(9.22)
  Vc p I

o Then from Eq. 19.20, we get

du1 du2
 a  bu1  cu2 and  du1.........................(9.23)
dt dt

o The MATLAB code for Eq. 9.23 is shown below


Effect of integral time on response

Fig. 9.3 MATLAB code for Eq. 9.23


Effect of integral time on response

o In Fig. 9.3, lines 5 to 14 contains the constants defined in the model.


o Lines 17 to 21 are the defined explicit equations, i.e., 9.22 and 𝜏 = 𝑉Τ𝐹 .
o Lines in green starting with % are comments that make the code easier to read.
o Line 24 in Fig. 9.3 is the initial conditions, Eq. 9.21.
o Function handle used is “I-only” so that the function definition is as shown in line 26.
o The state variable 𝑢 is a matrix containing two state variables 𝑢1 and 𝑢2 .
o Ode45 is used to solve this equation as shown by line 30.
o Remaining code are commands for plotting, addition labels and legend to the graph.
o Result of running the code in Fig. 9.3 is shown in Fig. 9.4.
Effect of integral time on response

Fig. 9.3 MATLAB code for Eq. 9.23 cont’d…


Effect of integral time on response

(a) (b)

Fig. 9.4 Response of STH temperature (a) no control and (b) Integral control
Effect of integral time on response

o To solve the above system of equations for various values of 𝜏𝐼 .


o The different values 𝜏𝐼 changes the equations so that two additional equations are
introduced.
o 𝑎, 𝑏 and 𝑐 in Eq. 9.22 do not contain integral time, thus remain the same but 𝑑
changes as
KC KC KC
d1  , d2  and d3  .......................................(9.24)
 I ,1  I ,2  I ,3
o The two system of equations in 9.23 are coupled.
o Thus if second Eq. changes so that its solution is different, this affects the solution of
the first equation.
o Thus, resulting system of equations contains six differential equations, each with a
unique solution.
Effect of integral time on response
du1 du2
 a  bu1  cu2 and  d1u1.........................(9.25)
dt dt

du3 du4
 a  bu3  cu4 and  d 2u3 .........................(9.26)
dt dt

du5 du6
 a  bu5  cu6 and  d3u5 .........................(9.27)
dt dt

o 𝑢 then becomes a matrix containing six state variables 𝑢1, 𝑢2 , 𝑢3 , 𝑢4 , 𝑢5 and 𝑢6

o The code in Fig. 9.3 then modifies to that shown in Fig. 9.4.
o Note that, three values of 𝜏𝐼 = 1, 5 𝑎𝑛𝑑 20 are defined.
o Three corresponding values of the parameter 𝑐 are also defined (lines 24 to 26).
Effect of integral time on response

Fig. 9.4 MATLAB code for Eq.


9.25 - 27
Effect of integral time on response

Fig. 9.4 MATLAB code for Eq.


9.25 - 27 cont’d…
Effect of integral time on response

o Six initial conditions corresponding to the six state variables are specified in line 30.
o The function definition contains the expressions for the six ODEs, line 32.
o 𝑢1 , 𝑢3 and 𝑢5 corresponds to 𝑥1 , 𝑥3 and 𝑥5 where 𝑥 = 𝑇 − 𝑇𝑠 .
o Thus 𝑢1 = 𝑥1 = 𝑇1 − 𝑇𝑠 or 𝑇1 = 𝑇𝑠 + 𝑢1 , line 37.
o 𝑇1 is the temperature of the STH liquid when 𝜏𝐼 = 1.
o The temperatures when 𝜏𝐼 = 5 and 20 are defined in a similar manner in lines 38
and 39.
o 𝑢2 = 𝑦 = 𝑄 − 𝑄𝑠 and its plot represents how heat supply varies with temperature.
o Note: Eqs are solved for 𝐾𝑐 = 0.5.
Effect of integral time on response

o The results after running the code in Fig. 9.4 are shown in Fig. 9.5

Fig. 9.5 Effect of integral time on the response of the liquid temperature for Kc = 0.5
Using function file and a script file to solve ODEs
o ODEs for the STH will be solved to determine the effect of integral time when PI control
is used.
o Method of creating a function file and running the code in the function file will be
employed.
o For PI control, the resulting system of equations is
t
dx
 a  bx  cy and y  Q  Qs   K C x  d  xdt ............(9.28)
dt 0
o Where
1 1 1 KC
a  (Ti  Ti ,s ), b , c and d ............(9.29)
  Vc p I
o The second Eq. of 9.28 can be expressed in derivative form
dx dy dx
 a  bx  cy and   KC  dx.................(9.30)
dt dt dt
Using function file and a script file to solve ODEs
o RHS of second Eq. in 9.30 can be written as algebraic expression
dx dy
 a  bx  cy and   K C  a  bx  cy   dx.................(9.31)
dt dt
o Or
dx dy
 a  bx  cy and  aK C  bK C x  cK C y  dx.................(9.32)
dt dt

o Eq. 9.32 consists the system of first-order ODEs that can be solved in MATLAB.
o Effect of 𝜏𝐼 can be by defining three values of 𝜏𝐼 so that constant 𝑑 becomes
KC KC KC
d1  , d2  and d3  .......................................(9.33)
 I ,1  I ,2  I ,3
o The different values of 𝑑 changes second Eq. in 9.32, in turn first Eq. also changes.
o Result is a system containing a set of six ODEs, Eqs. 9.34, 9.35 and 9.36
Using function file and a script file to solve ODEs
dx1 dy1
 a  bx1  cy1 and  aK C  bK C x1  cK C y1  d1 x1........(9.34)
dt dt
dx2 dy2
 a  bx2  cy2 and  aK C  bK C x2  cK C y2  d 2 x2 .....(9.35)
dt dt
dx3 dy3
 a  bx3  cy3 and  aK C  bK C x3  cK C y3  d 2 x3 .....(9.36)
dt dt

o The state variables in Eq.  u1   x1 


u   y 
9.34 to 9.36 can be  2  1
defined in terms of 𝑢. u3   x2 
u       ..........................................(9.37)
o Then, is a matrix u4   y2 
containing six state u5   x3 
variables as    
u6   y3 
Using function file and a script file to solve ODEs

o Function file containing Eqs. 9.34 to 9.36 is then created.


o From the tools bar, go to New → Function, opens a new function window.
o The window contains a predefined structure of a function as shown.

Fig. 9.6 MATLAB function structure


Using function file and a script file to solve ODEs

o Output arguments contains the output of the function.


o Can be defined with any letter or letters that point to what the function does.
o The input arguments are the independent and dependent inputs i.e., t and y.
o Code for the function is enclosed between the definition “function” and “end”.
o The function file code for Eqs. 9.34 to 36 is given in Fig. 9.7.
o In Fig. 9.7, f is assigned to the output arguments and the function is given the name
“PI_effect_integral_time”
o This function name must also be the name of the function file when saving.
o This function name must also be the name of the function file when saving.
Using function file and a script file to solve ODEs

o The output f contains six outputs for each of the ODE and is a column vector.
o Outputs contained in f are defined as in line 41.
o The values of 𝜏𝐼 used are indicated in lines 16 to 18 and the corresponding values of 𝑑
in lines 27 to 29.
o Right hand of each differential eq. is written in terms 𝑑𝑥𝑑𝑡 and 𝑑𝑦𝑑𝑡 and is the output.
o Note: any letter can be used to define these outputs.
o However, the letter(s) used must be the same as the outputs defined in f.
o The Eqs are solved for K C = 1 and the corresponding script file to run the code in Fig.
9.7 is shown in Fig. 9.8.
Using function file and a script file to solve ODEs

o Script file contains time span, initial conditions and the ODE solver, ode45 in this case.
o Six initial conditions are specified corresponding to the six state variables.
o Function handle in the ODE solver must have same name as function in the function
file or name under which function file is saved.
o The result of running the script file is shown in Fig. 9.9.
o Fig. 9.9, as 𝜏𝐼 decreases, response becomes more oscillatory with longer settling time.
o The overshoot decreases as 𝜏𝐼 decreases.
o However, generally observed behaviour is that overshoot increases as 𝜏𝐼 decreases.
o Discrepancy could be due to dynamics of other neglected elements.
Using function file and a script file to solve ODEs

Fig. 9.7 Function file and code for Eqs.


9.34 to 9.36.
Using function file and a script file to solve ODEs

Fig. 9.7 Function file and code for Eqs.


9.34 to 9.36 cont’d…
Using function file and a script file to solve ODEs

Fig. 9.8 Script file to run code in


function file in Fig. 9.37
Using function file and a script file to solve ODEs

Fig. 9.9 Effect of integral time on the response under PI control with Kc = 1

You might also like