You are on page 1of 96

Computational Physics

(Part II)

Belay Sitotaw Goshu (Ass. Prof)

College of Natural and Computational Science


Dire Dawa University

December 20, 2023

1/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Table of Contents
Extrapolated Difference

1 Numerical Differentiation Methods in Python


Extrapolated Difference
Newton’s Forward and Backward Difference Interpolation

2 Numerical Integration
The Midpoint Rule
Simpson’s Formula
Romberg Integration
Gaussian Quadrature

3 Numerical Solution of Ordinary Differential Equations

2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Extrapolated Difference
Extrapolation refers to estimating an unknown value based on
extending a known sequence of values or facts.
To extrapolate is to infer something not explicitly stated from existing
information.
Interpolation is the act of estimating a value within two known values
that exist within a sequence of values.
Interpolation and extrapolation are two types of prediction in
mathematics.
Although interpolation and extrapolation sound similar and are both
methods of estimating hypothetical values, they have different
purposes and work well in different scenarios.

2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
These are some differences between interpolation and extrapolation:
Prefixes: One way to understand the difference between interpolation
and extrapolation is by assessing their prefixes. ”Inter” means
”among” and ”extra” means ”outside.”
Purpose: Interpolation is used to predict values that exist within a data
set, and extrapolation is used to predict values that fall outside of a
data set and use known values to predict unknown values. Often,
interpolation is more reliable than extrapolation, but both types of
prediction can be valuable for different purposes.
Methods: There are many methods that you can use to conduct both
interpolation and extrapolation, including linear and polynomial
methods of prediction. Often, you can conduct interpolation and
extrapolation by drawing lines or curves on graphs or using known
functions.
Extrapolation is defined as an estimation of a value based on
extending the known series or factors beyond the area that is certainly
known. In other words, extrapolation is a method in which the data
values are considered as points such as x1 , x2 , · · · , xn .
2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Richardson extrapolation is a sequence acceleration technique. It can
be applied anytime we a solid theoretical error estimate. We used
central difference method to calculate derivatives of functions
f (x1 + h) − f (x1 − h)
Dh =
2h
Using Taylor’s theorem to expand f (x + h) and f (x − h) we get,

h2 ′′ f ′′′ (x1 )
f (x1 + h) = f (x1 ) + f ′ (x1 )h + f (x1 ) + h3 + ···
2! 3!
h2 ′′ f ′′′ (x1 )
f (x1 − h) = f (x1 ) − f ′ (x1 )h +
f (x1 ) − h3 + ···
2! 3!
Subtracting the above two equations and yields

f ′′′ (x1 ) 3 f ′′′′′ (x1 ) 5


f (x1 + h) − f (x2 − h) = 2f ′ (x1 )h + 2 h +2 h + O(h7 )
3! 5!

2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
So,
f ′′′ (x1 ) 2 f ′′′′′ (x1 ) 4
Dn = f ′ (x1 ) + h + h + O(h6 )
6 120
f (x + h) − f (x − h)
f n (x) = +E
2h

D is exact derivatives
D1 Estimated derivative E1 : Error of the D = D1 + E 1
using h = h1 estimation D1
D2 : Estimated derivative E2 : Error of the D = D2 + E 2
using h = h2 estimation D2

D 1 + E1 = D 2 + E 2
If we used second-order differentiation to get D1 and D2 , than
E1 = O(h12 ), E2 = O(h22 )
2
h2

E1 h1
≈ 12 → E1 ≈ E2
E2 h2 h2 2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Substitute this into the first equation and solve for E2
D2 − D1
E2 =
(h1 /h2 )2 − 1

The value of D is given by


D2 − D1
D ≈ D2 +
(h1 /h1 )2 − 1

This estimate is or order O(h4 ), obtained by two O(h2 ) estimates. A


special case of h2 = h1 /2 results in

D2 − D1 4 1
D = D2 + = D2 − D1
3 3 3
Use Richardson Extrapolation to estimate the first derivative of sin(x) at
x = π/4 using step sizes of h1 = π/3 and h2 = π/6. Use centered
differences of O(h2 ).
2/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Using h1 = π/3

′ sin(π/4 + π/3) − sin(π/4 − π/3)


D1 = f (π/4) = = 0.584772601
2π/3

Using h2 = π/6

′ sin(π + π/6) − sin(π/4 − π/6)


D2 = f (π/6) = = 0.675237237
2 ∗ π/6)

Apply Richardson Extrapolation


′ 4 1 4 1
D = f (π/4) = D2 − D1 = ×0.584772601 = ×0.675237237 = 0.705392
3 3 3 3
Use Richardson Extrapolation to estimate the first derivative of cos(x) at
x = π/4 using step sizes of h1 = π/3 and h2 = π/6. Use centered
differences of O(h2 ).

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Introduction

We are witnessing an intensive use of numerical methods across


different modern fields of science and technology.
For example, when solving engineering problems, it is relatively
common to use the calculation of the derivative of a function.
Input values of functions are specified in the form of an
argument-value pair, which in large data arrays, can be significantly
data-intensive to process.
Fortunately, many problems are much easier to solve if you use the
derivative of a function, helping across different fields like economics,
image processing, marketing analysis, etc.
Computation of derivatives is often used in data analysis. For
example, when finding the optimum of the values of functions.
In this section, we examine how you can calculate the value of the
derivative using numerical methods in Python.
3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Numerical Differentiation
Numerical Differentiation
Forward Difference
Central Difference
Extrapolated Difference
Numerical Integration
Trapezoid Rule
Mid-Point Rule
Simpson’s Rule
Romberg Integration
Gaussian Quadrature
Learning outcome
After the end of this section, you should be able to obtain numerical
approximation of the first and second derivatives of the function

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Numerical Differentiation
This Section deals with ways of numerically approximating derivatives
of functions. One reason for dealing with this now is that we will use
it briefly in the next Section.
To derive an approximation for the derivative of f , we return to
Taylor series. For an arbitrary function f (x) the Taylor series of f
around a = xj is
′ ′′
f (xj )(x − xj )0 f (xj )(x − xj )1 f (xj )(x − xj )2
f (x) = + + + ···
0! 1! 2!
If x is on a grid of points with spacing h, we can compute the Taylor
series at x = xj+1 to get
′ ′′
f (xj )(xj+1 ) − xj )0 f (xj )(xj+1 − xj )1 f (xj )(xj+1 − xj )2
f (xj+1 ) = + + +· ·
0! 1! 2!

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

Substituting h = xj+1 − xj and solving for f (xj ) gives the equation
′′ ′′ ′′′′
!
′ f (xj+1 ) − f (xj ) f (xj )h f (xj )h2 f (xj )h3
f (xj ) = − + + ···
h 2! 3! 4!

The terms that are in parentheses,


′′ ′′ ′′′′
!
f (xj )h f (xj )h2 f (xj )h3
− + + ···
2! 3! 4!

, are called higher order terms of h.


