FEM - From Single Elements to Global Matrices

In our series of blog posts

Hows, Whys and Wherefores of FEM in Quantitative Finance

we explained how to obtain the different matrices (stiffness, mass,...) for a single element but did not discuss the assembling process. Irrespective of the type of the partial differential equation (diffusion, reaction-diffusion, reaction-convection-diffusion), for a time dependent problem with a time discretisation Θ≠0  a system of linear equations

must be solved in each time step. For instance, for a single triangular element (with nodes
i,j,k) with linear test functions and a fully implicit time stepping scheme (Θ=1),
the resulting coefficient matrix U of the system of linear equations of a
diffusion-reaction equation is

where the corresponding right-hand-side vector is given by

To obtain a solution, terminal/initial and boundary conditions must be specified. The procedure to incorporate these element matrices into a global system of equations is called direct stiffness method.

I will now illustrate the procedure using a simple triangular mesh (see figure below)
consisting of 6 nodes and 4 elements.
The figure shows the simple mesh used for explaining the global matrix assembling process. Numbers in frames denote element numbers, numbers without frames are the node numbers. 

The table below connects the global node numbers to the local element numbers i,j,k.

The dimension of the coefficient matrix is 6x6 (6 unknowns). We will now loop through the elements and add their contributions to the global coefficient matrix - this assembling process is shown step by step in the next table table. The position for the entries of a single element (3x3 matrix) are determined by the global node numbers of the element's node.

Form of the global matrix after assembling the first (first table), the first and the second (second table) element and all (third table) elements. 

An identical procedure is performed for the assembling of the right-hand-side vector:

A last blog post in our FEM series will deal with the incorporation of boundary condition to our matrices - it will be published on Tuesday the 13th of August.
The post excerpts a chapter of the book A Workout in Computational Finance.