Lectures 1&2
Lectures 1&2
1 Preliminary
What is Numerical Method?
• Numerical method is an approach for solving various mathematical problems involving differential
equations, curve fitting, integrals and root findings through approximations rather than exact
solutions.
• For solving mathematical problems by numerical method, an input is provided in the form of
some numerical data or it is generated/created as called for by the problem.
• To avoid such difficulties, numerical solutions are obtained using high-speed computer, especially
when the geometry of the object of interest is irregular, or the boundary conditions are nonlinear.
dy d2 y d3 y dn y
f x, y, , 2 , 3 , . . . , n = 0
dx dx dx dx
dn y dy d2 y d3 y dn−1 y
= f x, y, , 2 , 3 , . . . , n−1 (1)
dxn dx dx dx dx
1
Santanu Manna MA 204N: Finite Difference Method
Its general solution will contain n arbitrary constants, which can be determined uniquely if n
conditions are prescribed.
If these conditions are prescribed at a single point i.e., solution is required in an open domain, then
such problems are called Initial Value Problems (IVPs).
If these conditions are prescribed at two or more points i.e., solution is required in a bounded
domain, then such problems are called Boundary Value Problems (BVPs).
A problem is said to be well-posed if the number of prescribed conditions are exactly same as the
order of the equation.
dy d2 y d3 y dn−1 y
z1 = , z2 = , z3 = , . . . , z n−1 =
dx dx2 dx3 dxn−1
So that the equation can be reduced to a system of n simultaneous equations of the first order,
given as:
dy
= z1
dx
2
Santanu Manna MA 204N: Finite Difference Method
For example,
x
x3 x2 x3 x4
Z
y (2) (x) = 1 + x2 − 1 − x + dx = 1 − x + + −
x0 3 2 3 12
x2 x4
Error = y (2) (x) − y (1) (x) = −
2 12
Max. error at x = 0.2:
(0.2)2 (0.2)4
− > 10−3 ⇒ Go for next iteration
2 12
x
x2 x3 x4
Z
y (3) (x) = 1 + x2 − 1 − x + + − dx
x0 2 3 12
x2 x3 x4 x5
=1−x+ + − +
2 6 12 60
x3 x5
Error = y (3) (x) − y (2) (x) = − + > 10−3
6 60
3
Santanu Manna MA 204N: Finite Difference Method
(x − x0 )2 ′′ (x − x0 )3 ′′′ (x − x0 )n (n)
y(x) = y0 + (x − x0 )y0′ + y0 + y0 + · · · + y0 (1.9)
2! 3! n!
With a remainder term:
(x − x0 )n+1 (n+1)
R= f (ξ), x0 ≤ ξ ≤ x (1.10)
(n + 1)!
To find the values of y0′ , y0′′ , y0′′′ , . . . , we proceed in the following manner:
By putting the values of y0′ , y0′′ , y0′′′ , . . ., we get the solution in the form of a power series, which
will be valid in the neighbourhood of x0 . This method is an approximate analytical method.
Now,
1 1 7 (iv) 5
y0′ = f (x0 , y0 ) = , y0′′ = x + yy ′ = , y0′′′ = 1 + yy ′′ + y ′ y ′ = , y0 = yy ′′′ + 3y ′ y ′′ =
2 2 4 2
The Taylor’s series solution is given by:
x x2 7x3 5x4
y(x) = 1 + + + +
2 4 24 48
The truncation error will be of order O(x5 ), i.e., O(10−5 ) for x = 0.1.
4
Santanu Manna MA 204N: Finite Difference Method
h2 ′′ h3 ′′′
y(xi + h) = yi + hyi′ + y + y + ... (4)
2 i 6 i
h2 h3 ′′′
y(xi − h) = yi − hyi′ + yi′′ − y + ... (5)
2 6 i
Subtracting and adding the above equations gives the finite difference approximations:
yi+1 − yi−1
yi′ ≈ (6)
2h
′′ yi+1 − 2yi + yi−1
yi ≈ (7)
h2
[2f1 (xi ) − hf2 (xi )] yi−1 + 2h2 f3 (xi ) − 4f1 (xi ) yi + [2f1 (xi ) + hf2 (xi )] yi+1 = 2h2 r(xi )
(10)
for i = 1, 2, . . . , n − 1
5
Santanu Manna MA 204N: Finite Difference Method
Backward Difference
The backward difference uses the function value at the current and previous grid point.
First Derivative:
yi − yi−1
y ′ (xi ) ≈
h
Second Derivative (from two backward steps):
yi − 2yi−1 + yi−2
y ′′ (xi ) ≈
h2
Central Difference
The central difference uses the average of forward and backward differences, providing higher accuracy
(second-order).
First Derivative:
yi+1 − yi−1
y ′ (xi ) ≈
2h
Second Derivative:
yi+1 − 2yi + yi−1
y ′′ (xi ) ≈
h2
Comparison
• Forward and Backward differences are first-order accurate.
• Central difference is second-order accurate and generally preferred when applicable.
6
Santanu Manna MA 204N: Finite Difference Method
Let us assume that y(xi ) = yi , We have to discretize the equation (12). Since eq. (12) is satisfied
by (xi , yi ), we may write
f1 (xi )y ′′ (xi ) + f2 (xi )y ′ (xi ) + f3 (xi )yi = r(xi ) (13)
Now by Taylor’s series we have
h2 ′′ h3
yi (xi + h) = yi + hyi′ (xi ) + yi (xi ) + yi′′′ (xi ) + . . . (14)
2 3!
and
h2 ′′ h3
yi (xi − h) = yi − hyi′ (xi ) + yi (xi ) − yi′′′ (xi ) + . . . (15)
2 3!
Subtracting the two expansions, we get
yi (xi + h) − yi (xi − h)
yi′ (xi ) = + O(h2 ) (16)
2h
or
yi+1 − yi−1
yi′ = where yi±1 = yi (xi ± h) (17)
2h
Putting two Taylor’s series expansions (14), we get
yi+1 − 2yi + yi−1
yi′′ (xi ) = + O(h2 ) (18)
h2
Using Eqs. (12) and (17) in Eq. (14), we get
(iv) yi−2 − 4yi−1 + 6yi − 4yi+1 + yi+2
yi (xi ) = + O(h2 ) (19)
h4
These are the finite difference equivalences of first, second, and fourth derivatives at any point.
Now replacing yi′ and yi′′ of Eq. (12) by their finite difference equivalents given in Eqs. (17) and
(18), we get
yi+1 − 2yi + yi−1 yi+1 − yi−1
f1 (xi ) + f (x
2 i ) + f3 (xi )yi = r(xi ) (20)
h2 2h
On arranging, equation (20) gives:
7
Santanu Manna MA 204N: Finite Difference Method
Example:
Solve:
xy ′′ + 3y ′ + (1 + x)y = 1 + x2 , 0<x<4
with boundary conditions:
y(0) = 1, y(4) = 0
Let h = 1, x0 = 0, x4 = 4, so the nodes are x1 = 1, x2 = 2, x3 = 3.
Discretizing using finite differences:
yi+1 − 2yi + yi−1 yi+1 − yi−1
xi +2 + (1 + xi )yi = 1 + x2i
h2 2h
At i = 1, x1 = 1:
y2 + y4 = 10 (similar derivation)
Given y0 = 1, y4 = 0, solve the linear system to find y1 , y2 , y3 .
Taking i = 1, x1 = 1, we get:
−y0 + 5y1 = 4 (iv)
Taking i = 2, x2 = 2, we get:
y1 − 2y2 + 7y3 = 10 (v)
8
Santanu Manna MA 204N: Finite Difference Method
y2 = 1
−y0 + 5y2 = 4 ⇒ −1 + 5y2 = 4 ⇒ y2 = 1
17
3y2 − 4y3 + 9y4 = 20 ⇒ 3(1) − 4y3 = 20 ⇒ −4y3 = 17 ⇒ y3 = −
4
Substituting into (v):
17 119 167
y1 + 7 − = 12 ⇒ y1 = 12 + =
4 4 4
Example:
Solve the boundary value problem:
d2 y
= −π 2 y, 0<x<1
dx2
with derivative boundary conditions:
y ′ (0) = 0, y ′ (1) = 0
These are Neumann boundary conditions specifying zero slope at both ends.
9
Santanu Manna MA 204N: Finite Difference Method
11. Thus, we are left with five equations in five unknowns: y0 , y1 , y2 , y3 , y4 , which can be solved
uniquely.
Step 1: Discretization
1
Let the interval [0, 1] be divided into N parts with step size h = N. Let yi ≈ y(xi ), where xi = ih.
10
Santanu Manna MA 204N: Finite Difference Method
Example
Solve the equation
1 2
y ′′ − xy = 1 at x = 0, ,
3 3
with boundary conditions:
y(0) + y ′ (0) = 1, y(1) = 2
Solution:
Here, h = 31 , so the equivalent finite difference equation becomes:
yi−1 − 2yi + yi+1
− xi yi = 1
(1/3)2
or,
x 1
i
yi−1 + + 2 yi + yi+1 = (i)
9 9
Now putting i = 0, 1, 2, we get:
1
y−1 − 2y0 + y1 = (x0 = 0)
9
1 1
y0 − + 2 y1 + y2 = (x1 = 1/3) (ii)
27 9
2 1
y1 − + 1 y2 + y3 = (x2 = 2/3)
27 9
Given: y3 = 2
Using the boundary condition y(0) + y ′ (0) = 1, we approximate the derivative using forward
difference:
y1 − y−1 y1 − y−1 3
y ′ (0) ≈ = 1 = (y1 − y−1 )
2h 2× 3 2
3
y0 + (y1 − y−1 ) = 1
2
Solving for y−1 :
2y0 + 3y1 − 2
y−1 = (iii)
3
11
Santanu Manna MA 204N: Finite Difference Method
Example
Solve the equation
1
y ′′ + (1 + x)y ′ − y = 0, y(0) = y ′ (0), y(1) + y ′ (1) = 1, with h =
2
Solution
We have the differential equation:
y ′′ + (1 + x)y ′ − y = 0 (i)
Taking finite difference approximations, we get:
1 1
(yi−1 − 2yi + yi+1 ) + (1 + xi ) · (yi+1 − yi−1 ) − yi = 0
h2 2h
Putting h = 21 , we get:
Putting i = 1
Putting i = 2
Boundary Conditions
From the condition y(0) = y ′ (0), using forward difference:
1 1
y0 = y0′ = (y1 − y−1 ) ⇒ y0 = (y1 − y−1 ) ⇒ y−1 = y1 − hy0 ⇒ y0 = y1 − y−1 (v)
h h
From this, solving gives:
2
y0 =
y1 (vi)
3
From the condition y(1) + y ′ (1) = 1, using backward difference:
1 1 + 2y1 − y2
y2 + (y3 − y1 ) = 1 ⇒ y2 + 2(y3 − y1 ) = 1 ⇒ y3 = (vii)
h 2
12
Santanu Manna MA 204N: Finite Difference Method
Substituting in (iii)
Substitute y0 = 23 y1 into (iii):
5 2 11
· y1 − 9y1 + y2 = 0
2 3 2
10 11
⇒ y1 − 9y1 + y2 = 0
6 2
8 11
⇒ − y1 + y2 = 0 (viii)
3 2
Hence,
y0 = 0.33
y1 = 0.5
y2 = 0.66
13
Santanu Manna MA 204N: Finite Difference Method
4 Summary
• Dirichlet conditions fix the values of the solution at the boundaries.
• They are common in physical problems involving flux, temperature gradients, etc.
• Finite difference methods handle them using derivative approximations.
• May introduce extra (”ghost”) points, which are eliminated by substitution.
Advantages
Simple and easy to implement.
Limitations
Less accurate for irregular geometries.
May need very fine grids for accuracy, increasing computation time.
14