The higher order terms can be rewritten as
′′ ′′ ′′′′
!
f (xj )h f (xj )h2 f (xj )h3
− + + · · · = h(α + ϵ(h))
2! 3! 4!

where α is some constant, and ϵ(h) is a function of h that goes to


zero as h goes to 0. 3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
You can verify with some algebra that this is true. We use the
abbreviation ′ O(h)′ for h(α + ϵ(h)), and in general, we use the
abbreviation ′ O(hp )′ to denote hp (α + ϵ(h)).
Substituting O(h) into the previous equations gives

′ f (xj+1 ) − f (xj )
f (xj ) = + O(h)
h
Here, O(h) describes the accuracy of the forward difference
formula for approximating derivatives.
This gives the forward difference formula for approximating
derivatives as
′ f (xj+1 ) − f (xj )
f (xj ) ≈
h
By computing the Taylor series around a = xj at x = xj−1 and again

solving for f (xj ), we get the backward difference formula

′ f (xj ) − f (xj−1 )
f (xj ) ≈ 3/11
Belay Sitotaw Goshu (Ass. Prof) Physics
h December 20, 2023
Cont...
We can construct an improved approximation of the derivative by
clever manipulation of Taylor series terms taken at different points.
To illustrate, we can compute the Taylor series around a = xj at both
xj+1 and xj−1 . Written out, these equations are

′ 1 ′′ 1 ′′′
f (xj+1 ) = f (xj ) + f (xj )h + f (xj )h2 + f (xj )h3 + · · ·
2 6
and
′ 1 ′′ 1 ′′′
f (xj−1 ) = f (xj ) − f (xj )h + f (xj )h2 − f (xj )h3 + · · ·
2 6
Subtracting the formulas above gives
′ 2 ′′′
f (xj+1 ) − f (xj−1 ) = 2f (xj ) + f (xj )h3 + · · ·
3

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

which when solved for f (xj ) gives the central difference formula

′ f (xj+1 ) − f (xj−1 )
f (xj ) =
2h
The Forward and Backward methods involve using the value of the
function at the point and the value at the next or previous point,
respectively.
The Central difference method, on the other hand, involves averaging
the Forward and Backward differences.
Table: Pros and Cons of Numerical differentiation

Methods Advantage Disadvantage


Forward Difference Easy to compute Less accurate
Backward Difference Simple mathematics Accuracy depends
on step-size selection
Central Difference Most accurate Complex math involved 3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Examples
Using Two point Forward difference, Backward difference, Central

difference formula numerical differentiation to find solution of f (1.10)
x 1 1.05 1.10 1.15 1.20 1.25 1.30
f(x) 1 1.0247 1.0488 1.0724 1.0955 1.1183 1.1402
Take Two-point FDF (Forward difference formula)
Take Two-point BDF (Backward difference formula)
Two Two-point CDF (Central difference formula)
Consider the function the function f (x) = x 3 , Calculate its first
derivative at point x = 3 numerically with the forward, backward, and
central finite difference formulas and using:
Points x = 2, x = 3, and x = 4.
points x = 2.75, x = 3, and x = 3.25
For finite difference:
df f (4) − f (3) 64 − 27 37 − 27
|x=3 = = = 37, error = | | = 37.04%
dx 4−3 1 27
3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
For backward difference
df f (3) − f (2) 27 − 8 19 − 27
|x=3 = = = 19, error = | | = 29.63%
dx 3−2 1 27
For central difference
df f (4) − f (2) 64 − 8 28 − 27
|x=3 = = = 28, error = | | = 3.04%
dx 4−2 2 27
The relative error is given by
Exact solution-Numerical solution
error = | |
Numerical solution
Use a forward difference, and the values of h shown, to approximate the
derivative of cos(x) at x = π/3 if
a) h = 0.1 and
b) h = 0.0001

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
The distance x of a runner from a fixed point is measured (in meters)
at intervals of half a second. The data obtained is
t 0.0 0.5 1.0 1.5 2.0
x 0.00 3.65 6.80 9.90 12.15
Use central differences to approximate the runner’s velocity at times
t = 0.5s, and t = 1.25s.
Find the derivative of cos θ using h = 0.01 at θ = 0 using analytical
and check your answer by python codes.
The following data gives the velocity of a particle for twenty two
seconds at an interval of five seconds. Find the initial acceleration
using the entire data using forward and backward differences:
Time[sec] 0 5 10 15 20
velocity[ms−1 ] 3 14 69 228 250

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
python
import numpy as np
def derivative(f,a,method='central',h=0.01):
'''Compute the difference formula for f'(a) with step size

Parameters
----------
f : function
Vectorized function of one variable
a : number
Compute derivative at x = a
method : string
Difference formula: 'forward', 'backward' or 'central'
h : number
Step size in difference formula

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont..
Compute derivative at x = a
method : string
Difference formula: 'forward', 'backward' or 'central'
h : number
Step size in difference formula
Returns
-------
float
Difference formula:
central: f(a+h) - f(a-h))/2h
forward: f(a+h) - f(a))/h
backward: f(a) - f(a-h))/h
'''

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
if method == 'central':
return (f(a + h) - f(a - h))/(2*h)
elif method == 'forward':
return (f(a + h) - f(a))/h
elif method == 'backward':
return (f(a) - f(a - h))/h
else:
raise ValueError("Method must be 'central',\\
'forward' or 'backward'.")
derivative(np.cos,0)
Let’s compute and plot the derivative of a complicated function
 2 x
4x + 2x + 1
y=
x + 2e x

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,6,100)
f = lambda x: ((4*x**2 + 2*x + 1)/(x + 2*np.exp(x)))**x
y = f(x)
dydx = derivative(f,x)
plt.figure(figsize=(12,5))
plt.plot(x,y,label='y=f(x)')
plt.plot(x,dydx,label="Central Difference y=f'(x)")
plt.legend()
plt.grid(True)
plt.show()

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont..
plot the derivative of sin(x)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,5*np.pi,100)
dydx = derivative(np.sin,x)
dYdx = np.cos(x)
plt.figure(figsize=(12,5))
plt.plot(x,dydx,'r.',label='Central Difference')
plt.plot(x,dYdx,'b',label='True Value')
plt.title('Central Difference Derivative of y = cos(x)')
plt.legend(loc='best')
plt.show()

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Extrapolated Difference
Extrapolation refers to estimating an unknown value based on
extending a known sequence of values or facts.
To extrapolate is to infer something not explicitly stated from existing
information.
Interpolation is the act of estimating a value within two known values
that exist within a sequence of values.
Interpolation and extrapolation are two types of prediction in
mathematics.
Although interpolation and extrapolation sound similar and are both
methods of estimating hypothetical values, they have different
purposes and work well in different scenarios.

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
These are some differences between interpolation and extrapolation:
Prefixes: One way to understand the difference between interpolation
and extrapolation is by assessing their prefixes. ”Inter” means
”among” and ”extra” means ”outside.”
Purpose: Interpolation is used to predict values that exist within a data
set, and extrapolation is used to predict values that fall outside of a
data set and use known values to predict unknown values. Often,
interpolation is more reliable than extrapolation, but both types of
prediction can be valuable for different purposes.
Methods: There are many methods that you can use to conduct both
interpolation and extrapolation, including linear and polynomial
methods of prediction. Often, you can conduct interpolation and
extrapolation by drawing lines or curves on graphs or using known
functions.
Extrapolation is defined as an estimation of a value based on
extending the known series or factors beyond the area that is certainly
known. In other words, extrapolation is a method in which the data
values are considered as points such as x1 , x2 , · · · , xn .
3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Richardson extrapolation is a sequence acceleration technique. It can
be applied anytime we a solid theoretical error estimate. We used
central difference method to calculate derivatives of functions
f (x1 + h) − f (x1 − h)
Dh =
2h
Using Taylor’s theorem to expand f (x + h) and f (x − h) we get,

