CH4404 Lecture 07 NumericalIntegration

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

3.

Numerical integration
Orest Shardt
Part 3. Numerical integration
• Numerical solutions for ODEs
• Excel and MATLAB

2
Simulating process dynamics

𝑑ℎ 𝑉ሶ𝑖𝑛 1
= − ℎ
𝑑𝑡 𝐴 𝜏
• Estimate derivative as:
𝑑ℎ ℎ 𝑡 + Δ𝑡 − ℎ(𝑡)

𝑑𝑡 Δ𝑡
• Then
ℎ 𝑡 + Δ𝑡 − ℎ(𝑡) 𝑉ሶ𝑖𝑛 (𝑡) 1
≈ − ℎ(𝑡)
Δ𝑡 𝐴 𝜏
𝑉ሶ𝑖𝑛 𝑡 1
ℎ 𝑡 + Δ𝑡 ≈ ℎ 𝑡 + Δ𝑡 − ℎ(𝑡)
𝐴 𝜏
• A way to estimate the evolution of ℎ
• Expect an accurate solution if Δ𝑡 is small enough

3
Simulating process dynamics

• In general, suppose the dynamics of a process is described by


𝑑𝑦
=𝑓 𝑦
𝑑𝑡
• Then
𝑦 𝑡 + Δ𝑡 ≈ 𝑦 𝑡 + Δ𝑡 𝑓 𝑦 𝑡
• Use it to solve for
𝑦 𝑡0 + 𝑘Δ𝑡 , 𝑘 = 1, 2, 3, …
• This is called explicit Euler integration
• Assume initial slope for each interval remains constant during the
interval

4
Simulating process dynamics

• How can we take into account changes in the derivative over


the interval Δ𝑡?
• Take a step forward by the Explicit Euler method:
𝑦෤ = 𝑦 𝑡 + Δ𝑡 𝑓 𝑦 𝑡
• Take the average of the slopes at 𝑦 and 𝑦:෤
𝑑𝑦 1
≈ 𝑓 𝑦 + 𝑓 𝑦෤
𝑑𝑡 2
• Then take a full step forward using the average slope:
Δ𝑡
𝑦 𝑡 + Δ𝑡 ≈ 𝑦 𝑡 + 𝑓 𝑦 + 𝑓 𝑦෤
2
• This is called Heun’s method

5
Simulating process dynamics

• Common techniques use 3 or 4 partial steps to calculate an


average value of the derivative during the integration interval
• RK4 and RK5 (RK: Runge-Kutta)

6
Higher order processes

• Suppose
𝑑2 𝑦 𝑑𝑦
= 𝑓 𝑦,
𝑑𝑡 2 𝑑𝑡
• Re-write as
𝑑 2 𝑦1
2
= 𝑓 𝑦1 , 𝑦2
𝑑𝑡
𝑑𝑦1
𝑦2 =
𝑑𝑡
• Then
𝑑𝑦2 𝑑 2 𝑦1
=
𝑑𝑡 𝑑𝑡 2
• We have two coupled first order equations:
𝑑𝑦1 𝑑𝑦2
= 𝑦2 , = 𝑓 𝑦1 , 𝑦2
𝑑𝑡 𝑑𝑡
• nth order differential equation: n coupled first order equations

7
Choosing the time step

• Need to choose Δ𝑡
• Small*: accurate but many calculations
• Large*: inaccurate but quick to calculate
• *compared to the time constant of the process

• Calculations are cheap


• Can often get good accuracy using many small timesteps

• Adaptive time-stepping
• Algorithms are available to automatically vary Δ𝑡 so that it is small when the derivative
is high and larger when the derivative is smaller
• For example, compare the values of RK4 and RK5, and use a smaller Δt if the
difference is too large
• Many libraries provide implementations
• Matlab: ode45
• Python: scipy.integrate.solve_ivp(fun, t_span, y0, method='RK45’, etc)

8
Stability

• For large Δ𝑡, the errors might be so significant that the


numerical solution diverges to ±∞ when the true solution is
known to remain bounded
• Called instability of the numerical method
• Use smaller Δ𝑡 or a different integration technique

9
Example

• Level change in two identical tanks


• Gains: 𝐾1 = 𝐾2 = 0.005 m/(L/s)
• Time constants 𝜏1 = 𝜏2 = 𝜏 = 25 s

• Tank 1:
𝑑ℎ1
𝜏 = 𝐾 𝑉ሶ𝑖𝑛,1 − ℎ1
𝑑𝑡
• Tank 2:
𝑑ℎ2
𝜏 = 𝐾𝑉ሶ𝑖𝑛,2 − ℎ2 , 𝑉ሶ𝑖𝑛,2 = ℎ1 /𝐾
𝑑𝑡
• Initial steady state:
ℎ1 = ℎ2 = 2 𝑚, 𝑉ሶ𝑖𝑛,1 = 400 𝐿/𝑠
• Change 𝑉ሶ𝑖𝑛,1 to 450 L/s at 𝑡 = 0
• Exact solution
ℎ2 𝑡 = 2.25 + exp(−𝑡/𝜏) −0.01𝑡 − 0.25

10
Example

11
Simulation of Process with Controller

• Part 2 of Experiment A
• Analyse the experimental data to determine process dynamics
• Design a controller
• Simulate the behaviour of the controlled process
• How does the level evolve over time if you change the level setpoint?
• See the effects of varying the controller’s parameters
• Matlab code on Brightspace (ode45)
• Use/modify it for your analysis

12
Next

• Controllers

13

You might also like