.. !split

.. _fem:deq:timedep:

Time-dependent problems
=======================

The finite element method is normally used for discretization in
space. There are two alternative strategies for performing
a discretization in time:

 * use finite differences for time derivatives to arrive at
   a recursive set of spatial problems that can be discretized by
   the finite element method, or

 * discretize in space by finite elements first, and then solve
   the resulting system of ordinary differential equations (ODEs) by
   some standard method for ODEs.

We shall exemplify these strategies using a simple diffusion problem


.. _Eq:fem:deq:diffu:eq:

.. math::
   :label: fem:deq:diffu:eq
        
        \frac{\partial u}{\partial t} = {\alpha}\nabla^2 u + f(\boldsymbol{x}, t),\quad
        \boldsymbol{x}\in\Omega, t\in (0,T],
        
        



.. _Eq:fem:deq:diffu:ic:

.. math::
   :label: fem:deq:diffu:ic
          
        u(\boldsymbol{x}, 0)  = I(\boldsymbol{x}),\quad \boldsymbol{x}\in\Omega,
        
        



.. _Eq:fem:deq:diffu:bcN:

.. math::
   :label: fem:deq:diffu:bcN
          
        \frac{\partial u}{\partial n} = 0,\quad \boldsymbol{x}\in\partial\Omega,\ t\in (0,T]
        
        {\thinspace .}
        

Here, :math:`u(\boldsymbol{x},t)` is the unknown function, :math:`{\alpha}` is a constant, and
:math:`f(\boldsymbol{x},t)` and :math:`I(x)` are given functions. We have assigned the particular
boundary condition :eq:`fem:deq:diffu:bcN` to minimize
the details on handling boundary conditions in the finite element method.

.. _fem:deq:diffu:FE:

Discretization in time by a Forward Euler scheme
------------------------------------------------

.. 2DO

.. N_s out: use N_t in time N in general space in all examples,

.. and change decay and vib to N_t


Time discretization  (1)
~~~~~~~~~~~~~~~~~~~~~~~~

We can apply a finite difference method in time to :eq:`fem:deq:diffu:eq`.
First we need a mesh in time, here taken as uniform with
mesh points :math:`t_n = n\Delta t`, :math:`n=0,1,\ldots,N_t`.
A Forward Euler scheme consists of sampling :eq:`fem:deq:diffu:eq`
at :math:`t_n` and approximating the time derivative by a forward
difference :math:`[D_t^+ u]^n\approx
(u^{n+1}-u^n)/\Delta t`. This approximation turns :eq:`fem:deq:diffu:eq`
into a differential equation that is discrete in time, but still
continuous in space.
With a finite difference operator notation we can write the
time-discrete problem as


.. _Eq:fem:deq:diffu:FE:eq:FEop:

.. math::
   :label: fem:deq:diffu:FE:eq:FEop
        
        [D_t^+ u = {\alpha}\nabla^2 u + f]^n,
        
        

for :math:`n=1,2,\ldots,N_t-1`. Writing this equation out in detail and
isolating the unknown :math:`u^{n+1}` on the left-hand side, demonstrates that
the time-discrete problem is a recursive set of problems that are
continuous in space:


.. _Eq:fem:deq:diffu:FE:eq:unp1:

.. math::
   :label: fem:deq:diffu:FE:eq:unp1
        
        u^{n+1} = u^n + \Delta t \left( {\alpha}\nabla^2 u^n + f(\boldsymbol{x}, t_n)\right)
        
        {\thinspace .}
        

Given :math:`u^0=I`, we can use :eq:`fem:deq:diffu:FE:eq:unp1` to compute
:math:`u^1,u^2,\dots,u^{N_t}`.

For absolute clarity in the various stages of the discretizations, we
introduce :math:`{u_{\small\mbox{e}}}(\boldsymbol{x},t)` as the exact solution of the space-and time-continuous
partial differential equation :eq:`fem:deq:diffu:eq` and
:math:`{u_{\small\mbox{e}}}^n(\boldsymbol{x})` as the time-discrete approximation, arising from the finite
difference method in time :eq:`fem:deq:diffu:FE:eq:FEop`.
More precisely, :math:`{u_{\small\mbox{e}}}` fulfills


.. _Eq:fem:deq:diffu:eq:uex:

.. math::
   :label: fem:deq:diffu:eq:uex
        
        \frac{\partial {u_{\small\mbox{e}}}}{\partial t} = {\alpha}\nabla^2 {u_{\small\mbox{e}}} + f(\boldsymbol{x}, t)
        ,
        

while :math:`{u_{\small\mbox{e}}}^{n+1}`, with a superscript,
is the solution of the time-discrete equations


.. _Eq:fem:deq:diffu:FE:eq:uex:n:

.. math::
   :label: fem:deq:diffu:FE:eq:uex:n
        
        {u_{\small\mbox{e}}}^{n+1} = {u_{\small\mbox{e}}}^n + \Delta t \left( {\alpha}\nabla^2 {u_{\small\mbox{e}}}^n + f(\boldsymbol{x}, t_n)\right)
        
        {\thinspace .}
        


Space discretization
~~~~~~~~~~~~~~~~~~~~

We now introduce a finite element approximation to :math:`{u_{\small\mbox{e}}}^n` and :math:`{u_{\small\mbox{e}}}^{n+1}`
in :eq:`fem:deq:diffu:FE:eq:uex:n`, where the coefficients depend on the
time level:


.. _Eq:fem:deq:diffu:femapprox:n:

.. math::
   :label: fem:deq:diffu:femapprox:n
        
        {u_{\small\mbox{e}}}^n \approx u^n = \sum_{j=0}^{N} c_j^{n}{\psi}_j(\boldsymbol{x}),
        
        



.. _Eq:fem:deq:diffu:femapprox:np1:

.. math::
   :label: fem:deq:diffu:femapprox:np1
          
        {u_{\small\mbox{e}}}^{n+1} \approx u^{n+1} = \sum_{j=0}^{N} c_j^{n+1}{\psi}_j(\boldsymbol{x})
        
        {\thinspace .}
        

Note that, as before, :math:`N` denotes the number of degrees of freedom
in the spatial domain. The number of time points is denoted by :math:`N_t`.
We define a space :math:`V` spanned by the basis functions :math:`\left\{ {{\psi}}_i \right\}_{i\in{\mathcal{I}_s}}`.