h2 ′′ f ′′′ (x1 )
f (x1 + h) = f (x1 ) + f ′ (x1 )h + f (x1 ) + h3 + ···
2! 3!
h2 ′′ f ′′′ (x1 )
f (x1 − h) = f (x1 ) − f ′ (x1 )h +
f (x1 ) − h3 + ···
2! 3!
Subtracting the above two equations and yields

f ′′′ (x1 ) 3 f ′′′′′ (x1 ) 5


f (x1 + h) − f (x2 − h) = 2f ′ (x1 )h + 2 h +2 h + O(h7 )
3! 5!

3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
So,
f ′′′ (x1 ) 2 f ′′′′′ (x1 ) 4
Dn = f ′ (x1 ) + h + h + O(h6 )
6 120
f (x + h) − f (x − h)
f n (x) = +E
2h

D is exact derivatives
D1 Estimated derivative E1 : Error of the D = D1 + E 1
using h = h1 estimation D1
D2 : Estimated derivative E2 : Error of the D = D2 + E 2
using h = h2 estimation D2

D 1 + E1 = D 2 + E 2
If we used second-order differentiation to get D1 and D2 , than
E1 = O(h12 ), E2 = O(h22 )
2
h2

E1 h1
≈ 12 → E1 ≈ E2
E2 h2 h2 3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Substitute this into the first equation and solve for E2
D2 − D1
E2 =
(h1 /h2 )2 − 1

The value of D is given by


D2 − D1
D ≈ D2 +
(h1 /h1 )2 − 1

This estimate is or order O(h4 ), obtained by two O(h2 ) estimates. A


special case of h2 = h1 /2 results in

D2 − D1 4 1
D = D2 + = D2 − D1
3 3 3
Use Richardson Extrapolation to estimate the first derivative of sin(x) at
x = π/4 using step sizes of h1 = π/3 and h2 = π/6. Use centered
differences of O(h2 ).
3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Using h1 = π/3

′ sin(π/4 + π/3) − sin(π/4 − π/3)


D1 = f (π/4) = = 0.584772601
2π/3

Using h2 = π/6

′ sin(π + π/6) − sin(π/4 − π/6)


D2 = f (π/6) = = 0.675237237
2 ∗ π/6)

Apply Richardson Extrapolation


′ 4 1 4 1
D = f (π/4) = D2 − D1 = ×0.584772601 = ×0.675237237 = 0.705392
3 3 3 3
Use Richardson Extrapolation to estimate the first derivative of cos(x) at
x = π/4 using step sizes of h1 = π/3 and h2 = π/6. Use centered
differences of O(h2 ).

4/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Newton’s Forward Differences

Consider the function y = f (x) which is tabulated for the values


xi = xo + ih, where i = 0, 1, 2, · · · , n

p(p − 1) 2 p(p − 1)(p − 2) 3


y (x) = yo + p∆yo + ∆ yo + ∆ yo + · · ·
2! 3!
Differentiating both sides w.r.t. p, we have

dy 2p − 1) 2 3p 2 − 6p + 2 3
= ∆yo + ∆ yo + ∆ yo + · · ·
dp 2! 3!
x−xo dp 1
Since p = h , therefore, dx = h
Now
3p 2 − 6p + 2 3
 
dy dy dp 1 2p − 1) 2
= · = ∆yo + ∆ yo + ∆ yo + · · ·
dx dp dx h 2! 3!

4/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

At x = x0 , p = 0. Hence putting p = 0,
   
dy 1 1 2 1 3 1 4
= ∆yo − ∆ yo + ∆ yo + − ∆ yo + · · ·
dx xo h 2 3 4
Derivatives using Newton’s backward difference formula
Consider the function y = f (x) which is tabulated for the values
xi = xo + ih, where i = 0, 1, 2, · · · , n
p(p + 1) 2 p(p + 1)(p + 2) 3
y (x) = yn + p∇yn + ∇ yn + ∇ yn + · · ·
2! 3!
Differentiating both sides w.r.t. p, we have
dy 2p + 1) 2 3p 2 + 6p + 2 3
= ∇yn + ∇ yn + ∇ yn + · · ·
dp 2! 3!
x−xn dp 1
Since p = h , therefore, dx = h
Now 5/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Newton’s backward difference formula

3p 2 + 6p + 2 3
 
dy dy dp 1 2p + 1) 2
= · = ∇yn + ∇ yn + ∇ yn + · · ·
dx dp dx h 2! 3!

At x = xn , p = 0. Hence putting p = 0,
   
dy 1 1 2 1 3 1 4
= ∇yn + ∇ yn + ∇ yn + + ∇ yo + · · ·
dx xn h 2 3 4

Given that
x 1.0 1.1 1.2 1.3 1.4 1.5 1.6
f(x) 7.989 8.403 8.781 9.129 9.451 9.750 10.03

Find dy
dx at x = 1.1 and x = 1.6 using Forward and Backward
differences
6/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

Find solution using Newton’s Forward difference formula at x = 1895


x 1891 1901 1911 1921 1931
f(x) 46 60 81 93 101
The difference table is:
x y ∇yo ∇2 yo ∇3 yo ∇4 yo
1891 46
26
1901 60 -5
21 -4
1911 81 -9 9
12 5
1921 93 -4
8
1931 101

7/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

Let h = x − xo = 1901 − 1891 = 10, and p = x−xh =


o 1895−1891
10 = 0.4
According to Newton’s Forward formula, the value of f (1985) is given by

