Professional Documents
Culture Documents
USING PYTHON
By
Samir Shrestha, PhD
Department of Mathematics
Kathmandu University, Dhulikhel
PYTHON LAB
Numerical Method
Implementaion
using
Python
Root Finding
Bisection Method
2
Question: Implement Bisection method to approximate a root of the equation 2𝑥 cos 2𝑥 − 𝑥 + 1 =0
correct to three decimal places that lies on the interval [-3, -2].
Algorithm
Step 1: Choose initial values for a and b and stopping criterion ,𝑒𝑝𝑠 > 0.
Step 2: Compute 𝑓𝑎 = 𝑓(𝑎) and 𝑓𝑏 = 𝑓(𝑏)
Step 3: If 𝑓𝑎 𝑓𝑏 > 0, 𝑎 and 𝑏 does not contain any root. Go to Step 1
Otherwise
𝑎+𝑏
Step 4: Compute 𝑥0 = and compute 𝑓0 = 𝑓(𝑥0)
2
Step 2: Choose starting point (𝑥0, 𝑦0) and stopping criterion 𝑒𝑝𝑠 > 0.
Step 3: Compute, x1 = 𝐹(𝑥0 , 𝑦0 ) and y1 = 𝐺(𝑥0 , 𝑦0 )
Step 4: If 𝑥1 − 𝑥0 < 𝑒𝑝𝑠 and 𝑦1 − 𝑦0 < 𝑒𝑝𝑠 STOP. (Solution achieved)
Otherwise
Update 𝑥0 = 𝑥1 and 𝑦0 = 𝑦1 and go to Step 3 and Step 4 and
continue the process until stopping criterion meet.
Interpolation
Forward Difference Table
Forward Difference Table
Question: Construct the forward difference table of following data using User Defined function and display the
output. 𝑥 15 20 25 30 35 40
𝑦 0.2588190 0.3420201 0.4226183 0.5 0.5735764 0.6427876
Algorithm
Step 1: Store the x-data into a list name x and y-data into a list name y
Step 2: Initialize a 2d-array D of size 𝑛 × (𝑛 + 1) as a zero matrix. Insert the 0th and 1st columns
of D by x and y.
Step 3: Fill the 2nd , 3rd and so on columns of D by finite differences of 1st order, 2nd order and so
on orders as follows:
for j = 2 to n+1
for i = 0 to n-j+1
D[i,j] = D[i+1,j-1]-D[i,j-1]
Step 4: Call the forward difference table function and print the table
Backward Difference Table
Backward Difference Table
Question: Construct the backward difference table of follwing data data using User Defined function and display
the output.
𝑥 15 20 25 30 35 40
𝑦 0.2588190 0.3420201 0.4226183 0.5 0.5735764 0.6427876
Algorithm
Step 1: Store the x-data into a list name x and y-data into a list name y
Step 3: Initialize a 2d-array D of size 𝑛 × (𝑛 + 1) as a zero matrix. Insert the 0th and 1st columns
of D by x and y.
Step 4: Fill the 2nd , 3rd and so on columns of D by finite differences of 1st order, 2nd order and so
on orders as follows:
for j = 2 to n+1
for i = 0 to n-j+1
D[i+j-1,j] = D[i+1+j-2,j-1]-D[i+1+j-2,j-1]
Step 4: Call the backward difference table function and print the table
Newton’s Interpolation Formula
Question: Write a program to estimate the value of the function 𝑓(0.1) and 𝑓(0.9) applying Newton’s forward and
backward interpolation polynomials using following table:
x 0.0 0.2 0.4 0.6 0.8 1.0
y 0.61 0.73 1.21 3.01 3.44 5.80
Algorithm
Step 1: Call forward difference and backward difference function with data x, y as input
and output as FD or BD
Step 2: Implement Newton’s Forward Interpolation to estimate the value of 𝑓(0.1)
𝛥𝑦0 𝛥2 𝑦0 𝛥3 𝑦0 𝛥𝑛 𝑦0
𝑦𝑛 𝑥 = 𝑦0 + 𝑝 + 𝑝 𝑝−1 + 𝑝 𝑝−1 𝑝−2 + ⋯+ 𝑝 𝑝 − 1 (𝑝 −
1! 2! 3! 𝑛!
Newton’s Forward Interpolation Formula
Question: Write a program to estimate the value of the function 𝑓(0.1) applying Newton’s forward interpolation
polynomials using following table:
x 0.0 0.2 0.4 0.6 0.8 1.0
y 0.61 0.73 1.21 3.01 3.44 5.80
Algorithm
Step 1: Call forward difference function data as an input and output as FD
Step 2: Implementing Newton’s Forward Interpolation to estimate the value of 𝑓 0.1 :
p = (0.21-x[0])/h
yp = 0
p_fact = 1
for i = 1 to n+1
yp = yp + p_fact* FD[0,i]
p_fact = p_fact*(p-(i-1))/factorial(i)
Print the interpolated value yp
Newton’s Backward Interpolation Formula
Question: Write a program to estimate the value of the function 𝑓(0.9) applying Newton’s backward interpolation
polynomials using following table:
x 0.0 0.2 0.4 0.6 0.8 1.0
y 0.61 0.73 1.21 3.01 3.44 5.80
Algorithm
Step 1: Call forward difference function data as an input and output as BD
Step 2: Implementing Newton’s Backward Interpolation to estimate the value of 𝑓 0.9 :
p = (0.29-x[-1])/h
yp = 0
p_fact = 1
for i = 1 to n+1
yp = yp + p_fact* BD[-1,i]
p_fact = p_fact*(p+(i-1))/factorial(i)
Print the interpolated value yp
Lagrange Interpolation
From following data points estimate the value of 𝑦 at 𝑥 = 2.5 using Lagrange Interpolation
formula. 𝑥 0 1 2 3 4
𝑦 0 1.7183 6.3891 19.0855 53.5982
The Lagrange Interpolating Polynomial of degree n that passes through the set of (n+1) points
𝑥𝑖 , 𝑦𝑖 , 𝑖 = 0,1,2, … , 𝑛 is given by
𝐿𝑛 𝑥 = 𝑛𝑖=0 𝑙𝑖 (𝑥)𝑦𝑖 ---------------(4)
where
𝑥 − 𝑥0 𝑥 − 𝑥1 ⋯ 𝑥 − 𝑥𝑖−1 𝑥 − 𝑥𝑖+1 ⋯ 𝑥 − 𝑥𝑛
𝑙𝑖 𝑥 =
𝑥𝑖 − 𝑥0 𝑥𝑖 − 𝑥1 ⋯ 𝑥𝑖 − 𝑥𝑖−1 𝑥𝑖 − 𝑥𝑖+1 ⋯ 𝑥𝑖 − 𝑥𝑛
Lagrange Interpolation
From following data points estimate the value of 𝑦 at 𝑥 = 2.5 using Lagrange Interpolation
formula. 𝑥 0 1 2 3 4
𝑦 0 1.7183 6.3891 19.0855 53.5982
Algorithm
Step 1: Read the 𝑥𝑖 and 𝑦𝑖 data in array/list with variable names x and y of n number of elements. Read the 𝑥𝑝 =
2.5 where interpolation to be done.
Step 2:
𝑥𝑝 = 2.5
Initialize 𝑦𝑝 = 0
for i = 1 to n
Set p = 1
for j = 1 to n
If 𝑖 ≠ 𝑗
Calculate 𝑝 = 𝑝 ∗ (𝑥𝑝 − 𝑥𝑗)/(𝑥𝑖 − 𝑥𝑗 )
Calculate 𝑦𝑝 = 𝑦𝑝 + 𝑝 ∗ 𝑦𝑖
Print the interpolated value 𝑦𝑝
Divided Difference Table
Divided Difference Table
From following data construct divided difference table using User Defined function and display
the output.
𝑥 -1 0 3 6 7
𝑦 3 -6 39 822 1611
Algorithm
Step 1: Read the 𝑥𝑖 and 𝑦𝑖 data in array/list with variable names x and y of n number of
elements.
Step 2: Initialize zero matrix D of size 𝑛 × (𝑛 + 1). Insert x and y in 1st and 2nd columns of
D by x and y
Step 3:
for j = 2 to n+1
for i =0 to n-j+1
D[i,j] = (D[i+1,j-1]-D[i,j-1])/(x[i+1+(j-2)]-x[i])
Step 4: Call the divided difference table function and print the table
Newton’s Divided Difference Interpolation
From following data points estimate the value of 𝑦 at 𝑥 = 2 using Newton’s General Interpolation
formula. 𝑥 -1 0 3 6 7
𝑦 3 -6 39 822 1611
𝑦 = 𝑦0 + 𝑥 − 𝑥0 𝑥0 , 𝑥1 + 𝑥 − 𝑥0 𝑥 − 𝑥1 𝑥0 , 𝑥1 , 𝑥2 +
𝑥 − 𝑥0 𝑥 − 𝑥1 𝑥 − 𝑥2 𝑥0 , 𝑥1 , 𝑥2 , 𝑥3 + ⋯
+ 𝑥 − 𝑥0 𝑥 − 𝑥1 … 𝑥 − 𝑥𝑛−1 𝑥0 , 𝑥1 , … , 𝑥𝑛
Newton’s Divided Difference Interpolation
From following data points estimate the value of 𝑦 at 𝑥 = 2 using Newton’s Divided Difference
Interpolation formula. 𝑥 -1 0 3 6 7
𝑦 3 -6 39 822 1611
Algorithm
Step 1: Call divided difference table function with data as an input and output as DD
Step 2: Implementing Newton’s Divided Interpolation to estimate the value of 𝑦 at 𝑥 = 2:
xp = 2
yp = 0
fact = 1
for i = 1 to n+1
yp = yp + fact* DD[0,i]
fact = fact*(xp-x[i-1])
Print the interpolated value yp
Numerical Differentation
Differentiation using Newton’s Forward Interpolation Formula
𝑑𝑦
Problem 1: Compute the first and second derivatives at the point 𝑥0 = 0 and 𝑥𝑛 = 6 using the following
𝑑𝑥
tabulated data using Newton’s interpolation formulas:
𝑥 0 1 2 3 4 5 6
𝑦 2 3 10 29 66 127 218
Differentiation Formulas
Differentiation using Newton’s Forward Interpolation Formula
𝑑𝑦
Problem 1: Compute the first and second derivatives at the point 𝑥0 = 0 using the following tabulated data
𝑑𝑥
using Newton’s forward interpolation formula:
𝑥 0 1 2 3 4 5 6
𝑦 2 3 10 29 66 127 218
Algorithm
Step 1: Call forward and backward difference table function with data as an input and output as
FD and BD respectively.
Step 2: Implementing Newton’s Forward and Backward Interpolation for derivative to
𝑑𝑦
estimate the value of
𝑑𝑥
Dy0 = 0
Dyn = 0
for i = 2 to n+1
Dy0 = Dy0 + (-1)**i*1/h*FD[0,i]*1/(i-1)
Dyn = Dyn + 1/h*BD[-1,i]*1/(i-1)
Print the interpolated value Dy0 and Dyn
Numerical Integration
TRAPEZOIDAL RULE
1 −𝑥 2
Problem: Evaluate the definite integral −1
𝑒 𝑑𝑥 correct to three decimal places by using
Trapezoidal Rule by taking n = 20 equal divisions of the interval [-1, 1]. Increase the value of n =
30, 40, 50, 100 and observes the value of integration.
Formula:
TRAPEZOIDAL RULE
1 −𝑥 2
Problem: Evaluate the definite integral −1
𝑒 𝑑𝑥 correct to three decimal places by using
Trapezoidal Rule by taking n = 20 equal divisions of the interval [-1, 1]. Increase the value of n =
30, 40, 50, 100 and observes the value of integration.
Algorithm
Step 1: Store the n = 20 division of the interval [-1, 1] into the array named as x and
−𝒙 𝟐
compute the corresponding function value of 𝒆 and store into the array named as y
Step 2:
n = length(x)
I = y[0] + y[n]
for i = 1 to n-1
I = I + 2*y[i]
I = h/2*I
Print the value of I
SIMPSON’S RULE
1 −𝑥 2
Problem: Evaluate the definite integral −1
𝑒 𝑑𝑥 correct to three decimal places by using Simpson’s
Rule by taking 20 equal divisions of the interval [-1, 1]. Increase the value of n = 30, 40, 50, 100 and
observes the value of integration.
Formula:
SIMPSON’S RULE
Algorithm
Step 1: Store the n = 20 division of the interval [-1, 1] into the array named as x and
−𝒙 𝟐
compute the corresponding function value of 𝒆 and store into the array named as y
Step 2:
n = length(x)
I = y[0] + y[n]
for i = 1 to n-1
if i is odd
I = I + 4*y[i]
else
I = I + 2*y[i]
I = h/3*I
Print the value of I
DOUBLE INTEGRATION: TRAPEZOIDAL and SIMPSON’S RULE
1 −(𝑥 2 +𝑦 2 )
Problem: Evaluate the definite integral −1
𝑒 𝑑𝑥𝑑𝑦
correct to three decimal places by using
Trapezoidal Rule by taking m = n = 20 equal divisions of the interval [-1, 1]. Increase the value of
m = n = 30, 40, 50, 100 and observes the value of integration.
1 2 2
Problem: Evaluate the definite integral −1 𝑒 −(𝑥 +𝑦 ) 𝑑𝑥𝑑𝑦 correct to three decimal places by using
Simpson’s Rule by taking m = n = 20 equal divisions of the interval [-1, 1]. Increase the value of
m = n = 30, 40, 50, 100 and observes the value of integration.
System of Linear Equations
JACOBI ITERATION METHOD
Problem: Solve the following system of linear equations using Jacobi Iteration Method:
10𝑥1 − 2𝑥2 − 𝑥3 − 𝑥4 = 3
−2𝑥1 + 10𝑥2 − 𝑥3 − 𝑥4 = 15
−𝑥1 − 2𝑥2 + 10𝑥3 − 2𝑥4 = 27
−𝑥1 − 𝑥2 − 2𝑥3 + 10𝑥4 = −9
Starting with initial guess 𝑥1 = 𝑥2 = 𝑥3 = 𝑥4 = 0
Method: We rewrite the equations into following form:
1
𝑥1 = (3 + 2𝑥2 + 𝑥3 + 𝑥4 )
10
1
𝑥2 = (15 +2𝑥1 +𝑥3 + 𝑥4 )
10
1
𝑥3 = (27 +𝑥1 +2𝑥2 + 2𝑥4 )
10
1
𝑥4 = (−9 +𝑥1 +𝑥2 + 2𝑥3 )
10
Starting with initial values 𝑥1 = 0, 𝑥2 = 0, 𝑥30 = 0, 𝑥40 = 0, we approximate the next value
0 0
Formula: 𝑦𝑖+1 = 𝑦𝑖 + 𝑘1 ,
where,
𝑘1 = ℎ𝑓 𝑥𝑖 , 𝑦𝑖 , 𝑖 = 0,1,2, ⋯ , 𝑛 − 1
Algorithm: Euler’s Method
1
Step 1: Construct the user defined function for f x, y = 3 − 2𝑥 − 𝑦 and initialize the arrays x
2
and y of size (n+1). Assign the initial condition x[0] = 0, y[0] = 1
Step 2:
for i = 0 to n-1
𝑘1 = ℎ𝑓(𝑥 𝑖 , 𝑦[𝑖])
y[i+1]= 𝑦[𝑖] + 𝑘1
x[i+1]= 𝑥[𝑖] + ℎ
End
Step3: Plot the points y versus x
𝑥
−2
Step 4: Plot the exact solution y 𝑥 = 14 − 4𝑥 − 13𝑒 and compare the approximated and
exact solutions taking the steps sizes ℎ = 0.5, 0.1, 0.01
MODIFIED EULER’S METHOD
or
HEUN’S METHOD
or
RUNGE-KUTTA 2ND ORDER METHOD
RUNGE-KUTTA 2ND ORDER METHOD
Example: Solve ODE (IVP)
𝑑𝑦 1
= 3 − 2𝑥 − 𝑦, 𝑦
0 = 1,
𝑑𝑥 2
by implementing Runge-Kutta 2nd order method to find the approximate solution on the interval 0 ≤
𝑥
−
𝑥 ≤ 2. Plot the solution and Compare the results with the exact solution y 𝑥 = 14 − 4𝑥 − 13𝑒 2 by
taking the steps sizes ℎ = 0.5, 0.1, 0.01
Formula: 1
𝑦𝑖+1 = 𝑦𝑖 + 𝑘1 + 𝑘2 ,
2
where,
𝑘1 = ℎ𝑓 𝑥𝑖 , 𝑦𝑖 ,
𝑘2 = ℎ𝑓 𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘1 , 𝑖 = 0,1, ⋯ , 𝑛 − 1
Algorithm: Runge-kutta 2nd Order Method
1
Step 1: Construct the user defined function for f x, y = 3 − 2𝑥 − 𝑦 and initialize the arrays x
2
and y of size (n+1). Assign the initial condition x[0] = 0, y[0] = 1
Step 2:
for i = 0 to n-1
𝑘1 = ℎ𝑓(𝑥 𝑖 , 𝑦[𝑖])
𝑘2 = ℎ𝑓(𝑥 𝑖 + ℎ, 𝑦 𝑖 + 𝑘1)
1
y[i+1]= 𝑦 𝑖 + (𝑘1 + 𝑘2)
2
x[i+1]= 𝑥[𝑖] + ℎ
End
Step3: Plot the points y versus x
𝑥
−
Step 4: Plot the exact solution y 𝑥 = 14 − 4𝑥 − 13𝑒 and compare the approximated and
2
Formula: 1
𝑦𝑖+1 = 𝑦𝑖 + 𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 ,
6
where,
𝑘1 = ℎ𝑓 𝑥𝑖 , 𝑦𝑖
ℎ 𝑘1
𝑘2 = ℎ𝑓 𝑥𝑖 + , 𝑦𝑖 +
2 2
ℎ 𝑘2
𝑘3 = ℎ𝑓 𝑥𝑖 + ,𝑦 +
2 𝑖 2
𝑘4 = ℎ𝑓 𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘3 , 𝑖 = 0,1, ⋯ , 𝑛 − 1
Algorithm: Runge-kutta 2nd Order Method
1
Step 1: Construct the user defined function for f x, y = 3 − 2𝑥 − 𝑦 and initialize the arrays x
2
and y of size (n+1). Assign the initial condition x[0] = 0, y[0] = 1
Step 2:
for i = 0 to n-1
k1= ℎ𝑓 𝑥 𝑖 , 𝑦 𝑖
ℎ 𝑘1
𝑘2 = ℎ𝑓 𝑥 𝑖 + ,𝑦𝑖 +
2 2
𝑘3 = ℎ𝑓(𝑥 𝑖 + ℎ/2, 𝑦 𝑖 + 𝑘2/2)
k4= ℎ𝑓(𝑥 𝑖 + ℎ, 𝑦 𝑖 + 𝑘3)
1
y[i+1]= 𝑦 𝑖 + (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4)
6
x[i+1]= 𝑥[𝑖] + ℎ
End
Step3: Plot the points y versus x
𝑥
−2
Step 4: Plot the exact solution y 𝑥 = 14 − 4𝑥 − 13𝑒 and compare the approximated and
exact solutions taking the steps sizes ℎ = 0.5, 0.1, 0.01
FINITE DIFFERENCE METHOD (FDM)
Second Order Differential Equation
(Boundary Value Problem)
Finite Difference Method (FDM) for ODE (BVP):
Let us consider the second order ordinary differential equation:
𝑦 ′′ 𝑥 + 𝑝 𝑥 𝑦 ′ 𝑥 + 𝑞 𝑥 𝑦 = 𝑟(𝑥)---------- (1)
with boundary conditions 𝑦 𝑥0 = 𝑦0 and 𝑦 𝑥𝑛 = 𝑦𝑛 .
We divide the interval [𝑥0 , 𝑥𝑛 ] into n-number of equal sub-intervals by
set of points 𝑥0 , 𝑥1 , ⋯ , 𝑥𝑛 such that 𝑥0 < 𝑥1 < ⋯ < 𝑥𝑛 with equal
step size ℎ, that means, 𝑥𝑖 = 𝑥0 + 𝑖ℎ, 𝑖 = 0,1, ⋯ , 𝑛.
Which reduces Eq. (1) in the following system of linear equations:𝐴𝑌 = 𝑑
𝑏1 𝑐1 0 0 ⋯ 0 𝑦1 𝑑1 − 𝑎1 𝑦0
𝑎2 𝑏2 𝑐2 0 ⋯ 0 𝑦2 𝑑2
0 𝑎3 𝑏3 𝑐3 ⋯ 0 𝑦3 = 𝑑3
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
0 0 0 0 𝑎𝑛−1 𝑏𝑛−1 𝑦𝑛−1 𝑑𝑛−1 − 𝑐𝑛−1 𝑦𝑛
where
ℎ ℎ
𝑎𝑖 = 1 − 𝑝𝑖 , 𝑏𝑖 = −2 + 𝑞𝑖 ℎ , 𝑐𝑖 = 1 + 𝑝𝑖 , 𝑑𝑖 = ℎ2 𝑟𝑖
2
2 2
FINITE DIFFERENCE METHOD (FDM)
Example: Implement the boundary value problem
𝑦 ′′ + 𝑦 = −1
with boundary conditions 𝑦 0 = 0, 𝑦 1 = 0 by using finite difference method (FDM) taking
the step size ℎ = 0.1. Plot the result and compare with the exact solution 𝑦 =
1−cos 1
cos 𝑥 + sin 𝑥 − 1
sin 1
Formula: Solve the following system of linear equations which is in matrix form:
𝑏1 𝑐1 0 0 ⋯ 0 𝑦1 𝑑1 − 𝑎1 𝑦0
𝑎2 𝑏2 𝑐2 0 ⋯ 0 𝑦2 𝑑2
0 𝑎3 𝑏3 𝑐3 ⋯ 0 𝑦3 = 𝑑3
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
0 0 0 0 𝑎𝑛−1 𝑏𝑛−1 𝑦𝑛−1 𝑑𝑛−1 − 𝑐𝑛−1 𝑦𝑛
where
ℎ ℎ
𝑎𝑖 = 1 − 𝑝𝑖 , 𝑏𝑖 = −2 + 𝑞𝑖 ℎ , 𝑐𝑖 = 1 + 𝑝𝑖 , 𝑑𝑖 = ℎ2 𝑟𝑖
2
2 2
Algorithm: Finite Difference Method (FDM)
Step 1: Construct the user defined functions for p x = 0, 𝑞 𝑥 = 0, 𝑟 𝑥 = 1. Divide the interval [0,1] into n-
subintervals of size h = 0.1. Store the these point in the array name x of length (n+1). Initialize the array name y with
y[0] = 0 and y[n] = 1
𝑏1 𝑐1 0 0 ⋯ 0
𝑎2 𝑏2 𝑐2 0 ⋯ 0
Step 2: Construct the tri-diagonal matrix A = 0 𝑎3 𝑏3 𝑐3 ⋯ 0 and column matrix B =
⋮ ⋮ ⋮ ⋮ ⋮ ⋮
0 0 0 0 𝑎𝑛−1 𝑏𝑛−1
𝑑1 − 𝑎1 𝑦0
𝑑2
𝑑3
⋮
𝑑𝑛−1 − 𝑐𝑛−1 𝑦𝑛
ℎ ℎ
with 𝑎𝑖 = 1 − 𝑝𝑖 , 𝑏𝑖 = −2 + 𝑞𝑖 ℎ2 , 𝑐𝑖 = 1 + 𝑝𝑖 , 𝑑𝑖 = ℎ2 𝑟𝑖
2 2
Step3: Compute 𝐴−1 𝐵 and store the value in 𝑌 and insert Y into the array y from 1 t0 (n-1) indices: y[1:-1]=Y
Step 4: Plot the points y versus x
1−cos 1
Step 4: Plot the exact solution𝑦 = cos 𝑥 + sin 𝑥 − 1
sin 1
and compare the approximated and exact solutions taking the steps sizes ℎ = 0.05 0.001
CURVE FITTING
Formula: Problem: Fit the straight line 𝑦 = 𝑎0 + 𝑎1 𝑥 from the set of data points 𝑥𝑖 , 𝑦𝑖 , 𝑖 =
1,2, ⋯ , 𝑚
Solution: Solve two normal equations to find the parameters 𝑎0 , 𝑎1 :
𝑚 𝑚