.. Also note that we use :math:`u^n` as the numerical solution we want

.. to compute in a program, while :math:`{u_{\small\mbox{e}}}` and :math:`{u_{\small\mbox{e}}}^n` are used when

.. we occasionally

.. need to refer to the exact solution and the time-discrete solution,

.. respectively.


Variational forms  (1)
----------------------

A weighted residual method with weighting functions :math:`w_i` can
now be formulated. We insert :eq:`fem:deq:diffu:femapprox:n` and
:eq:`fem:deq:diffu:femapprox:np1` in
:eq:`fem:deq:diffu:FE:eq:uex:n` to obtain the residual


.. math::
         R = u^{n+1} - u^n - \Delta t \left( {\alpha}\nabla^2 u^n + f(\boldsymbol{x}, t_n)\right)
        {\thinspace .} 

The weighted residual principle,


.. math::
         \int_\Omega Rw{\, \mathrm{d}x} = 0,\quad \forall w\in W,

results in


.. math::
        
        \int_\Omega
        \left\lbrack
        u^{n+1} - u^n - \Delta t \left( {\alpha}\nabla^2 u^n + f(\boldsymbol{x}, t_n)\right)
        \right\rbrack w {\, \mathrm{d}x} =0, \quad\forall w \in W{\thinspace .}
        

From now on we use the Galerkin method so :math:`W=V`.
Isolating the unknown :math:`u^{n+1}` on the left-hand side gives


.. math::
        
        \int_{\Omega} u^{n+1}{\psi}_i{\, \mathrm{d}x} = \int_{\Omega}
        \left\lbrack u^n - \Delta t \left( {\alpha}\nabla^2 u^n + f(\boldsymbol{x}, t_n)\right)
        \right\rbrack v{\, \mathrm{d}x},\quad \forall v\in V
        {\thinspace .}
        


As usual in spatial finite element problems involving second-order
derivatives, we apply integration by parts on the term
:math:`\int (\nabla^2 u^n)v{\, \mathrm{d}x}`:


.. math::
         \int_{\Omega}{\alpha}(\nabla^2 u^n)v {\, \mathrm{d}x} =
        -\int_{\Omega}{\alpha}\nabla u^n\cdot\nabla v{\, \mathrm{d}x} +
        \int_{\partial\Omega}{\alpha}\frac{\partial u^n}{\partial n}v {\, \mathrm{d}x}
        {\thinspace .}
        

The last term vanishes because we have the Neumann condition
:math:`\partial u^n/\partial n=0` for all :math:`n`. Our discrete problem in
space and time then reads


.. _Eq:fem:deq:diffu:FE:vf:u:np1:

.. math::
   :label: fem:deq:diffu:FE:vf:u:np1
        
        \int_{\Omega} u^{n+1}v{\, \mathrm{d}x} =
        \int_{\Omega} u^n vdx -
        \Delta t \int_{\Omega}{\alpha}\nabla u^n\cdot\nabla v{\, \mathrm{d}x} +
        \Delta t\int_{\Omega}f^n v{\, \mathrm{d}x},\quad \forall \boldsymbol{v}\in V{\thinspace .}
        
        

This is the variational formulation of our recursive set of spatial
problems.




.. admonition:: Nonzero Dirichlet boundary conditions

   As in stationary problems,
   we can introduce a boundary function :math:`B(\boldsymbol{x},t)` to take care
   of nonzero Dirichlet conditions:
   
   
   .. _Eq:fem:deq:diffu:femapprox:n:B:

.. math::
   :label: fem:deq:diffu:femapprox:n:B
           
           {u_{\small\mbox{e}}}^n \approx u^n = B(\boldsymbol{x},t_n) + \sum_{j=0}^{N} c_j^{n}{\psi}_j(\boldsymbol{x}),
           
           
   
   
   
   .. _Eq:fem:deq:diffu:femapprox:np1:B:

.. math::
   :label: fem:deq:diffu:femapprox:np1:B
             
           {u_{\small\mbox{e}}}^{n+1} \approx u^{n+1} = B(\boldsymbol{x},t_{n+1}) +
           \sum_{j=0}^{N} c_j^{n+1}{\psi}_j(\boldsymbol{x})
           
           {\thinspace .}






Simplified notation for the solution at recent time levels
----------------------------------------------------------

In a program it is only necessary to store :math:`u^{n+1}` and :math:`u^n` at the
same time. We therefore drop the :math:`n` index in programs and work with
two functions: ``u`` for :math:`u^{n+1}`, the new unknown, and ``u_1`` for
:math:`u^n`, the solution at the previous time level. This is also
convenient in the mathematics to maximize the correspondence with the
code. From now on :math:`u_1` means the discrete unknown at the previous
time level (:math:`u^{n}`) and :math:`u` represents the
discrete unknown at the new time level (:math:`u^{n+1}`).
Equation :eq:`fem:deq:diffu:FE:vf:u:np1` with this new
naming convention is expressed as


.. _Eq:fem:deq:diffu:FE:vf:u:

.. math::
   :label: fem:deq:diffu:FE:vf:u
        
        \int_{\Omega} u vdx =
        \int_{\Omega} u_1 vdx -
        \Delta t \int_{\Omega}{\alpha}\nabla u_1\cdot\nabla v{\, \mathrm{d}x} +
        \Delta t\int_{\Omega}f^n v{\, \mathrm{d}x}
        {\thinspace .}
        
        

This variational form can alternatively be expressed by the inner
product notation:


.. _Eq:fem:deq:diffu:FE:vf:u:short:

.. math::
   :label: fem:deq:diffu:FE:vf:u:short
        
        (u,v) = (u_1,v) -
        \Delta t ({\alpha}\nabla u_1,\nabla v) +
        (f^n, v)
        {\thinspace .}
        
        


Deriving the linear systems
---------------------------

To derive the equations for the new unknown coefficients :math:`c_j^{n+1}`,
now just called :math:`c_j`, we insert


.. math::
         u = \sum_{j=0}^{N}c_j{\psi}_j(\boldsymbol{x}),\quad
        u_1 = \sum_{j=0}^{N} c_{1,j}{\psi}_j(\boldsymbol{x})

in :eq:`fem:deq:diffu:FE:vf:u` or :eq:`fem:deq:diffu:FE:vf:u:short`,
let the equation hold for all :math:`v={\psi}`, $i=0,\ldots,$N,
and order the terms as matrix-vector products:


.. math::
        
        \sum_{j=0}^{N} ({\psi}_i,{\psi}_j) c_j =
        \sum_{j=0}^{N} ({\psi}_i,{\psi}_j) c_{1,j}
        -\Delta t \sum_{j=0}^{N} (\nabla{\psi}_i,{\alpha}\nabla{\psi}_j) c_{1,j}
        + (f^n,{\psi}_i),\quad i=0,\ldots,N
        {\thinspace .}
        

This is a linear system :math:`\sum_j A_{i,j}c_j = b_i` with


.. math::
         A_{i,j} = ({\psi}_i,{\psi}_j)
        

and


.. math::
         b_i = \sum_{j=0}^{N} ({\psi}_i,{\psi}_j) c_{1,j}
        -\Delta t \sum_{j=0}^{N} (\nabla{\psi}_i,{\alpha}\nabla{\psi}_j) c_{1,j}
        + (f^n,{\psi}_i){\thinspace .}  


It is instructive and convenient for implementations to write the linear
system on the form


.. math::
        
        Mc = Mc_1 - \Delta t Kc_1 + f,
        

where


.. math::
        
        M &= \{M_{i,j}\},\quad M_{i,j}=({\psi}_i,{\psi}_j),\quad i,j\in{\mathcal{I}_s},\\ 
        K &= \{K_{i,j}\},\quad K_{i,j}=(\nabla{\psi}_i,{\alpha}\nabla{\psi}_j),
        \quad i,j\in{\mathcal{I}_s},\\ 
        f &= \{(f(\boldsymbol{x},t_n),{\psi}_i)\}_{i\in{\mathcal{I}_s}},\\ 
        c &= \{c_i\}_{i\in{\mathcal{I}_s}},\\ 
        c_1 &= \{c_{1,i}\}_{i\in{\mathcal{I}_s}}
        {\thinspace .}
        



.. index:: mass matrix

.. index:: stiffness matrix


We realize that :math:`M` is the matrix arising from a term with the
zero-th derivative of :math:`u`, and called the mass matrix, while :math:`K` is
the matrix arising from a Laplace term :math:`\nabla^2 u`. The :math:`K` matrix
is often known as the *stiffness matrix*. (The terms mass and stiffness
stem from the early days of finite elements when applications to
vibrating structures dominated. The mass matrix arises from the
mass times acceleration term in Newton's second law, while the stiffness
matrix arises from the elastic forces in that law. The mass and stiffness
matrix appearing in a diffusion have slightly different mathematical
formulas.)

**Remark.**
The mathematical symbol :math:`f` has two meanings, either the
function :math:`f(\boldsymbol{x},t)` in the PDE or the :math:`f` vector in the linear system
to be solved at each time level. The symbol :math:`u` also has different
meanings, basically the unknown in the PDE or the finite element
function representing the unknown at a time level. The actual
meaning should be evident from the context.


Computational algorithm
-----------------------

We observe that :math:`M` and :math:`K` can be precomputed so that we can avoid
computing the matrix entries at every time level. Instead, some
matrix-vector multiplications will produce the linear system to be solved.
The computational algorithm has the following steps:

1. Compute :math:`M` and :math:`K`.

2. Initialize :math:`u^0` by interpolation or projection

3. For :math:`n=1,2,\ldots,N_t`:

  1. compute :math:`b = Mc_1 - \Delta t Kc_1 + f`

  2. solve :math:`Mc = b`

  3. set :math:`c_1 = c`