p(p − 1) 2 p(p − 1)(p − 2) 3 p(p − 1)(p − 2)(p


y (x) = yo +p∆yo + ∆ yo + ∆ yo +
2! 2! 3!
solve the value of y at x = 1985, where you can take values of ∆ from the
above table. In this example yo = 46, ∆yo = 20, ∆2 yo = −5, ∆2 yo = 2,
and ∆4 yo = −3
The value of y (1895) = 54.3528
Apply the backward difference to solve the problem
The elevation above a datum line of seven points of a road are given below:

x 0 300 600 900 1200 1500 1800


f(x) 135 149 157 201 205 193

Find the gradient of the road at the middle point.


8/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...

Here h = 300, x0 = 0, y0 = 135, we require the gradient dy /dx at


x = 900. p = x−x
h
o
The difference table is shown below
x y ∆yo ∆2 yo ∆3 yo ∆4 yo ∆5 yo ∆6 y o
0 135
14
300 149 -6
8 24
600 157 18 -50
26 -26 70
900 183 -8 20 -86
18 -6 -16
1200 201 -14 4
4 -2
1500 205 -16
-12
1800 193 9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Example
Consider the function f (x) = cos(x).

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont..
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-poster')
%matplotlib inline
# step size
h = 0.1
# define grid
x = np.arange(0, 2*np.pi, h)
# compute function
y = np.cos(x)
# compute vector of forward differences
forward_diff = np.diff(y)/h
# compute corresponding grid
x_diff = x[:-1:]
# compute exact solution
exact_solution = -np.sin(x_diff)
# Plot solution
plt.figure(figsize = (12, 8)) 9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
plt.plot(x_diff, forward_diff, '--', \
label = 'Finite difference approximation')
plt.plot(x_diff, exact_solution, \
label = 'Exact solution')
plt.legend()
plt.show()

# Compute max error between


# numerical derivative and exact solution
max_error = max(abs(exact_solution - forward_diff))
print(max_error)

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
generate forward difference table
# Reading number of unknowns
n = int(input('Enter number of data points: '))
# Making numpy array of n & n x n size and initializing
# to zero for storing x and y value along with differences of
x = np.zeros((n))
y = np.zeros((n,n))
# Reading data points
print('Enter data for x and y: ')

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i][0] = float(input( 'y['+str(i)+']='))
# Generating forward difference table
for i in range(1,n):
for j in range(0,n-i):
y[j][i] = y[j+1][i-1] - y[j][i-1]
print('\nFORWARD DIFFERENCE TABLE\n');
for i in range(0,n):
print('%0.2f' %(x[i]), end='')
for j in range(0, n-i):
print('\t\t%0.2f' %(y[i][j]), end='')
print()

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Certainly! Let’s explore numerical differentiation using three different
methods: Newton’s forward difference, backward difference, and center
difference. These methods allow us to approximate derivatives of a
function based on discrete data points.
Newton’s Forward Difference:
The forward difference formula for the first derivative is given by:

∆Y0 − 12 ∆2 Y0 + 31 ∆3 Y0 − 14 ∆4 Y0 + . . .
 
dy
[ ≈ ]
dx x=x0 h

For any value of (x):


  2t−1 2 3t 2 −6t+2 3 4t3−18t2+22t−6 4
dy ∆Y0 + 2! ∆ Y0 + 3! ∆ Y0 + 4! ∆ Y0 + ...
[ = ]
dx h

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Newton’s Backward Difference:
Similar to the forward difference, but with backward data points. The
formula for the first derivative is:
∆Y0 − 12 ∆2 Y0 + 31 ∆3 Y0 − 14 ∆4 Y0 + . . .
 
dy
[ ≈ ]
dx x=x0 h

Center Difference (Midpoint):


The center difference formula for the first derivative is:
3t 2 −6t+2 3
∆Y0 + 2t−1 2 ∆ Y0 + 4t3−18t2+22t−6 ∆4 Y0 + .
 
