Technology

Solving the Diffusion Equation Explicitly

2025-05-09 08:28:08


This post is part of a series of Finite Difference Method Articles. Other posts in the series concentrate on Derivative Approximation, the Crank-Nicolson Implicit Method and the Tridiagonal Matrix Solver/Thomas Algorithm:

  • Derivative Approximation via Finite Difference Methods
  • Solving the Diffusion Equation Explicitly
  • Crank-Nicolson Implicit Scheme
  • Tridiagonal Matrix Solver via Thomas Algorithm


In Part 1 of the series on Finite Difference Methods, it was shown that continuous derivatives could be approximated and applied to a discrete domain. The next step is to apply these derivatives to a parabolic PDE. The heat equation is the canonical example of a parabolic PDE, and this will now be discretised.


The heat equation with initial condition g is given below:


ft=2fx2,f(x,0)=g(x)


This is discretised by applying a forward difference to the time derivative and a centered second difference for the diffusion term to give:


fin+1finΔt=fi+1n2fin+fi1n(Δx)2


This equation can be rearranged for fin+1 to give:


fin+1=fin+Δt(Δx)2(fi+1n2fin+fi1n)



Computation of Solution

For n=1 all of the approximations to the solution f are known on the right hand side of the equation. This makes the equation explicit. Thus the only remaining task is to determine Δt, which is set to (Δx)2. Thus the equation reduces to:


fin+1=fi+1nfin+fi1n


This formula will allow calculation of fi2 for all i1,...,I as an approximation to g provides the discretised initial condition. Let gi be such an approximation, with three 'heat bumps' and take I=13, then the values of gi are given by:


0010012100100


This will allow a solution to be generated by progressing from the initial condition in a temporal manner. This is referred to as a time marched solution:


n=10010012100100n=20111110111110n=31231102011321n=43665232325663


For n=4 the discretised solution is wildly oscillating and bears no resemblance to the continuous solution, which has the physical interpretation of diffusing the heat bumps. Clearly one of the assumptions made about the formulae is incorrect.



Stability

It turns out that the issue is with the choice of time step size Δt relative to the spatial step size Δx. The correct stability criterion is given by:


Δt(Δx)212


It is beyond the scope of this article to prove this result, but note that it puts a significant restriction on the time step - a problem which will be remedied in the next article.


Applying this new stability criterion, with Δt=0.5×(Δx)2, to the above formulae produces:


fin+1=12fi+1n+12fi1n


The calculation for this formulae provides:


n=10010012100100n=200.500.50.51110.50.500.50n=30.2500.50.250.750.7510.750.750.250.500.25n=400.3750.1250.6250.50.8750.750.8750.50.6250.1250.3750


This is an accurate approximation to the true solution. There are no negative values and the physical interpretation of the heat diffusing through a 1D bar fits with the solution.



Conclusion

The choice of time step is very restrictive. In practice, with a refined spatial mesh, the time step would be proihibitively small. The next article will introduce another method to overcome the small timestep issue.




Reference: Solving the Diffusion Equation Explicitly

From https://www.quantstart.com/articles/Solving-the-Diffusion-Equation-Explicitly/

Leave a comment :

Other interesting articles

There are many other interesting articles, try selecting them from below.