# The heat equation#

Authors: Anders Logg and Hans Petter Langtangen

Minor modifications by: Jørgen S. Dokken

As a first extension of the Poisson problem from the previous chapter, we consider the time-dependent heat equation, or the time-dependent diffusion equation. This is the natural extension of the Poisson equation describing the stationary distribution of heat in a body to a time-dependent problem. We will see that by discretizing time into small time intervals and applying standard time-stepping methods, we can solve the heat equation by solving a sequence of variational problems, much like the one we encountered for the Poisson equation.

## The PDE problem#

The model problem for the time-dependent PDE reads

Here \(u\) varies with space and time, e.g. \(u=u(x,y,t)\) if the spatial domain \(\Omega\) is two-dimensional. The source function \(f\) and the boundary values \(u_D\) may also vary with space and time. The initial condition \(u_0\) is a function of space only.

## The variational formulation#

A straightforward approach to solving time-dependent PDEs by the finite element method is to first discretize the time derivative by a finite difference approximation, which yields a sequence of stationary problems, and then turn each stationary problem into a variational formulation. We will let the superscript \(n\) denote a quantity at time \(t_n\), where \(n\) is an integer counting time levels. For example, \(u^n\) means \(u\) at time level \(n\). The first step of a finite difference discretization in time consists of sampling the PDE at some time level, for instance \(t_{n+1}\)

The time-derivative can be approximated by a difference quotient. For simplicity and stability reasons, we choose a simple backward difference:

where \(\Delta t\) is the time discretization parameter. Inserting the latter expression into our equation at time step \(n+1\) yields

This is our time-discrete version of the heat equation. It is called a *backward Euler* or a *implicit Euler* discretization.

We reorder the equation such that the left-hand side contains the terms with only the unknown \(u^{n+1}\) and right-hand side contains only computed terms. The resulting equation is a sequence of stationary problems for \(u^{n+1}\), assuming \(u^{n}\) is known from the previous time step:

Given \(u_0\), we can solve for \(u^0, u^1, u^2\) and so on.

We then in turn use the finite element method. This means that we have to turn the equation into its weak formulation. We multiply by the test-function of \(v\in \hat{V}\) and integrate second-order derivatives by parts. We now introduce the symbol \(u\) for \(u^{n+1}\) and we write the resulting weak formulation as

where

## Projection or interpolation of the initial condition#

In addition to the variational problem to be solved in each time step, we also need to approximate the initial condition. This equation can also be turned into a variational problem

with

When solving this variational problem \(u^0\) becomes the \(L^2\)-projection of the given initial value \(u_0\) into the finite element space.

The alternative is to construct \(u^0\) by just interpolating the initial value \(u_0\). We covered how to use interpolation in DOLFINx in the membrane chapter.

We can use DOLFINx to either project or interpolate the initial condition. The most common choice is to use a projection, which computes an approximation to \(u_0\). However, in some applications where we want to verify the code by reproducing exact solutions, one must use interpolation. In this chapter, we will use such a problem.