Numerical Integration: Presented By: Amir Patel PHD (Mechatronics) Cape Town

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

Lecture 1:

Numerical Integration
13 Feb 2023

Presented by:
Amir Patel
PhD (Mechatronics) Cape Town
Electrical Engineering Department EEE4119F
University of Cape Town Mechatronics II
Overview

• First Order
• Second Order
• Midpoint Method
• Runge-Kutta
Method
Autonomous drifting
Simple First-Order

Example:

Solve this analytically:


• First re-arrange

• Integrate:
Simple First-Order

Assume initial condition:

• This means solution is: 𝑦 = 𝑒 −𝑡

• Initial condition is crucial:


• Sometimes called an Initial Value Problem
Discrete Derivatives
Lecture 1:

Position
Numerical
Integration

time
• Admin

• First Order • Remember, derivative of function→


• Second Order
slope of function
• Eg: Car on highway
• Midpoint Method

• Runge-Kutta
• On computer, represent continuous
Method
function using discrete
Computer Implementation

On a computer we represent as:

• How do we code that for our example?


Comparison
Euler Method
Lecture 1:
Numerical
Integration

• Admin

• First Order • We now have a way of calculating the


• Second Order
initial slope
• We can use this to extrapolate into
• Midpoint Method
the future 𝑑𝑦
• Runge-Kutta = −𝑦 𝑤𝑖𝑡ℎ 𝑦 𝑡 = 0 =1
• Eg. 𝑑𝑡
Method

• →Slope @t=0 is -1
Extrapolation

• Extrapolation is
good for t <0.2
• But horrible
thereafter!
Car Analogy

• Imagine you are driving…


• See a sign that next town
is 10 km away
• Your speedo says 60
km/h
• Predict 10mins journey
• But…
• Traffic lights?
• Slow-moving trucks?
Euler Algorithm
𝑑𝑦
= −𝑦 𝑤𝑖𝑡ℎ 𝑦 𝑡 = 0 =1
𝑑𝑡

• We generally want to take


small increments (∆𝑡), so:

𝑑𝑦
𝑦1 = 𝑦0 + ቤ ∆𝑡
𝑑𝑡 𝑡=0

• Specifically for this case:

𝑦1 = 𝑦0 − 𝑦0 ∆𝑡
Example (∆𝑡=0.5)

1. Start at y =1 with
slope = -1
2. Extrapolate to
(0.5), slope now
-0.5
3. Extrapolate to
(0.25), slope now
-0.25
4. …and so on!
Code
Taylor Series
Lecture 1:
Numerical
Integration

• Admin • Consider an arbitrary function (y)


• First Order • We can make an approximation:
• Second Order

• Midpoint Method
• Solve for coefficients
• Runge-Kutta
• Simplest approximation would be to
Method
use derivative:
Taylor Series

• We can make this better with second derivative:

• Add more derivatives:

• Called Taylor Series


Example: 𝒚 = 𝒆 −𝒕

exp(-t)

1-t

• Taylor series:

• All derivatives of y are 1 at origin


Error Analysis

• Initial value problem:

Euler method
Error term
• Only need to care about first term
• Why?

• Error scales with ∆𝒕


• If we half ∆𝒕 , then we half the error!
Euler Error
Code
Mass spring system
Lecture 1:
Numerical
Integration

• Admin • From Newton II:


• First Order

• Second Order

• Midpoint Method
• Convert to two 1st order equations:
• Runge-Kutta
Method
Euler (mass spring)

• Two 1st order DE’s:

• Euler:
Euler (mass spring)

• Write as vector:

• Then:

• Assuming spring constant/mass = 1


Code (euler solver)
Code (derivative)
Compare to analytical
A better derivative
Lecture 1:
Numerical
Integration

• Admin

• First Order

• Second Order
• We want a better estimate of the slope
between samples
• Midpoint Method
• Euler assumed the initial derivative
• Runge-Kutta
was held throughout step
Method
• This caused instability!
Approximate derivatives (y=e-x)

• Plot derivatives:
• Initial
• Final
• Middle

• Middle is the best!


Midpoint Method Error

• Extrapolation based on
midpoint gradient:

• Taylor series:

• Substitute:

• Exactly Taylor approximation of y for first three terms


• Much more accurate than Euler!
Problem…

• We don’t have the slope at the midpoint!


• Let’s use Euler:

∆𝒕
• Then using 𝒚𝑵+𝟏/𝟐 and , can calculate the derivative
𝟐
• Last step in algorithm is then:
𝒅𝒚
Exercise = −𝒚
𝒅𝒕

• Use large time-step (∆𝒕) = 1s


• with y(t=0) = 1 then 𝒅𝒚/𝒅𝒕0=-1

• Step1: Euler

• Step2: Deriv

• Step3: Shoot to end: 𝑦 ∆𝑡


𝑁+1 = 1 + ∆𝑡(−1 + )
2
Plot
Code

time+dt/2,yH)
Compare (Mass-spring)
𝒅𝒚
Error Plot ( = −𝒚)
𝒅𝒕
Code
Runge-Kutta Method
Lecture 1:
Numerical
Integration

• Admin

• First Order

• Second Order
• Most popular method
• 4th Order Runge-Kutta Method
• Midpoint Method
• Instead of shooting to midpoint,
• Runge-Kutta
shoot to 4 specific points
Method
RK4 Algorithm

Assume:

Then:
𝒅𝒚
Error Analysis ( = −𝒚)
𝒅𝒕

t^4
The end.

• Next lecture

You might also like