0% found this document useful (0 votes)
5 views7 pages

Finite Difference Method - MatrixMethod

finite difference method- matrix method solution for Machine Learning

Uploaded by

Disha Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views7 pages

Finite Difference Method - MatrixMethod

finite difference method- matrix method solution for Machine Learning

Uploaded by

Disha Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Finite Difference Method:

Another way to solve the ODE boundary value problems is the finite difference method, where
we can use finite difference formulas at evenly spaced grid points to approximate the differential
equations. This way, we can transform a differential equation into a system of algebraic
equations to solve.
In the finite difference method, the derivatives in the differential equation are approximated
using the finite difference formulas. We can divide the interval of [a,b] into n equal subintervals
of length h as shown in the following figure.

Commonly, we usually use the central difference formulas in the finite difference methods due to
the fact that they yield better accuracy. The differential equation is enforced only at the grid
points, and the first and second derivatives are:

𝑑𝑦 𝑦𝑖+1 − 𝑦𝑖−1
=
𝑑𝑥 2ℎ
𝑑2 𝑦 𝑦𝑖−1 − 2𝑦𝑖 + 𝑦𝑖+1
=
𝑑𝑥 2 ℎ2
These finite difference expressions are used to replace the derivatives of y in the differential
equation which leads to a system of n+1 linear algebraic equations if the differential equation is
linear. If the differential equation is nonlinear, the algebraic equations will also be nonlinear.

Example: Consider the following second order equation of rocket motion:


𝑑2𝑦
= −𝑔
𝑑𝑥 2
Boundary conditions are: y(0)=0, y(10)=50. Let’s take n=50. Since the time interval is [0,
10] and we have n=50, therefore, h=0.2, using the finite difference approximated derivatives, we
have:
𝑦0 = 0
𝑦𝑖−1 − 2𝑦𝑖 + 𝑦𝑖+1 = −𝑔ℎ2 , 𝑖 = 1, 2, 3, … … … , 𝑛 − 1
𝑦10 = 50
In matrix form: AY=B, the components of A are basically the co-efficient of yi for each equations.
For first row put i=0, then coefficients of y0 is 1 and other terms of this row (here, rest of the 49
terms) are all 0. Similarly it will happen for other equations also.

Therefore, we have n+1=51 equations in the system.


Python Script:
Eigen Value Problem:
Example: Consider the 1D Schrödinger equation for a particle in a box (potential energy,
V=0)
𝑑2𝜓 2𝑚
= − 𝐸𝜓
𝑑𝑥 2 ℏ2
We discretize the domain (width of the potential) into equal step size: x=h such that we can
write:
𝜓𝑖−1 −2𝜓𝑖 +𝜓𝑖+1 2𝑚
=− 𝐸𝜓𝑖
ℎ2 ℏ2

h is just step size, don’t mix with Planck’s constant.


𝜓0 −2𝜓1 +𝜓2 2𝑚
Put i=1, =− 𝐸𝜓1
ℎ2 ℏ2
𝜓1 −2𝜓2 +𝜓3 2𝑚
Put i=2, =− 𝐸𝜓1
ℎ2 ℏ2

……………………………
𝜓𝑛−1 −2𝜓𝑛 +𝜓𝑛+1 2𝑚
Put i=n, =− 𝐸𝜓1
ℎ2 ℏ2

In matrix form:
Let’s define the grid points xi such that:
𝑥1 = 0, 𝑥2 = ℎ, ……, 𝑥𝑛 = (𝑛 − 1)ℎ with boundary conditions 𝜓(0)= 𝜓(𝐿)=0
We will construct a matrix A such that:
2𝑚𝐸
𝐴𝜓 = 𝜓
ℏ2
This matrix A represents the finite difference approximation of the second derivative. This will
be a tridiagonal matrix, that means except the diagonal, top and bottom diagonal elements are
zero. The diagonal elements represent −2⁄ℎ2 , and the off-diagonal elements represent 1⁄ℎ2 .
The matrix looks like this for N grid points:
Therefore,

= E

Let’s understand through python script step by step:

Here we are basically discretizing the x using linspace. If we put just ‘N’ in linspace then the
division is little bit problem as shown in above. So it’s better to take ‘N+1’ in linspace as
following:

Now let’s form the matrix A:


But, here my diagonal element for matrix A is -2. Then we have to just multiply -2 with np.ones.

Next, to make the elements for upper diagonal ‘np.diag(np.ones(N-2),1)’ and lower diagonal
‘np.diag(np.ones(N-2),-1)’
To check the dimension of the matrix:

ℏ2 𝐸
Now, we will multiply the constant with matrix A and form a new matrix H:
2𝑚ℎ2

So, we have now 𝐻𝜓 = 𝐸𝜓


We want to solve the eigenvalue problem. The inbuilt function of numpy will give us directly the
eigenvalues i.e energies, here and the eigenvectors i.e. 𝜓s.
Thus, we also can plot probability density by plotting 𝜓 ∗ 𝜓.

To solve the s-wave Schrödinger equation for the ground and first excited states of the
hydrogen atom using the matrix method, we first need to discretize the problem and then solve it
numerically using matrix diagonalization.
Schrödinger Equation for Hydrogen Atom (s-wave):
The radial Schrödinger equation for the hydrogen atom (with l=0l = 0l=0, i.e., s-wave) is given
by:
ℏ2 𝑑 2 𝑒 2
[− − ] 𝑢(𝑟) = 𝐸𝑢(𝑟)2
2𝑚 𝑑𝑟 2 𝑟

Where, 𝑢(𝑟) is the radial wavefunction


𝑢(𝑟)

You might also like