Professional Documents
Culture Documents
(Part II)
1/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Table of Contents
Extrapolated Difference
2 Numerical Integration
The Midpoint Rule
Simpson’s Formula
Romberg Integration
Gaussian Quadrature
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
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
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
Using h2 = π/6
3/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Introduction
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!
′ 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
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
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
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
Using h2 = π/6
4/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Newton’s Forward Differences
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...
7/11
Belay Sitotaw Goshu (Ass. Prof) Physics December 20, 2023
Cont...
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()
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
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
Example:
Suppose we have the following data points: [
] 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
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
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
M (b − a)5
Error =
180 n4
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
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
I2 (h2 ) + I1 (h1 )
c≈
h12 − h22
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
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
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
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.
Rπ
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
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
w 1 a0 + a 1 x + a 2 x 2 + a 3 x 3
−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
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
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
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
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
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