In case of finite element basis functions, interpolation of the
initial condition at the nodes means :math:`c_{1,j} = I(\boldsymbol{x}_j)`. Otherwise
one has to solve the linear system :math:`\sum_j{\psi}_j(x_{i})c_j =
I(x_{i})`, where :math:`\boldsymbol{x}_j` denotes an interpolation point.  Projection
(or Galerkin's method) implies solving a linear system with :math:`M` as
coefficient matrix : :math:`\sum_j M_{i,j}c_{1,j} = (I,{\psi}_i)`,
:math:`i\in{\mathcal{I}_s}`.


.. _fem:deq:diffu:FE:fdvsP1fe:

Comparing P1 elements with the finite difference method
-------------------------------------------------------

We can compute the :math:`M` and :math:`K` matrices using P1 elements in 1D.
A uniform mesh on :math:`[0,L]` is introduced for this purpose.
Since the boundary conditions are solely of Neumann type in this
sample problem, we have no restrictions on the basis functions
:math:`{\psi}_i` and can simply choose :math:`{\psi}_i = {\varphi}_i`, :math:`i=0,\ldots,N=N_n`.

From the section :ref:`fem:deq:1D:comp:global` or
:ref:`fem:deq:1D:comp:elmwise` we have that the :math:`K` matrix is the same as we get
from the finite difference method: :math:`h[D_xD_x u]^n_i`, while
from the section :ref:`fem:approx:fe:fd:feproj` we know that :math:`M` can be
interpreted as the finite difference approximation
:math:`[u + \frac{1}{6}h^2D_xD_x u]^n_i` (times :math:`h`). The equation system :math:`Mc=b`
in
the algorithm is therefore equivalent to the finite difference scheme


.. _Eq:fem:deq:diffu:FE:fdinterp:

.. math::
   :label: fem:deq:diffu:FE:fdinterp
        
        [D_t^+(u + \frac{1}{6}h^2D_xD_x u) = {\alpha} D_xD_x u + f]^n_i
        
        {\thinspace .}
        

(More precisely, :math:`Mc=b` divided by :math:`h` gives the equation above.)

Lumping the mass matrix
~~~~~~~~~~~~~~~~~~~~~~~

By applying Trapezoidal integration one can turn :math:`M` into a diagonal
matrix with :math:`(h/2,h,\ldots,h,h/2)` on the diagonal. Then there is
no need to solve a linear system at each time level, and the finite
element scheme becomes identical to a standard finite difference method


.. _Eq:fem:deq:diffu:FE:fdinterp:lumped:

.. math::
   :label: fem:deq:diffu:FE:fdinterp:lumped
        
        [D_t^+ u = {\alpha} D_xD_x u + f]^n_i
        
        {\thinspace .}
        


The Trapezoidal integration is not as accurate as exact integration and
introduces therefore an error. Whether this error has a good or bad
influence on the overall numerical method is not immediately obvious,
and is analyzed in detail in the section :ref:`fem:deq:diffu:anal`.
The effect of the error is at least not more severe than what is
produced by the finite difference method.


.. index:: mass matrix

.. index:: mass lumping

.. index:: lumped mass matrix


Making :math:`M` diagonal is usually referred to as *lumping the mass matrix*.
There is an alternative method to using an integration rule
based on the node points: one can sum the entries in each row, place
the sum on the diagonal, and set all other entries in the row equal
to zero. For P1 elements the methods of lumping the mass matrix give
the same result.



.. _fem:deq:diffu:BE:

Discretization in time by a Backward Euler scheme
-------------------------------------------------

Time discretization  (2)
~~~~~~~~~~~~~~~~~~~~~~~~

The Backward Euler scheme in time applied to our diffusion problem
can be expressed as follows using the finite difference operator notation:


.. math::
        
        [D_t^- u = {\alpha}\nabla^2 u + f(\boldsymbol{x}, t)]^n
        {\thinspace .}
        

Written out, and collecting the unknown :math:`u^n` on the left-hand side
and all the known terms on the right-hand side,
the time-discrete differential equation becomes


.. _Eq:fem:deq:diffu:BE:eq:un:

.. math::
   :label: fem:deq:diffu:BE:eq:un
        
        {u_{\small\mbox{e}}}^{n} - \Delta t \left( {\alpha}\nabla^2 {u_{\small\mbox{e}}}^n + f(\boldsymbol{x}, t_{n})\right) =
        {u_{\small\mbox{e}}}^{n-1}
        
        {\thinspace .}
        

Equation :eq:`fem:deq:diffu:BE:eq:un` can compute
:math:`{u_{\small\mbox{e}}}^1,{u_{\small\mbox{e}}}^2,\dots,{u_{\small\mbox{e}}}^{N_t}`,
if we have a start :math:`{u_{\small\mbox{e}}}^0=I` from the initial condition.
However, :eq:`fem:deq:diffu:BE:eq:un` is a partial differential
equation in space and needs a solution method based on discretization
in space. For this purpose we use an expansion as in
:eq:`fem:deq:diffu:femapprox:n`-:eq:`fem:deq:diffu:femapprox:np1`.

Variational forms  (2)
~~~~~~~~~~~~~~~~~~~~~~

Inserting :eq:`fem:deq:diffu:femapprox:n`-:eq:`fem:deq:diffu:femapprox:np1`
in :eq:`fem:deq:diffu:BE:eq:un`, multiplying by :math:`{\psi}_i` (or :math:`v\in V`),
and integrating by parts, as we did in the Forward Euler case, results
in the variational form


.. _Eq:fem:deq:diffu:BE:vf:u:n:

.. math::
   :label: fem:deq:diffu:BE:vf:u:n
        
        \int_{\Omega} \left( u^{n}v
        + \Delta t {\alpha}\nabla u^n\cdot\nabla v\right){\, \mathrm{d}x}
        = \int_{\Omega} u^{n-1}  v{\, \mathrm{d}x} -
        \Delta t\int_{\Omega}f^n v{\, \mathrm{d}x},\quad\forall v\in V
        
        {\thinspace .}
        

Expressed with :math:`u` as :math:`u^n` and :math:`u_1` as :math:`u^{n-1}`, this becomes


.. _Eq:fem:deq:diffu:BE:vf:u:

.. math::
   :label: fem:deq:diffu:BE:vf:u
        
        \int_{\Omega} \left( uv
        + \Delta t {\alpha}\nabla u\cdot\nabla v\right){\, \mathrm{d}x}
        = \int_{\Omega} u_1 v{\, \mathrm{d}x} +
        \Delta t\int_{\Omega}f^n v{\, \mathrm{d}x},
        
        

or with the more compact inner product notation,


.. _Eq:fem:deq:diffu:BE:vf:u:short:

.. math::
   :label: fem:deq:diffu:BE:vf:u:short
        
        (u,v) + \Delta t ({\alpha}\nabla u,\nabla v)
        = (u_1,v) +
        \Delta t (f^n,v)
        
        {\thinspace .}
        


Linear systems
~~~~~~~~~~~~~~

Inserting :math:`u=\sum_j c_j{\psi}_i` and :math:`u_1=\sum_j c_{1,j}{\psi}_i`,
and choosing :math:`v` to be the basis functions :math:`{\psi}_i\in V`,
:math:`i=0,\ldots,N`, together with doing some algebra, lead
to the following linear system to be
solved at each time level:


.. _Eq:fem:deq:diffu:BE:vf:linsys:

.. math::
   :label: fem:deq:diffu:BE:vf:linsys
        
        (M + \Delta t K)c = Mc_1 + f,
        
        

where :math:`M`, :math:`K`, and :math:`f` are as in the Forward Euler case.
This time we really have to solve a linear system at each time level.
The computational algorithm goes as follows.

1. Compute :math:`M`, :math:`K`, and :math:`A=M + \Delta t K`

2. Initialize :math:`u^0` by interpolation or projection

3. For :math:`n=1,2,\ldots,N_t`:

  1. compute :math:`b = Mc_1 + f`

  2. solve :math:`Ac = b`

  3. set :math:`c_1 = c`


In case of finite element basis functions, interpolation of the
initial condition at the nodes means :math:`c_{1,j} = I(\boldsymbol{x}_j)`. Otherwise
one has to solve the linear system :math:`\sum_j{\psi}_j(x_{i})c_j =
I(x_{i})`, where :math:`\boldsymbol{x}_j` denotes an interpolation point.  Projection
(or Galerkin's method) implies solving a linear system with :math:`M` as
coefficient matrix : :math:`\sum_j M_{i,j}c_{1,j} = (I,{\psi}_i)`,
:math:`i\in{\mathcal{I}_s}`.

We know what kind of finite difference operators the :math:`M` and :math:`K`
matrices correspond to (after dividing by :math:`h`), so
:eq:`fem:deq:diffu:BE:vf:linsys` can be interpreted as
the following finite difference method:


.. _Eq:fem:deq:diffu:BE:fdinterp:

.. math::
   :label: fem:deq:diffu:BE:fdinterp
        
        [D_t^-(u + \frac{1}{6}h^2D_xD_x u) = {\alpha} D_xD_x u + f]^n_i
        
        {\thinspace .}
        


The mass matrix :math:`M` can be lumped, as explained in the section :ref:`fem:deq:diffu:FE:fdvsP1fe`, and then the linear system arising
from the finite element method with P1 elements corresponds
to a plain Backward Euler finite difference method for the diffusion
equation:


.. _Eq:fem:deq:diffu:BE:fdinterp:lumped:

.. math::
   :label: fem:deq:diffu:BE:fdinterp:lumped
        
        [D_t^- u = {\alpha} D_xD_x u + f]^n_i
        
        {\thinspace .}
        


.. _fem:deq:diffu:Dirichlet:

Dirichlet boundary conditions
-----------------------------

Suppose now that the boundary condition :eq:`fem:deq:diffu:bcN` is
replaced by a mixed Neumann and Dirichlet condition,


.. math::
        
        u(\boldsymbol{x},t) = u_0(\boldsymbol{x},t),\quad  \boldsymbol{x}\in\partial\Omega_D,
        



.. math::
          
        -{\alpha}\frac{\partial}{\partial n} u(\boldsymbol{x},t) = g(\boldsymbol{x},t),\quad
         \boldsymbol{x}\in\partial{\Omega}_N{\thinspace .}
        


Using a Forward Euler discretization in time, the variational
form at a time level becomes


.. math::
        
        \int_\Omega u^{n+1}v{\, \mathrm{d}x} =
        \int_\Omega (u^n - \Delta t{\alpha}\nabla u^n\cdot\nabla v){\, \mathrm{d}x} -
        \Delta t\int_{\partial\Omega_N} gv{\, \mathrm{d}s},\quad \forall v\in V{\thinspace .}
        


Boundary function  (2)
~~~~~~~~~~~~~~~~~~~~~~

The Dirichlet condition :math:`u=u_0` at :math:`\partial\Omega_D` can be incorporated
through a boundary function :math:`B(\boldsymbol{x})=u_0(\boldsymbol{x})` and demanding that :math:`v=0`
at :math:`\partial\Omega_D`. The expansion for :math:`u^n` is written as


.. math::
         u^n(\boldsymbol{x}) = u_0(\boldsymbol{x},t_n) + \sum_{j\in{\mathcal{I}_s}}c_j^n{\psi}_j(\boldsymbol{x}){\thinspace .}

Inserting this expansion in the variational formulation and letting it
hold for all basis functions :math:`{\psi}_i` leads to the linear system


.. math::
        
        \sum_{j\in{\mathcal{I}_s}} \left(\int_\Omega {\psi}_i{\psi}_j{\, \mathrm{d}x}\right)
        c^{n+1}_j &= \sum_{j\in{\mathcal{I}_s}}
        \left(\int_\Omega\left( {\psi}_i{\psi}_j -
        \Delta t{\alpha}\nabla {\psi}_i\cdot\nabla{\psi}_j\right){\, \mathrm{d}x}\right) c_j^n - \\ 
        &\quad  \int_\Omega\left( u_0(\boldsymbol{x},t_{n+1}) - u_0(\boldsymbol{x},t_n)
        + \Delta t{\alpha}\nabla u_0(\boldsymbol{x},t_n)\cdot\nabla
        {\psi}_i\right){\, \mathrm{d}x} \\ 
        & \quad + \Delta t\int_\Omega f{\psi}_i{\, \mathrm{d}x} -
        \Delta t\int_{\partial\Omega_N} g{\psi}_i{\, \mathrm{d}s},
        \quad i\in{\mathcal{I}_s}{\thinspace .}
        

In the following,
we adopt the convention that the unknowns :math:`c_j^{n+1}` are written as
:math:`c_j`, while the known :math:`c_j^n` from the previous time level
are denoted by :math:`c_{1,j}`.

Finite element basis functions  (2)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When using finite elements, each basis function :math:`{\varphi}_i` is associated
with a node :math:`x_{i}`. We have a collection of nodes
:math:`\{x_{i}\}_{i\in{I_b}}` on the boundary :math:`\partial\Omega_D`.
Suppose :math:`U_k^n` is the known
Dirichlet value at :math:`x_{k}` at time :math:`t_n` (:math:`U_k^n=u_0(x_{k},t_n)`).
The appropriate boundary function is then


.. math::
         B(\boldsymbol{x},t_n)=\sum_{j\in{I_b}} U_j^n{\varphi}_j{\thinspace .}

The unknown coefficients :math:`c_j` are associated with the rest of the nodes,
which have numbers :math:`\nu(i)`, :math:`i\in{\mathcal{I}_s} = \{0,\ldots,N\}`. The basis
functions for :math:`V` are chosen as :math:`{\psi}_i = {\varphi}_{\nu(i)}`, :math:`i\in{\mathcal{I}_s}`,
and all of these vanish at the boundary nodes as they should.
The expansion for :math:`u^{n+1}` and :math:`u^n` become


.. math::
        
        u^n &= \sum_{j\in{I_b}} U_j^n{\varphi}_j + \sum_{j\in{\mathcal{I}_s}}c_{1,j}{\varphi}_{\nu(j)},\\ 
        u^{n+1} &= \sum_{j\in{I_b}} U_j^{n+1}{\varphi}_j +
        \sum_{j\in{\mathcal{I}_s}}c_{j}{\varphi}_{\nu(j)}{\thinspace .}
        

The equations for the unknown coefficients :math:`c_i` become


.. math::
        
        \sum_{j\in{\mathcal{I}_s}} \left(\int_\Omega {\varphi}_i{\varphi}_j{\, \mathrm{d}x}\right)
        c_j &= \sum_{j\in{\mathcal{I}_s}}
        \left(\int_\Omega\left( {\varphi}_i{\varphi}_j -
        \Delta t{\alpha}\nabla {\varphi}_i\cdot\nabla{\varphi}_j\right){\, \mathrm{d}x}\right) c_{1,j}
        - \\ 
        &\quad  \sum_{j\in{I_b}}\int_\Omega\left( {\varphi}_i{\varphi}_j(U_j^{n+1} - U_j^n)
        + \Delta t{\alpha}\nabla {\varphi}_i\cdot\nabla
        {\varphi}_jU_j^n\right){\, \mathrm{d}x} \\ 
        &\quad + \Delta t\int_\Omega f{\varphi}_i{\, \mathrm{d}x} -
        \Delta t\int_{\partial\Omega_N} g{\varphi}_i{\, \mathrm{d}s},
        \quad i\in{\mathcal{I}_s}{\thinspace .}
        


Modification of the linear system  (2)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Instead of introducing a boundary function :math:`B` we can work with
basis functions associated with all the nodes and incorporate the
Dirichlet conditions by modifying the linear system.
Let :math:`{\mathcal{I}_s}` be the index set that counts all the nodes:
:math:`\{0,1,\ldots,N=N_n\}`. The
expansion for :math:`u^n` is then :math:`\sum_{j\in{\mathcal{I}_s}}c^n_j{\varphi}_j` and the
variational form becomes


.. math::
        
        \sum_{j\in{\mathcal{I}_s}} \left(\int_\Omega {\varphi}_i{\varphi}_j{\, \mathrm{d}x}\right)
        c_j &= \sum_{j\in{\mathcal{I}_s}}
        \left(\int_\Omega\left( {\varphi}_i{\varphi}_j -
        \Delta t{\alpha}\nabla {\varphi}_i\cdot\nabla{\varphi}_j\right){\, \mathrm{d}x}\right) c_{1,j}
         \\ 
        &\quad - \Delta t\int_\Omega f{\varphi}_i{\, \mathrm{d}x} -
        \Delta t\int_{\partial\Omega_N} g{\varphi}_i{\, \mathrm{d}s}{\thinspace .}
        

We introduce the matrices :math:`M` and :math:`K` with entries
:math:`M_{i,j}=\int_\Omega{\varphi}_i{\varphi}_j{\, \mathrm{d}x}` and
:math:`K_{i,j}=\int_\Omega{\alpha}\nabla{\varphi}_i\cdot\nabla{\varphi}_j{\, \mathrm{d}x}`,
respectively.
In addition, we define the vectors :math:`c`, :math:`c_1`, and :math:`f` with
entries :math:`c_i`, :math:`c_{1,i}`, and
:math:`\int_\Omega f{\varphi}_i{\, \mathrm{d}x} - \int_{\partial\Omega_N}g{\varphi}_i{\, \mathrm{d}s}`.
The equation system can then be written as


.. math::
        
        Mc = Mc_1 - \Delta t Kc_1 + \Delta t f{\thinspace .}
        

When :math:`M`, :math:`K`, and :math:`b` are assembled without paying attention to
Dirichlet boundary conditions, we need to replace equation :math:`k`
by :math:`c_k=U_k` for :math:`k` corresponding to all boundary nodes (:math:`k\in{I_b}`).
The modification of :math:`M` consists in setting :math:`M_{k,j}=0`, :math:`j\in{\mathcal{I}_s}`, and
the :math:`M_{k,k}=1`. Alternatively, a modification that preserves
the symmetry of :math:`M` can be applied. At each time level one forms
:math:`b = Mc_1 - \Delta t Kc_1 + \Delta t f` and sets :math:`b_k=U^{n+1}_k`,
:math:`k\in{I_b}`, and solves the system :math:`Mc=b`.

In case of a Backward Euler method, the system becomes
:eq:`fem:deq:diffu:BE:vf:linsys`. We can write the system
as :math:`Ac=b`, with :math:`A=M + \Delta t K` and :math:`b = Mc_1 + f`.
Both :math:`M` and :math:`K` needs to be modified because of Dirichlet
boundary conditions, but the diagonal entries in :math:`K` should be
set to zero and those in :math:`M` to unity. In this way, :math:`A_{k,k}=1`.
The right-hand side must read :math:`b_k=U^n_k` for :math:`k\in{I_b}` (assuming
the unknown is sought at time level :math:`t_n`).

.. _fem:deq:diffu:Dirichlet:ex:

Example: Oscillating Dirichlet boundary condition
-------------------------------------------------

We shall address the one-dimensional initial-boundary value problem


.. _Eq:fem:deq:diffu:Dirichlet:ex:pde:

.. math::
   :label: fem:deq:diffu:Dirichlet:ex:pde
        
        u_t = ({\alpha} u_x)_x + f,\quad  \boldsymbol{x}\in\Omega =[0,L],\ t\in (0,T],
         
        



.. _Eq:fem:deq:diffu:Dirichlet:ex:uic:

.. math::
   :label: fem:deq:diffu:Dirichlet:ex:uic
          
        u(x,0) = 0,\quad  \boldsymbol{x}\in\Omega,
        
        



.. _Eq:fem:deq:diffu:Dirichlet:ex:uL:

.. math::
   :label: fem:deq:diffu:Dirichlet:ex:uL
          
        u(0,t) = a\sin\omega t,\quad  t\in (0,T],
        
        



.. _Eq:fem:deq:diffu:Dirichlet:ex:uR:

.. math::
   :label: fem:deq:diffu:Dirichlet:ex:uR
          
        u_x(L,t) = 0,\quad  t\in (0,T]{\thinspace .}
        
        

A physical interpretation may be that :math:`u` is the temperature
deviation from a constant mean temperature in a body :math:`\Omega`
that is subject to an oscillating temperature (e.g., day and
night, or seasonal, variations) at :math:`x=0`.

We use a Backward Euler scheme in time and P1 elements of
constant length :math:`h` in space.
Incorporation of the Dirichlet condition at :math:`x=0` through
modifying the linear system at each time level means that we
carry out the computations as explained in the section :ref:`fem:deq:diffu:BE` and get a system :eq:`fem:deq:diffu:BE:vf:linsys`.
The :math:`M` and :math:`K` matrices computed without paying attention to
Dirichlet boundary conditions become


.. math::
        
        M = \frac{h}{6}
        \left(
        \begin{array}{cccccccccc}
        2  1  0
        \cdots  \cdots  \cdots  \cdots  \cdots  0 
        



.. math::
          
        1  4  1  \ddots          \vdots 
        



.. math::
          
        0  1  4  1 
        \ddots       \vdots 
        



.. math::
          
        \vdots  \ddots    \ddots  \ddots  0     \vdots 
        



.. math::
          
        \vdots    \ddots  \ddots  \ddots  \ddots  \ddots   \vdots 
        



.. math::
          
        \vdots     0  1  4  1  \ddots  \vdots 
        



.. math::
          
        \vdots      \ddots  \ddots  \ddots \ddots   0 
        



.. math::
          
        \vdots      \ddots   1   4   1 
        



.. math::
          
        0 \cdots  \cdots \cdots  \cdots  \cdots   0  1  2
        \end{array}
        \right)
        



.. math::
          
        K = \frac{{\alpha}}{h}
        \left(
        \begin{array}{cccccccccc}
        1  -1  0 \cdots  \cdots  \cdots  \cdots  \cdots  0 
        



.. math::
          
        -1  2  -1  \ddots          \vdots 
        



.. math::
          
        0  -1  2  -1  \ddots       \vdots 
        



.. math::
          
        \vdots  \ddots    \ddots  \ddots  0     \vdots 
        



.. math::
          
        \vdots    \ddots  \ddots  \ddots  \ddots  \ddots   \vdots 
        



.. math::
          
        \vdots     0  -1  2  -1  \ddots  \vdots 
        



.. math::
          
        \vdots      \ddots  \ddots  \ddots \ddots   0 
        



.. math::
          
        \vdots      \ddots   -1   2   -1 
        



.. math::
          
        0 \cdots  \cdots \cdots  \cdots  \cdots   0  -1  1
        \end{array}
        \right)
        

The right-hand side of the variational form contains :math:`Mc_1` since
there is no source term (:math:`f`) and no boundary term from the
integration by parts (:math:`u_x=0` at :math:`x=L` and we compute as if :math:`u_x=0` at
:math:`x=0` too). We must incorporate the Dirichlet boundary
condition :math:`c_0=a\sin\omega t_n` by ensuring that this is the
first equation in the linear system. To this end,
the first row in :math:`K` and :math:`M` are set to zero, but the diagonal
entry :math:`M_{0,0}` is set to 1. The right-hand side is :math:`b=Mc_1`,
and we set :math:`b_0 = a\sin\omega t_n`.
Note that in this
approach, :math:`N=N_n`, and :math:`c` equals the unknown :math:`u` at each node
in the mesh. We can write the complete linear system as


.. math::
        
        c_0 = a\sin\omega t_n,
        



.. math::
          
        \frac{h}{6}(c_{i-1} + 4c_i + c_{i+1}) + \Delta t\frac{{\alpha}}{h}(-c_{i-1}
        +2c_i + c_{i+1}) = \frac{h}{6}(c_{1,i-1} + 4c_{1,i} + c_{1,i+1}),
        



.. math::
          
        \qquad i=1,\ldots,N_n-1,\nonumber
        



.. math::
          
        \frac{h}{6}(c_{i-1} + 2c_i) + \Delta t\frac{{\alpha}}{h}(-c_{i-1}
        +c_i) = \frac{h}{6}(c_{1,i-1} + 2c_{1,i}),\quad i=N_n{\thinspace .}
        


The Dirichlet boundary condition can alternatively be implemented
through a boundary function :math:`B(x,t)=a\sin\omega t{\varphi}_0(x)`:


.. math::
         u^n(x) = a\sin\omega t_n{\varphi}_0(x) +
        \sum_{j\in{\mathcal{I}_s}} c_j{\varphi}_{\nu(j)}(x),\quad
        \nu(j) = j+1{\thinspace .}

Now, :math:`N=N_n-1` and the :math:`c` vector contains values of :math:`u` at nodes
:math:`1,2,\ldots,N_n`. The right-hand side gets a contribution


.. _Eq:fem:deq:diffu:Dirichlet:ex:bterm:

.. math::
   :label: fem:deq:diffu:Dirichlet:ex:bterm
        
        \int_0^L \left(
        a(\sin\omega t_n - \sin\omega t_{n-1}){\varphi}_0{\varphi}_i
        - \Delta t{\alpha} a\sin\omega t_n\nabla{\varphi}_0\cdot\nabla{\varphi}_i\right){\, \mathrm{d}x}
        {\thinspace .}
        
        



.. _fem:deq:diffu:anal:

Analysis of the discrete equations
----------------------------------

The diffusion equation :math:`u_t = {\alpha} u_{xx}` allows a (Fourier)
wave component :math:`u=\exp{(\beta t + ikx)}` as solution if
:math:`\beta = -{\alpha} k^2`, which follows from inserting the wave component
in the equation. The exact wave component can alternatively be written
as


.. _Eq:fem:deq:diffu:analysis:Ae:

.. math::
   :label: fem:deq:diffu:analysis:Ae
        
        u = {A_{\small\mbox{e}}}^n e^{ikx},\quad {A_{\small\mbox{e}}} = e^{-{\alpha} k^2\Delta t}{\thinspace .}
        
        

Many numerical schemes for the diffusion equation
has a similar wave component as solution:


.. _Eq:fem:deq:diffu:analysis:uni0:

.. math::
   :label: fem:deq:diffu:analysis:uni0
        
        u^n_q = A^n e^{ikx},
        
        

where is an amplification factor to be calculated by inserting
:eq:`fem:deq:diffu:analysis:uni` in the scheme.
We introduce :math:`x=qh`, or :math:`x=q\Delta x` to align the notation with
that frequently used in finite difference methods.

A convenient start of the calculations is to establish some
results for various finite difference operators acting
on


.. _Eq:fem:deq:diffu:analysis:uni:

.. math::
   :label: fem:deq:diffu:analysis:uni
        
        u^n_q = A^n e^{ikq\Delta x}{\thinspace .}
        
        



.. math::
        
        [D_t^+ A^n e^{ikq\Delta x}]^n &= A^n e^{ikq\Delta x}\frac{A-1}{\Delta t},\\ 
        [D_t^- A^n e^{ikq\Delta x}]^n &= A^n e^{ikq\Delta x}\frac{1-A^{-1}}{\Delta t},\\ 
        [D_t A^n e^{ikq\Delta x}]^{n+\frac{1}{2}} &= A^{n+\frac{1}{2}} e^{ikq\Delta x}\frac{A^{\frac{1}{2}}-A^{-\frac{1}{2}}}{\Delta t} = A^ne^{ikq\Delta x}\frac{A-1}{\Delta t},\\ 
        [D_xD_x A^ne^{ikq\Delta x}]_q &= -A^n \frac{4}{\Delta x^2}\sin^2\left(\frac{k\Delta x}{2}\right){\thinspace .}
        


Forward Euler discretization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We insert :eq:`fem:deq:diffu:analysis:uni` in the
Forward Euler scheme with P1 elements in space and :math:`f=0` (this type of analysis
can only be carried out if :math:`f=0`),


.. _Eq:fem:deq:diffu:FE:fdinterp2:

.. math::
   :label: fem:deq:diffu:FE:fdinterp2
        
        [D_t^+(u + \frac{1}{6}h^2D_xD_x u) = {\alpha} D_xD_x u]^n_q
        
        {\thinspace .}
        

We have


.. math::
         [D_t^+D_xD_x Ae^{ikx}]^n_q = [D_t^+A]^n [D_xD_x e^{ikx}]_q
        = -A^ne^{ikp\Delta x}
        \frac{A-1}{\Delta t}\frac{4}{\Delta x^2}\sin^2 (\frac{k\Delta x}{2})
        {\thinspace .}  

The term :math:`[D_t^+Ae^{ikx} + \frac{1}{6}\Delta x^2 D_t^+D_xD_x Ae^{ikx}]^n_q`
then reduces to


.. math::
         \frac{A-1}{\Delta t} - \frac{1}{6}\Delta x^2 \frac{A-1}{\Delta t}
        \frac{4}{\Delta x^2}\sin^2 (\frac{k\Delta x}{2}), 

or

.. math::
         \frac{A-1}{\Delta t} \left(1 - \frac{2}{3}\sin^2 (k\Delta x/2)\right)
        {\thinspace .}  

Introducing :math:`p=k\Delta x/2` and :math:`C={\alpha}\Delta t/\Delta x^2`,
the complete scheme becomes


.. math::
        
        (A-1) \left(1 - \frac{2}{3}\sin^2 p\right)
        = -4C\sin^2 p,

from which we find :math:`A` to be


.. math::
         A = 1 - 4C\frac{\sin^2 p}{1 - \frac{2}{3}\sin^2 p}
        {\thinspace .} 


How does this :math:`A` change the stability criterion compared to the
Forward Euler finite difference scheme and centered differences in
space? The stability criterion is :math:`|A|\leq 1`, which here implies
:math:`A\leq 1` and :math:`A\geq -1`. The former is always fulfilled, while
the latter leads to


.. math::
        
        4C\frac{\sin^2 p}{1 + \frac{2}{3}\sin^2 p} \leq 2{\thinspace .}
        

The factor :math:`\sin^2 p/(1 - \frac{2}{3}\sin^2 p)`
can be plotted for :math:`p\in [0,\pi/2]`, and the maximum value goes to 3
as :math:`p\rightarrow \pi/2`. The worst case for stability therefore occurs for
the shortest possible wave, :math:`p=\pi/2`, and the stability criterion becomes


.. math::
        
        C\leq \frac{1}{6}\quad\Rightarrow\quad \Delta t\leq \frac{\Delta x^2}{6{\alpha}},
        

which is a factor 1/3 worse than for the standard Forward Euler
finite difference method for the diffusion equation, which demands
:math:`C\leq 1/2`.
Lumping the mass matrix will, however, recover the finite difference
method and therefore imply :math:`C\leq 1/2` for stability.


Backward Euler discretization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can use the same approach and insert
:eq:`fem:deq:diffu:analysis:uni` in the
Backward Euler scheme with P1 elements in space and :math:`f=0`:


.. _Eq:fem:deq:diffu:BE:fdinterp2:

.. math::
   :label: fem:deq:diffu:BE:fdinterp2
        
        [D_t^-(u + \frac{1}{6}h^2D_xD_x u) = {\alpha} D_xD_x u]^n_i
        
        {\thinspace .}
        

Similar calculations as in the Forward Euler case lead to


.. math::
        
        (1-A^{-1}) \left(1 - \frac{2}{3}\sin^2 p\right)
        = -4C\sin^2 p,

and hence


.. math::
        
        A = \left( 1 + 4C\frac{\sin^2 p}{1 - \frac{2}{3}\sin^2 p}\right)^{-1}
        {\thinspace .}
        


Comparing amplification factors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It is of interest to compare :math:`A` and :math:`{A_{\small\mbox{e}}}` as functions of :math:`p`
for some :math:`C` values. Figure
:ref:`fem:deq:diffu:fig:A:BE` display the amplification factors
for the Backward Euler scheme corresponding
a coarse mesh with :math:`C=2` and a mesh at the stability limit
of the Forward Euler scheme in the finite difference method,
:math:`C=1/2`. Figures
:ref:`fem:deq:diffu:fig:A:FE2` and :ref:`fem:deq:diffu:fig:A:BE2` shows how
the accuracy increases with lower :math:`C` values for both the
Forward Euler and Backward schemes, respectively.
The striking fact, however, is that the accuracy of the finite element
method is significantly less than the finite difference method for
the same value of :math:`C`. Lumping the mass matrix to recover the
numerical amplification factor :math:`A` of the finite difference method
is therefore a good idea in this problem.


.. _fem:deq:diffu:fig:A:BE:

.. figure:: diffu_A_factors2_BE.png
   :width: 600

   *Comparison of coarse-mesh amplification factors for Backward Euler discretization of a 1D diffusion equation*



.. _fem:deq:diffu:fig:A:FE2:

.. figure:: diffu_A_factors2_fine_FE.png
   :width: 600

   *Comparison of fine-mesh amplification factors for Forward Euler discretization of a 1D diffusion equation*



.. _fem:deq:diffu:fig:A:BE2:

.. figure:: diffu_A_factors2_fine_BE.png
   :width: 600

   *Comparison of fine-mesh amplification factors for Backward Euler discretization of a 1D diffusion equation*


.. FIGURE: [fig-fem/diffu_A_factors2_FE, width=400 frac=0.8] Comparison of amplification factors for Forward Euler discretization of a 1D diffusion equation.


.. FIGURE: [fig-fem/diffu_A_factors2_FE, width=400 frac=0.8] Comparison of amplification factors for Forward Euler discretization of a 1D diffusion equation.


Remaining tasks:

 * Taylor expansion of the error in the amplification factor :math:`{A_{\small\mbox{e}}} - A`

 * Taylor expansion of the error :math:`e = ({A_{\small\mbox{e}}}^n - A^n)e^{ikx}`

 * :math:`L^2` norm of :math:`e`