dy 2! ∆ Y0 + 3! 4!
[ ≈
dx x=x0 h

Example:
Suppose we have the following data points: [

xf (x) 0.0 1.0000 0.10.9975 0.2 0.9900 0.30.9776 0.4 0.8604

] 9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
We want to find the derivative at (x=0). Using Newton’s forward
difference:  
dy
[ = 0.25033]
dx x=0
And the second derivative:
 
d2y
[ = −9.67667]
dx2 x=0
Remember that these methods provide approximations, especially when
the data points are not very close together. Choose the method that best
suits your specific problem

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Numerical integration
It is assumed that already know the basics about integrals from
mathematics courses and that you want to use Python to find numerical
solutions.
The Indefinite Integral
The indefinite integral of f (x) is a FUNCTION F (x)
Z
f (x) = f (x)dx = F (x) + c

The Definite Integral


The definite integral of f (x) is a Number and represents the area
under the curve f (x) from x = a to x = b
R
Area under the curve f (x) = f (x)dx = F (x) + c
Given the function: f (x) = x 2 We know that the exact solution is

x3
Z
x 2 dx = +C
3
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
Evaluate the integral of f (x) = x 2 from a = 0 to b = 3
3
x3
Z
1 3
x 2 dx =

= 3 −0 =9
0 3 3

Check your answer using python codes


import matplotlib.pyplot as plt
import numpy as np
xstart = 0
xstop = 3,0
increment = 0.1
x = np.arange(xstart,xstop,increment)
y = x**2
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.axis([0, 1, 0, 1])
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
plt.fill_between(x,y)
plt.show()
Given y = f (x) the approximation of the Area (A) under the curve
can be found dividing the area up into rectangles and then summing
the contribution from all the rectangles
This is known as the Trapezoid rule.
Z b n−1
∆x X
f (x)dx =,→ A = yi+1 + yi
a 2
i=1
Z b
∆x
f (x)dx ≈ (f (x0 ) + f (xn ) + 2(f (x1 ) + f (x2 ) + · · · + f (xn−1 ))
a 2
Find the are of the curve defined by f (x) = x 2 between 0 and 1.
import numpy as np
import matplotlib.pyplot as plt
a = 0; b = 1
9/11
N = 10
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
x = np.linspace(a,b,N+1)
y = x**2;
y_right = y[1:]
y_left = y[:-1]
# Trapezoid Rule
dx = (b - a)/N
A = (dx/2) * np.sum(y_right + y_left)
print("A =", A)
Given a piston cylinder device (e.g., it could be a car or a motorcycle):
We want to find the work produced W in this piston cylinder device
The work produced will then be: W = P∆V
The work produced based on the change in volume from V1 to V2 is
then: Z V2
W = PdV
V1
nRT
We can use the ideal gas law: PV = nRT and i.e., P = V
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
We also assume that the piston contains 1 kmol of gas at 300K and
that the temperature is constant during the process.
where P = Pressure, V is the Volume, m3 , N is the Number of moles,
kmol, R is the Universal gas constant, 8.314 kJ/kmol K, and T is the
Temperature, K
This gives:
Z V2 Z V2
nRT
W = PdV = dV
V1 V1 V
Find the amount of work by the gas whose volume changes from
V1 = 2m3 to V2 = 8m3 assume that the piston contains 1 kmol of
gas at 300K and that the temperature is constant during the process.
Check your answer with the following python codes

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
from scipy import integrate
V1 = 2
V2 = 8
n = 1
R = 8.314
T = 300
def work_eq(V, n, R, T):
W = (n*R*T)/V
return W
W, e = integrate.quad(work_eq, V1, V2, args=(n, R, T,))
print("W =", W)
The area of each strip (trapezium) is found separately. Then the area
under the curve and the ordinates at x0 and xn is approximately equal to
the sum of the areas of the n trapeziums.

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Midpoint Rule
The definite integral of a function over an interval as the limit of
Riemann sums. In general, any Riemann sum of a function f (x) over
Rb
an interval [a,b] may be viewed as an estimate of a f (x)dx.
A Riemann sum of a function f (x) over an interval [a,b] is obtained
by selecting a partition
P = (x1 , x2 , · · · , xn , where a = x1 < x2 < · · · < xn = b
and a set
S = {x1∗ , x2∗ , · · · , xnn }
where xi−1 ≤ xi∗ ≤ xi+1
The Riemann P sum corresponding to the partition P and the set S is
given by n ni+1 f (xi∗ ∆x), where ∆xi = xi − xi−1 , the length of the
ith subinterval.
Assume that f (x) is continuous on [a,b]. Let n be a positive integer
and ∆x = b−a
n .

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
If [a,b] is divided into n subintervals, each of length ∆x, and mi is the
midpoint of the ith
R1
Use the midpoint rule to estimate 0 x 2 dx using four subintervals.
Compare the result with the actual value of this integral.
1−0
Each subinterval has length ∆x = 4 = 14 .
Therefore, the subintervals consist of [0, 14 ], [ 14 , 21 ], [ 12 , 34 ], and [ 43 , 1]
The midpoints of these subintervals are { 18 , 83 , 85 , 78 }.
Thus,
 
1 1 3 5 7
M4 = × f ( ) + f (1) + f ( ) + f ( ) + f ( )
4 8 8 8 8
1
Evaluate the integral off f (x) = x2
in the interval from [1, 3]

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
import numpy as np
def compositeMidpoint(f, a, b, n, demoMode=False):
h = (b-a)/n
x = np.linspace(a+h/2, b-h/2, n)
if demoMode:
print(f"With {a=}, {b=}, {n=}, {h=} and the nodes are
M_n = sum(f(x)) * h
return M_n
def f(x): return 1/x**2
a=1
b=3
I_exact = 2.0/3.0
M_10 = compositeMidpoint(f, a, b, 10, demoMode=True)
error = M_10 - I_exact
print(f"{M_10=}, {error=}")

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
print()
M_100 = compositeMidpoint(f, a, b, 100, demoMode=False)
error = M_100 - I_exact
print(f"{M_100=}, {error=}")
R2 2
Approximate the integral 0 e x dx using the composite midpoint rule
with ten step.
The following values of a function f are given:
x 0 0.3 0.6 0.9 1.2 1.5 1.8
f(x) 0.5 0.6 0.8 1.3 2 3.2 4.8
Use
R 1.8 the composite rectangle method to approximate the integral
0 f (x)dx

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Simpson’s Formula
Simpson’s Rule is a numerical method for approximating the integral
of a function between two limits, a and b.
It’s based on knowing the area under a parabola, or a plane curve. In
this rule, N is an even number and
(b − a)
h=
N
.
Simpson’s Rule is a numerical method for approximating the integral
of a function between two limits, a and b.
It’s based on knowing the area under a parabola, or a plane curve. In
this rule, N is an even number and
(b − a)
h=
N
.
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
The y values are the function evaluated at equally spaced x values
between a and b.
Simpson’s rule is used to find the estimated value of a definite integral
Z b
f (x)dx
a

The area under a curve (a definite integral) by dividing the area under
the curve into rectangles whereas while using Simpson’s rule.
Simpson’s 1/3 Rules
Z x0 +nh
h
f (x)dx = ((f (x0 ) + f (xn ) + 4(f (x1 ) +
x0 3

...f(xn−1 )) + 2(f (x2 ) + ... + f (xn−2 )


This is known as the Simpson’s one-third rule or simply Simpson’s
rule and is most commonly used.
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
is an even number which is the number of subintervals that the
interval [a, b] should be divided into.
x0 = a and xn = b
b−a
h=
n
x0 , x1 , · · · , xn are the ends of the n subintervals.
Simpson’s rule gives just an approximate value of the integral, not the
exact value. So there is always an error that can be calculated using
the following formula.

M (b − a)5
Error =
180 n4

where |f (4) (x)| ≤ M

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
Simpson’s 3/8 rule
Z b
3h
f (x)dx = (f (x0 ) + f (xn ) + 2(f (x3 ) + f (x6 ) + · · · f (xn−3 )) +
a 8

3(f(x1 ) + f (x2 ) + · · · + f (xn−1 ))


Find the solution using Simpson’s 3/8 rule
x 1.4 1.6 1.8 2.0 2.2
f(x) 4.055 4.935 6.044 7.389 9.025
The vertical distance in meters covered
h by a rocketifrom t=8 to t
R 32 140000
=32 sec is given by S = 8 2000 ln 140000−2100×t − 9.8tdt
Use the Simpson’s 3/8 rule to find the approximate vale of the
distance when n =4
The following values of a function f are given:
x -18 -12 -6 0 6 12 18
f(x) 0 2.6 3.2 4.8 5.6 6 6.2
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Use the Simpson’s 1/3 (composite rectangle) method to approximate the
R 18
integral −18 f (x)dx
R 6 dx
Evaluate 0 1+x 2

Trapezoidal rule,
Simpson’s 1/3 rule,
Simpson’s 3/8 rule, by dividing the interval (0, 6) into six parts each of
width h = 1.
R 1 x2
Evaluate the integral 0 1+x 3 using Simpson’s 1/3 rule. Compare the
error with the exact value by dividing the interval (0, 1) into 4 equal
parts
R2 2
Use the Trapezoidal rule to estimate the integral 0 e x dx taking the
number 10 intervals.
The velocity v(km/min) of a moped which starts from rest, is given
at fixed intervals of time t (min) as follows:
t 2 4 6 8 10 12 14 16 18 20
v 10 18 25 29 32 20 11 5 2 0
Estimate approximately the distance covered in twenty minutes 9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
A solid of revolution is formed by rotating about the x-axis, the area
between the x-axis, the lines x = 0 and x = 1 and a curve through
the points with the following co-ordinates:
x 0.00 0.25 0.50 0.75 1.00
y 1.0000 0.9896 0.9589 0.9089 0.8415
Estimate the volume of the solid formed using Simpson’s rule.
The velocity v of a particle at distance s from a point on its path is
given by the table:
s(ft) 0 10 20 30 40 50 60
v(ft/s) 47 58 64 65 61 52 38
Estimate the time taken to travel sixty feet by using Simpson’s 1/3
rule. Compare the result with Simpson’s 3/8 rule.

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Romberg Integration
Romberg Integration is an extrapolation formula of the Trapezoidal
Rule for integration. It provides a better approximation of the integral
by reducing the True Error.
The truncation error in a multi-segment Trapezoidal Rule with n
segments for an integral
Z b
I = f (x)dx
a

is given by
1
E ≈− (b − a)2 h2 f¯′′
12
If I1 (h1 ) is is the approximate value of the integral from Trapezoidal
rule, and is the truncation error with a step side h1 we can write the
following
I ≈ I1 (h1 + E1 (h1 ) ≈ I1 (h1 ) + ch12
where c = − 1 (b − a)2 h2 f¯′′
12
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont....
Similarly if we take another step h2 we can write

I ≈ I2 (h2 ) = E2 (h2 ) ≈ I2 (h2 ) + ch22

Combining the above two equations and we can solve for c

I2 (h2 ) + I1 (h1 )
c≈
h12 − h22

Substituting these equation and we obtain an improved estimate of


the integral as:
I2 (h2 ) − I1 (h1 )
I ≈ I2 (h2 ) +
( hh21 )2 − 1
Can be shown that the error of this estimate is now O(h4 ) compared
to O(h2 ) for regular trapezoidal rule.
For the special case where the interval is halved, h2 = h1/2

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
I2 (h2 ) − I1 (h1 ) 4 1
I ≈ I2 (h2 ) + ≈ I2 (h2 ) − I1 (h1 )
3 3 3

Each successive application of the Richardson extrapolation to


Trapezoidal Rule eliminates the leading terms in the error estimate.

Thus, if the first application of Richardson produces an error of order


O(h4 ) , the next application will produce an error of order O(h2 ), and
so on.

The general formulation of this procedure on trapezoidal rule is


known as the Romberg Integration

If I0,n and I0, n2 indicate the values of the integral computed by the
trapezoidal rule with n and n2 segments

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Then the improved value of the integral given by Richardson’s
extrapolation is
41 I0,n − I0, n2
I1,n =
41 − 1
If the extrapolation is applied the second time

42 I1,n − I1, n2
I2,n =
42 − 1
When the extrapolation process is repeated k times, we can
generalize the formula as:

4k Ik−1,n − Ik−1, n2
Ik,n =
4k − 1
By applying a proper number of Richardson’s extrapolations, any
desired accuracy may be obtained

|Ik,n − Ik−1,n | ≤ ϵ 9/11


Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Romberg integration is a convenient way of carrying out this
procedure of successively eliminating terms in the expression for the
error. One proceeds as follows.
Start with 1 interval and compute I0,1
Then do two intervals and compute I0,2 from which I1,2 can be
determined
Now we use the trapezoidal rule several times successively halving h
and apply to each pair of values as per the following scheme:

I(h)
I(h/2)
I(h/2) I(h, h/2, h/4)
I(h/2, h/4) I(h, h/2, h/4, h/8)
I(h/4) I(h/2, h/4, h/8)
I(h/4), h/8)
I(h/8)
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
The computation is continued until successive values are close to each
oth- er. This method is called Richardson’s deferred approach to the
limit and its systematic refinement is called Romberg’s method.
R1 1
Evaluate 0 1+x dx correct to three decimal places using Romberg’s
method. Hence find the value of ln 2.
Solution Taking h = 0.5, 0.25, and 0.125 successively, let us evaluate
the given integral by the Trapezoidal rule.
When h = 0.5, the values of y = (1 + x)−1 are
x 0 0.5 1
y 1 0.6666 0.5
0.5
I (h) = (1 + 0.5 + 2 ∗ 0.666) = 0.7083
2
When h = 0.25, the values of y = (1 + x)−1 are
x 0 0.25 0.5 0.75 1
y 1 0.8 0.6666 0.5714 0.5

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

0.25
I (h/2) = (1 + 0.5 + 2 ∗ (0.8 + 0.666 + 0.5714 + 0.5)) = 0.697
2

When h = 0.125, the values of y = (1 + x)−1 are


x 0 0.125 0.25 0.375 0.5 0.75 0.875 1
y 1 0.8889 0.8 0.7272 0.6666 0.5714 0.5333 0.5
0.125
I (h/4) = (1 + 0.5 + 2 ∗ (0.89 + 0.8 + 0.73 + 0.6667 + 0.57 + 0.53 + 0.
2
Using Romberg’s formulae, we obtain
4 1 4 1
I (h, h/2) = I (h/2) − I (h) = ∗ 0.697 − ∗ 0.7803 = 0.6931
3 3 3 3
4 1 4 1
I (h, h/2, h.4) = I (h/2, h/4)− I (h, h/2) = ∗0.6931− ∗0.6932 = 0.6931
3 3 3 2

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
R1 1
Hence the value of the integral 0 1+x dx = 0.693
# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
gf = lambda x: np.exp(-x**2) + 1 / np.sqrt(np.pi)
# using scipy.integrate.romberg()
Rom = integrate.romberg(gf, 1, 2, show = True)
print(Rom)
The final result is 0.6994468414978009 after 33 function.

Use Romberg integration method evaluate 0 sinxdx
# import numpy and scipy.integrate
import numpy as np
from scipy import integrate
gf = lambda x: np.sin(x)
# using scipy.integrate.romberg()
Romk = integrate.romberg(gf, 0, 3.14, show = True)
print(Rom) 9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Gaussian Quadrature
In numerical analysis, a quadrature rule is an approximation of the
definite integral of a function, usually stated as a weighted sum of
function values at specified points within the domain of integration.
In general for Newton-Cotes (equispaced interpolation points/ data
points/ integration
Z xS
f (x)dx = h (w1 f1 (x1 ) + w2 f2 (x2 ) + ....wN fN (xN )) + E
xE

Note that for Newton-Cotes formulae only the weighting coefficients


were unknown wi and the xi were fixed
However the number of and placement of the integration points
influences the accuracy of the Newton-Cotes formulae:
N is even N th degree interpolation function exactly integrates an
degree polynomial (N + 1)th degree. This is due to the placement of
one of the data points.
N odd N th degree interpolation function exactly integrates an degree
polynomial.
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
Since a general polynomial of degree 2n-1 has 2n coefficients, it is
possible to find a Gauss integration scheme with n number of
integration points and n number of associated weights to exactly
integrate that polynomial function on the interval [-1, 1].
The following figure illustrates the concept of using the Gauss
integration points to calculate the area under the curve for
polynomials.

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont....

Figure: Caption
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
For a first-degree polynomial, 2n − 1 = 1, therefore, n = 1, so, 1
integration point is sufficient as will be shown here. Consider
f (x) = ax + b with a,b ∈ R, then:
Z 1
a
f (x)dx = 2b + x 2 |1−1 = 2b = 2f (0)
−1 2

So, for functions that are very close to being affine, a numerical
integration scheme with 1 integration point that is x1 = 0 with an
associated weight of 2 can be employed. In other words, a one-point
numerical integration scheme has the form:
Z 1
f (x)dx = 2f (0)
−1

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
For a third-degree polynomial, 2n − 1 = 3, therefore, n = 2, so, 2
integration points are sufficient as will be shown here. Consider
f (x) = a0 + a1 x + a2 x 2 + a3 x 3 , then:
Z 1
2
(a0 + a1 x + a2 x 2 + a3 x 3 )dx = 2a0 + a2
−1 3

Assuming 2 integration points in the Gauss integration scheme yields:


Z 1
(a0 + a1 x + a2 x 2 + a3 x 3 )dx = w1 a0 + a1 x + a2 x 2 + a3 x 3 +

−1

w 1 a0 + a 1 x + a 2 x 2 + a 3 x 3


For the Gauss integration scheme to yield accurate results, the


right-hand sides of the two equations above need to be equal for any
choice of a cubic function. Therefore the multipliers of a0 , a1 , a2 , and
a3 should be the same. So, four equations in four unknowns can be
written as follows:
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
w1 + w2 = 0
w1 x1 + w2 x2 = 0
2
w1 x12 + w22 =
3
w1 x13 + w2 x23 = 0

−1 √1 ,
The solution to the above four equations yields x1 = √
3
, x2 = 3
w 1 = w 2 = 1.
So, for functions that are very close to being cubic, the following
numerical integration scheme with 2 integration points can be
employed: Z 1
−1 1
I = f (x)dx ≈ f ( √ + f ( √ )
−1 3 3

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
For a fifth-degree polynomial, 2n − 1 = 5, therefore, n = 3, so, 3
integration points are sufficient to exactly integrate a fifth-degree
polynomial. Consider

f (x) = a0 + a1 x + a2 x 2 + a3 x 3 + a4 x 4 + a5 x 5

then
Z 1
2a2 2a4
(a0 + a1 x + a2 x 2 + a3 x 3 + a4 x 4 + a5 x 5 )dx = 2a0 + +
−1 3 5

Assuming 3 integration points in the Gauss integration scheme yields:


Z 1
f (x)dx = w1 (a0 + a1 x1 + a2 x22 + a3 x33 + a4 x44 + a5 x55 )+
−1

w2 (a0 + a1 x2 + a3 x22 + a3 x22 + a4 x42 + a5 x22 )+


w3 (a0 + a1 x3 + a2 x32 + a3 x32 + a4 x44 + a5 x52 )
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
For the Gauss integration scheme to yield accurate results, the
right-hand sides of the two equations above need to be equal for any
choice of a fifth-order polynomial function.
Therefore the multipliers of a0 , a1 , a2 , a3 , a4 , and a5 should be the
same. So, six equations in six unknowns can be written as follows:

w1 + w2 + w3 = 2






 w1 x1 + w2 x2 + w3 x3 = 0
w x 2 + w x 2 + w x 2 = 2

1 1 2 2 3 3 3
w x 3 + w x3 + w x3 = 0


 1 1 2 2 3 3
4 + w x4 + w x4 = 2




 w x
1 1 2 2 3 3 5
w x 5 + w x 5 + w x 5 = 0

1 1 2 2 3 3

q
The solution to the above six equations yields x1 = − 35 , x2 = 0,
q
x3 = 35 , w1 = 59 , w2 = 89 , and w3 = 95
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
So, for functions that are very close to being fifth-order polynomials,
the following numerical integration scheme with 3 integration points
can be employed:
Z 1 r r
5 3 8 5 3
f (x)dx ≈ f (− ) + f (0) + f ( )
−1 9 5 9 9 5

# import scipy.integrate.
from scipy import integrate
gfg = lambda x: x**8 + x**4
# using scipy.integrate.quadrature() method
geek = integrate.quadrature(gfg, 0.0, 1.8)
print(geek)
output: (25.81905715199999, 0.0)

9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
# import scipy.integrate.
from scipy import integrate
gfg = lambda x: x**2 + 2 * x + 4
# using scipy.integrate.quadrature() method
geek = integrate.quadrature(gfg, 0.0, 4.0)
print(geek)
Output: (53.33333333333333, 7.105427357601002e-15)
R2
Suppose that the following integral shall be calculated: 0 ax 2 + bx
from scipy.integrate import quad
def integrand(x, a, b):
return a*x**2 + b
a = 2
b = 1
I = quad(integrand, 0, 1, args=(a,b))
I
Output: (1.6666666666666667, 1.8503717077085944e-14)
9/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...

10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Numerical Solution of Ordinary Differential Equations

An ordinary differential equation (ODE) is an equation that involves


some ordinary derivatives (as opposed to partial derivatives) of a
function. Often,
our goal is to solve an ODE, i.e., determine what function or
functions satisfy the equation.
For example, if you are given
dx
= cos(t) (1)
dt
Since the antiderivative of cost is sin(t), then x(t) must be sin(t).

x(t) = sin(t) + c (2)

for some arbitrary constant c. You can verify that indeed x(t)
satisfies the equation dx
dt = cos(t).
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
The Euler Method
Let dS(t)
dt = F (t, S(t)) be an explicitly defined first order ODE. That
is, F is a function that returns the derivative, or change, of a state
given a time and state value.
Let t be a numerical grid of the interval [t0 , tf ] with spacing h.
Without loss of generality, we assume that t0 = 0, and that tf = Nh
for some positive integer, N.
The linear approximation of S(t) around tj at tj+1 is

dS(tj )
S(tj+1 ) = S(tj ) + (tj+1 − ti ) (3)
dt
which can also be written

S(tj+1 ) = S(tj ) + hF (tj , S(tj )) (4)

This formula is called the Explicit Euler Formula, and it allows us to


compute an approximation for the state at S(tj+1 ) given the state at
S(tj ) 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
The differential equation dfdt(t) = e −t with initial condition f0 = −1
has the exact solution f (t) = −e −t . Approximate the solution to this
initial value problem between 0 and 1 in increments of 0.1 using the
Explicity Euler Formula. Plot the difference between the
approximated solution and the exact solution.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-poster')
%matplotlib inline
# Define parameters
f = lambda t, s: np.exp(-t) # ODE
h = 0.1 # Step size
t = np.arange(0, 1 + h, h) # Numerical grid
s0 = -1 # Initial Condition
# Explicit Euler Method
s = np.zeros(len(t))
s[0] = s0
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
for i in range(0, len(t) - 1):
s[i + 1] = s[i] + h*f(t[i], s[i])
plt.figure(figsize = (12, 8))
plt.plot(t, s, 'bo--', label='Approximate')
plt.plot(t, -np.exp(-t), 'g', label='Exact')
plt.title('Approximate and Exact Solution \
for Simple ODE')
plt.xlabel('t')
plt.ylabel('f(t)')
plt.grid()
plt.legend(loc='lower right')
plt.show()
If we repeat the process for h=0.01, we get a better approximation for the
solution:

10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
h = 0.01 # Step size
t = np.arange(0, 1 + h, h) # Numerical grid
s0 = -1 # Initial Condition
# Explicit Euler Method
s = np.zeros(len(t))
s[0] = s0
for i in range(0, len(t) - 1):
s[i + 1] = s[i] + h*f(t[i], s[i])
plt.figure(figsize = (12, 8))
plt.plot(t, s, 'b--', label='Approximate')
plt.plot(t, -np.exp(-t), 'g', label='Exact')
plt.title('Approximate and Exact Solution \
for Simple ODE')
plt.xlabel('t')
plt.ylabel('f(t)')
plt.grid()
plt.legend(loc='lower right')
plt.show() 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Modified Euler Method
Modified Euler Method If we approximate the integration of the above
equation by Trapezoidal method , then the equation takes the form
hn hn
xn+1 = xn + hn f (tn + , xn + f (tn , xn )) (5)
2 2

1 Radioactive decay is governed by the equation dy dt = −ky . This is an


exponential decay equation and the exact solution is given by
y = y0 e −kt , where y0 is the initial value of y at t = 0. We shall solve
this equation by Euler method

import matplotlib.pyplot as plt


import numpy as np
# initial value of t, y
k,t,y=1,0.0,5.0
y_{0} =y
tt,yy=[],[]
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
t_{f}=10 # final value of x
dt=0.01 # step length
# defining function
def f(t,y,k):
return -k*y
# implementation of Euler method
while t<=t_{f}:
tt.append(t)
yy.append(y)
y=y+dt*f(t,y,k)
t+=dt
#calculation of exact result
tt=np.array(tt)
exact=y0*np.exp(-k*tt)
# Plotting solution with exact result
plt.subplot(2,1,1)
plt.plot(tt,yy,'k--',label="dt=%.4f"%(dt))
plt.plot(tt, y0*np.exp(-k*tt),'k',label="Exact solution") 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
plt.xlabel("time")
plt.ylabel("y")
plt.legend(loc='best')
# Plotting absolute error
diff=exact-yy
plt.subplot(2,1,2)
plt.plot(tt,diff,'k.',label="Absolute error")
plt.xlabel("time")
plt.ylabel("Error")
plt.legend(loc='best')
plt.title("ODE by Euler method")
plt.savefig("decay-euler.png")

10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Runge Kutta Methods
Runge Kutta (RK) methods are one of the most widely used methods
for solving ODEs. Recall that the Euler method uses the first two
terms in Taylor series to approximate the numerical integration, which
is linear:

S(tj+1 ) = S(tj+h ) = S(tj ) + hS (tj )
.
Second order Runge Kutta method: Let us first derive the second
order RK method. Let dS(t)
dt = F (t, S(t)), then we can assume an
integration formula the form of

S(tj+h ) = S(t1 ) + c1 F (t, S(t))h + c2 [F (t + ph, S(t)) + qhF (t, S(t))]h

We can attempt to find these parameters c1 , c2 , p, q by matching the


above equation to the second-order Taylor series, which gives us
′ 1 ′′
S(t + h) = S(t) + S (t)h + S (t)h2
2!
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
or
1 ′
S(t + h) = S(t) + F (t, S(t))h + F (t, S(t))h2
2!
′ ∂F ∂F ∂S
Noting that F (t, S(t)) = ∂t + ∂S ∂t
Placed in Taylor’s equation, we can rewrite the last term by applying
Taylor series in several variables, which gives us:
∂F ∂F
F [t + ph, S + qhF ] = F + ph + qh F
∂t ∂S
thus, the equation becomes:
 
∂F ∂F
S(t + h) = S(t) + (c1 + c2 )Fh + c1 p+q F h2
∂t ∂S

Comparing the above two equations, we can easily obtain:


1 1
c1 + c2 = 1, c2 p = , c2 q =
2 2
10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
cont...
Because the above equation has four unknowns and only three
equations, we can assign any value to one of the parameters and get
the rest of the parameters. One popular choice is:
1 1
c1 = .c2 = , p = q = 1
2 2
We can also define:
k1 = F (tj , S(tj ))
k2 = F (tj + ph, S(tj + qhk1 )
where we will have
1
S(tj+1 ) = S(tj ) + (k1 + k2 )
2
Fourth-order Runge Kutta method: A classical method for integrating
ODEs with a high order of accuracy is the Fourth Order Runge Kutta
(RK4) method. It is obtained from the Taylor series using similar
approach we just discussed in the second-order method. 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
python
This method uses four points k1 , k2 , k3 , and k4 . A weighted average
of these is used to produce the approximation of the solution. The
formula is as follows.
k1 = F (tj , S(tj ))
 
h 1
k2 = F tj + , S(tj ) + k1 h
2 2
 
h 1
k3 = F tj + , S(tj ) + k2 h
2 2
k4 = F (tj + h, S(tj ) + k3 h)
Therefore, we will have:
h
S(tj+1 = S(tj ) + (k1 + 2k2 + 2k3 + k4 )
6

10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Summary
Ordinary differential equations (ODEs) are equations that relate a
function to its derivatives, and initial value problems are a specific
kind of ODE solving problem.
Most initial value problems cannot be integrated explicitly and
therefore require numerical solutions.
There are explicit, implicit, and predictor-corrector methods for
numerically solving initial value problems.
The accuracy of the scheme used depends on its order of
approximation of the ODE.
The stability of the scheme used depends on the ODE, the scheme,
and the choice of the integration parameters.

10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
problems
a Python program that implements the Runge-Kutta 4th order
method for a given ODE:
import math
def rk4_solver(f, y0, t0, tf, h):
# Runge-Kutta 4th Order Method [By Bottom Science]
t = t0
y = y0
while t <= tf:
k1 = h * f(t, y)
k2 = h * f(t + h/2, y + k1/2)
k3 = h * f(t + h/2, y + k2/2)
k4 = h * f(t + h, y + k3)
y = y + (k1 + 2*k2 + 2*k3 + k4) / 6
t = t + h
return y
import numpy as np
def f(t, y):
return math.sin(t) - y 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
y0 = 0
t0 = 0
tf = 10
h = 0.1
y_sol = rk4_solver(f, y0, t0, tf, h)
print("Solution at t = ", tf, " is: ", y_sol)
a Python function that implements the Runge-Kutta 2nd order
method for a given ODE:
import math
def rk2_solver(f, y0, t0, tf, h):
# Runge-Kutta 2nd Order Method [By Bottom Science]
t = t0
y = y0
while t <= tf:
k1 = h * f(t, y)
k2 = h * f(t + h, y + k1)
y = y + (k1 + k2) / 2
t = t + h 10/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
return y
def f(t, y):
return math.sin(t) - y
y0 = 0
t0 = 0
tf = 10
h = 0.1
y_sol = rk2_solver(f, y0, t0, tf, h)
print("Solution at t = ", tf, " is: ", y_sol)

11/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
References I

11/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023

You might also like