This repository contains a MATLAB implementation of three finite difference schemes for solving the Heat Equation:
where:
-
$u(x,t)$ is the temperature at position ( x ) and time ( t ), -
$\alpha$ is the thermal diffusivity constant.
The following three finite difference schemes are implemented to solve this equation:
-
Crank-Nicolson Scheme:
$$\frac{{U_i^{n + 1} - U_i^n}}{\tau } = \frac{1}{{2{h^2}}}\left[ {U_{i + 1}^{n + 1} - 2U_i^{n + 1} + U_{i - 1}^{n + 1} + U_{i + 1}^n - 2U_i^n + U_{i - 1}^n} \right]$$ -
Forward Euler Scheme:
$$\frac{{U_i^{n + 1} - U_i^n}}{\tau } = \frac{1}{{{h^2}}}\left( {U_{i - 1}^n - 2U_i^n + U_{i + 1}^n} \right)$$ -
Stabilized Central Time Scheme:
$$\frac{{U_i^{n + 1} - U_i^{n - 1}}}{{2\tau }} = \frac{1}{{{h^2}}}\left( {U_{i + 1}^n - U_i^{n + 1} - U_i^{n - 1} + U_{i - 1}^n} \right)$$
The main objective of this project is to implement these schemes to solve the 1D Heat Equation with zero Dirichlet boundary conditions and an initial condition of your choice, over the interval (0,1).
- MATLAB R2015 or later
- Crank Nicholson Scheme Implementation/:
- CNS.m: Crank-Nicholson function which generates
- A Mesh Plot to the solution to the heat equation
- A plot of
$U_{Approx}$ and$U_{exact}$ at the final time
- MainCNS.m: A script that contains the implementation of CNS for different values of N which generates
- Results for each N
- Convergence plot.
- CNS.m: Crank-Nicholson function which generates
Consider the following Partial Differential Equation (PDE) for the Heat Equation:
with the boundary and initial conditions:
and the initial condition:
where:
-
$u(x, t)$ represents the temperature distribution at position ( x ) and time ( t ), -
$u_t$ is the partial derivative of ( u ) with respect to time, -
$u_{xx}$ is the second spatial derivative of ( u ).
The exact solution to this problem is:
Here are the results from applying the finite difference schemes to the heat equation:
![]() |
![]() |
|---|---|
| Crank-Nicolson solution with dt=0.0125 | Plot of Exact and Approximated solution at T = 0.5 |
![]() |
|---|
| Convergence Plot for Crank-Nicolson Solution |
![]() |
![]() |
|---|---|
| Forward Euler solution with dt=0.0025 | Plot of Exact and Approximated solution at T = 0.5 |
![]() |
|---|
| Convergence Plot for Forward Euler |
![]() |
![]() |
|---|---|
| Stabilaized Central Time Solution of Heat Equation with dt=0.0053 | Plot of Exact and Approximated solution at T = 0.5 |
![]() |
|---|
| Convergence Plot for Stabilized Central Time |
The project is accompanied by a detailed report which contains the Analysis of Stability and Consistency.








