Finite Difference Heat Equation using NumPy
The problem we are solving is the heat equation

with Dirichlet Boundary Conditions ( ) over the domain
with the initial conditions

You can think of the problem as solving for the temperature in a one-dimensional metal rod when the ends of the rod is kept at 0 degrees. Intuitively, you know that the temperature is going to go to zero as time goes to infinite.
To solve this problem using a finite difference method, we need to
discretize in space first. I will be using a second-order centered
difference to approximate . This will give the following semi-discrete problem:

where

and

The next step is to discretize in time. We can do this by using the Crank-Nicolson method which is

where

If we apply this method to the semi-discrete problem, we will get

If we want to solve for , we get the following system of equations

We can implement this method using the following python code. This code will then generate the following movie.
Please note that the movie was sped up by a factor of ten.