$$ \newcommand{\uex}{{u_{\small\mbox{e}}}} \newcommand{\Aex}{{A_{\small\mbox{e}}}} \newcommand{\half}{\frac{1}{2}} \newcommand{\halfi}{{1/2}} \newcommand{\tp}{\thinspace .} \newcommand{\normalvec}{\boldsymbol{n}} \newcommand{\x}{\boldsymbol{x}} \newcommand{\X}{\boldsymbol{X}} \renewcommand{\u}{\boldsymbol{u}} \renewcommand{\v}{\boldsymbol{v}} \newcommand{\w}{\boldsymbol{w}} \newcommand{\V}{\boldsymbol{V}} \newcommand{\e}{\boldsymbol{e}} \newcommand{\f}{\boldsymbol{f}} \newcommand{\dfc}{\alpha} % diffusion coefficient \newcommand{\Ix}{\mathcal{I}_x} \newcommand{\Iy}{\mathcal{I}_y} \newcommand{\Iz}{\mathcal{I}_z} \newcommand{\If}{\mathcal{I}_s} % for FEM \newcommand{\Ifd}{{I_d}} % for FEM \newcommand{\Ifb}{{I_b}} % for FEM \newcommand{\sequencei}[1]{\left\{ {#1}_i \right\}_{i\in\If}} \newcommand{\basphi}{\varphi} \newcommand{\baspsi}{\psi} \newcommand{\refphi}{\tilde\basphi} \newcommand{\psib}{\boldsymbol{\psi}} \newcommand{\sinL}[1]{\sin\left((#1+1)\pi\frac{x}{L}\right)} \newcommand{\xno}[1]{x_{#1}} \newcommand{\Xno}[1]{X_{(#1)}} \newcommand{\yno}[1]{y_{#1}} \newcommand{\xdno}[1]{\boldsymbol{x}_{#1}} \newcommand{\dX}{\, \mathrm{d}X} \newcommand{\dx}{\, \mathrm{d}x} \newcommand{\ds}{\, \mathrm{d}s} \newcommand{\Real}{\mathbb{R}} \newcommand{\Integer}{\mathbb{Z}} $$

 

 

 

Finite element basis functions

The specific basis functions exemplified in the section Approximation of functions are in general nonzero on the entire domain \( \Omega \), see Figure 13 for an example where we plot \( \psi_0(x)=\sin\half\pi x \) and \( \psi_1(x)=\sin 2\pi x \) together with a possible sum \( u(x)=4\psi_0(x) - \half\psi_1(x) \). We shall now turn the attention to basis functions that have compact support, meaning that they are nonzero on only a small portion of \( \Omega \). Moreover, we shall restrict the functions to be piecewise polynomials. This means that the domain is split into subdomains and the function is a polynomial on one or more subdomains, see Figure 14 for a sketch involving locally defined hat functions that make \( u=\sum_jc_j\baspsi_j \) piecewise linear. At the boundaries between subdomains one normally forces continuity of the function only so that when connecting two polynomials from two subdomains, the derivative becomes discontinuous. These type of basis functions are fundamental in the finite element method.


Figure 13: A function resulting from adding two sine basis functions.


Figure 14: A function resulting from adding three local piecewise linear (hat) functions.

We first introduce the concepts of elements and nodes in a simplistic fashion as often met in the literature. Later, we shall generalize the concept of an element, which is a necessary step to treat a wider class of approximations within the family of finite element methods. The generalization is also compatible with the concepts used in the FEniCS finite element software.

Elements and nodes

Let us divide the interval \( \Omega \) on which \( f \) and \( u \) are defined into non-overlapping subintervals \( \Omega^{(e)} \), \( e=0,\ldots,N_e \): $$ \begin{equation} \Omega = \Omega^{(0)}\cup \cdots \cup \Omega^{(N_e)}\tp \end{equation} $$ We shall for now refer to \( \Omega^{(e)} \) as an element, having number \( e \). On each element we introduce a set of points called nodes. For now we assume that the nodes are uniformly spaced throughout the element and that the boundary points of the elements are also nodes. The nodes are given numbers both within an element and in the global domain. These are referred to as local and global node numbers, respectively. Figure 15 shows element boundaries with small vertical lines, nodes as small disks, element numbers in circles, and global node numbers under the nodes.


Figure 15: Finite element mesh with 5 elements and 6 nodes.

Nodes and elements uniquely define a finite element mesh, which is our discrete representation of the domain in the computations. A common special case is that of a uniformly partitioned mesh where each element has the same length and the distance between nodes is constant.

Example

On \( \Omega =[0,1] \) we may introduce two elements, \( \Omega^{(0)}=[0,0.4] \) and \( \Omega^{(1)}=[0.4,1] \). Furthermore, let us introduce three nodes per element, equally spaced within each element. Figure 16 shows the mesh. The three nodes in element number 0 are \( x_0=0 \), \( x_1=0.2 \), and \( x_2=0.4 \). The local and global node numbers are here equal. In element number 1, we have the local nodes \( x_0=0.4 \), \( x_1=0.7 \), and \( x_2=1 \) and the corresponding global nodes \( x_2=0.4 \), \( x_3=0.7 \), and \( x_4=1 \). Note that the global node \( x_2=0.4 \) is shared by the two elements.


Figure 16: Finite element mesh with 2 elements and 5 nodes.

For the purpose of implementation, we introduce two lists or arrays: nodes for storing the coordinates of the nodes, with the global node numbers as indices, and elements for holding the global node numbers in each element, with the local node numbers as indices. The nodes and elements lists for the sample mesh above take the form

nodes = [0, 0.2, 0.4, 0.7, 1]
elements = [[0, 1, 2], [2, 3, 4]]

Looking up the coordinate of local node number 2 in element 1 is here done by nodes[elements[1][2]] (recall that nodes and elements start their numbering at 0).

The numbering of elements and nodes does not need to be regular. Figure 17 shows and example corresponding to

nodes = [1.5, 5.5, 4.2, 0.3, 2.2, 3.1]
elements = [[2, 1], [4, 5], [0, 4], [3, 0], [5, 2]]


Figure 17: Example on irregular numbering of elements and nodes.

The basis functions

Construction principles

Finite element basis functions are in this text recognized by the notation \( \basphi_i(x) \), where the index now in the beginning corresponds to a global node number. In the current approximation problem we shall simply take \( \baspsi_i = \basphi_i \).

Let \( i \) be the global node number corresponding to local node \( r \) in element number \( e \). The finite element basis functions \( \basphi_i \) are now defined as follows.

A visual impression of three such basis functions are given in Figure 18.


Figure 18: Illustration of the piecewise quadratic basis functions associated with nodes in element 1.

Properties of \( \basphi_i \)

The construction of basis functions according to the principles above lead to two important properties of \( \basphi_i(x) \). First, $$ \begin{equation} \basphi_i(\xno{j}) =\delta_{ij},\quad \delta_{ij} = \left\lbrace\begin{array}{ll} 1, & i=j,\\ 0, & i\neq j, \end{array}\right. \tag{23} \end{equation} $$ when \( \xno{j} \) is a node in the mesh with global node number \( j \). The result \( \basphi_i(\xno{j}) =\delta_{ij} \) arises because the Lagrange polynomials are constructed to have exactly this property. The property also implies a convenient interpretation of \( c_i \) as the value of \( u \) at node \( i \). To show this, we expand \( u \) in the usual way as \( \sum_jc_j\baspsi_j \) and choose \( \baspsi_i = \basphi_i \): $$ u(\xno{i}) = \sum_{j\in\If} c_j \baspsi_j (\xno{i}) = \sum_{j\in\If} c_j \basphi_j (\xno{i}) = c_i \basphi_i (\xno{i}) = c_i \tp $$ Because of this interpretation, the coefficient \( c_i \) is by many named \( u_i \) or \( U_i \).

Second, \( \basphi_i(x) \) is mostly zero throughout the domain:

Since \( A_{i,j} \) is the integral of \( \basphi_i\basphi_j \) it means that most of the elements in the coefficient matrix will be zero. We will come back to these properties and use them actively in computations to save memory and CPU time.

We let each element have \( d+1 \) nodes, resulting in local Lagrange polynomials of degree \( d \). It is not a requirement to have the same \( d \) value in each element, but for now we will assume so.

Example on piecewise quadratic finite element functions

Figure 18 illustrates how piecewise quadratic basis functions can look like (\( d=2 \)). We work with the domain \( \Omega = [0,1] \) divided into four equal-sized elements, each having three nodes. The nodes and elements lists in this particular example become

nodes = [0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]
elements = [[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8]]

Figure 19 sketches the mesh and the numbering. Nodes are marked with circles on the \( x \) axis and element boundaries are marked with vertical dashed lines in Figure 18.


Figure 19: Sketch of mesh with 4 elements and 3 nodes per element.

Let us explain in detail how the basis functions are constructed according to the principles. Consider element number 1 in Figure 18, \( \Omega^{(1)}=[0.25, 0.5] \), with local nodes 0, 1, and 2 corresponding to global nodes 2, 3, and 4. The coordinates of these nodes are \( 0.25 \), \( 0.375 \), and \( 0.5 \), respectively. We define three Lagrange polynomials on this element:

  1. The polynomial that is 1 at local node 1 (\( x=0.375 \), global node 3) makes up the basis function \( \basphi_3(x) \) over this element, with \( \basphi_3(x)=0 \) outside the element.
  2. The Lagrange polynomial that is 1 at local node 0 is the "right part" of the global basis function \( \basphi_2(x) \). The "left part" of \( \basphi_2(x) \) consists of a Lagrange polynomial associated with local node 2 in the neighboring element \( \Omega^{(0)}=[0, 0.25] \).
  3. Finally, the polynomial that is 1 at local node 2 (global node 4) is the "left part" of the global basis function \( \basphi_4(x) \). The "right part" comes from the Lagrange polynomial that is 1 at local node 0 in the neighboring element \( \Omega^{(2)}=[0.5, 0.75] \).
As mentioned earlier, any global basis function \( \basphi_i(x) \) is zero on elements that do not contain the node with global node number \( i \).

The other global functions associated with internal nodes, \( \basphi_1 \), \( \basphi_5 \), and \( \basphi_7 \), are all of the same shape as the drawn \( \basphi_3 \), while the global basis functions associated with shared nodes also have the same shape, provided the elements are of the same length.


Figure 20: Illustration of the piecewise linear basis functions associated with nodes in element 1.

Example on piecewise linear finite element functions

Figure 20 shows piecewise linear basis functions (\( d=1 \)). Also here we have four elements on \( \Omega = [0,1] \). Consider the element \( \Omega^{(1)}=[0.25,0.5] \). Now there are no internal nodes in the elements so that all basis functions are associated with nodes at the element boundaries and hence made up of two Lagrange polynomials from neighboring elements. For example, \( \basphi_1(x) \) results from the Lagrange polynomial in element 0 that is 1 at local node 1 and 0 at local node 0, combined with the Lagrange polynomial in element 1 that is 1 at local node 0 and 0 at local node 1. The other basis functions are constructed similarly.

Explicit mathematical formulas are needed for \( \basphi_i(x) \) in computations. In the piecewise linear case, one can show that $$ \begin{equation} \basphi_i(x) = \left\lbrace\begin{array}{ll} 0, & x < \xno{i-1},\\ (x - \xno{i-1})/(\xno{i} - \xno{i-1}), & \xno{i-1} \leq x < \xno{i},\\ 1 - (x - x_{i})/(\xno{i+1} - x_{i}), & \xno{i} \leq x < \xno{i+1},\\ 0, & x\geq \xno{i+1}\tp \end{array} \right. \tag{24} \end{equation} $$ Here, \( \xno{j} \), \( j=i-1,i,i+1 \), denotes the coordinate of node \( j \). For elements of equal length \( h \) the formulas can be simplified to $$ \begin{equation} \basphi_i(x) = \left\lbrace\begin{array}{ll} 0, & x < \xno{i-1},\\ (x - \xno{i-1})/h, & \xno{i-1} \leq x < \xno{i},\\ 1 - (x - x_{i})/h, & \xno{i} \leq x < \xno{i+1},\\ 0, & x\geq \xno{i+1} \end{array} \right. \tag{25} \end{equation} $$

Example on piecewise cubic finite element basis functions

Piecewise cubic basis functions can be defined by introducing four nodes per element. Figure 21 shows examples on \( \basphi_i(x) \), \( i=3,4,5,6 \), associated with element number 1. Note that \( \basphi_4 \) and \( \basphi_5 \) are nonzero on element number 1, while \( \basphi_3 \) and \( \basphi_6 \) are made up of Lagrange polynomials on two neighboring elements.


Figure 21: Illustration of the piecewise cubic basis functions associated with nodes in element 1.

We see that all the piecewise linear basis functions have the same "hat" shape. They are naturally referred to as hat functions, also called chapeau functions. The piecewise quadratic functions in Figure 18 are seen to be of two types. "Rounded hats" associated with internal nodes in the elements and some more "sombrero" shaped hats associated with element boundary nodes. Higher-order basis functions also have hat-like shapes, but the functions have pronounced oscillations in addition, as illustrated in Figure 21.

A common terminology is to speak about linear elements as elements with two local nodes associated with piecewise linear basis functions. Similarly, quadratic elements and cubic elements refer to piecewise quadratic or cubic functions over elements with three or four local nodes, respectively. Alternative names, frequently used later, are P1 elements for linear elements, P2 for quadratic elements, and so forth: Pd signifies degree \( d \) of the polynomial basis functions.

Calculating the linear system

The elements in the coefficient matrix and right-hand side are given by the formulas (17) and (18), but now the choice of \( \baspsi_i \) is \( \basphi_i \). Consider P1 elements where \( \basphi_i(x) \) piecewise linear. Nodes and elements numbered consecutively from left to right in a uniformly partitioned mesh imply the nodes $$ x_i=i h,\quad i=0,\ldots,N,$$ and the elements $$ \begin{equation} \Omega^{(i)} = [\xno{i},\xno{i+1}] = [i h, (i+1)h],\quad i=0,\ldots,N_e=N-1 \tp \end{equation} $$ We have in this case \( N \) elements and \( N+1 \) nodes, and \( \Omega=[\xno{0},\xno{N}] \). The formula for \( \basphi_i(x) \) is given by (25) and a graphical illustration is provided in Figures 20 and 23. First we clearly see from the figures the very important property \( \basphi_i(x)\basphi_j(x)\neq 0 \) if and only if \( j=i-1 \), \( j=i \), or \( j=i+1 \), or alternatively expressed, if and only if \( i \) and \( j \) are nodes in the same element. Otherwise, \( \basphi_i \) and \( \basphi_j \) are too distant to have an overlap and consequently their product vanishes.


Figure 22: Illustration of the piecewise linear basis functions corresponding to global node 2 and 3.

Calculating a specific matrix entry

Let us calculate the specific matrix entry \( A_{2,3} = \int_\Omega \basphi_2\basphi_3\dx \). Figure 22 shows how \( \basphi_2 \) and \( \basphi_3 \) look like. We realize from this figure that the product \( \basphi_2\basphi_3\neq 0 \) only over element 2, which contains node 2 and 3. The particular formulas for \( \basphi_{2}(x) \) and \( \basphi_3(x) \) on \( [\xno{2},\xno{3}] \) are found from (25). The function \( \basphi_3 \) has positive slope over \( [\xno{2},\xno{3}] \) and corresponds to the interval \( [\xno{i-1},\xno{i}] \) in (25). With \( i=3 \) we get $$ \basphi_3(x) = (x-x_2)/h,$$ while \( \basphi_2(x) \) has negative slope over \( [\xno{2},\xno{3}] \) and corresponds to setting \( i=2 \) in (25), $$ \basphi_2(x) = 1- (x-x_2)/h\tp$$ We can now easily integrate, $$ A_{2,3} = \int_\Omega \basphi_2\basphi_{3}\dx = \int_{\xno{2}}^{\xno{3}} \left(1 - \frac{x - \xno{2}}{h}\right) \frac{x - x_{2}}{h} \dx = \frac{h}{6}\tp $$

The diagonal entry in the coefficient matrix becomes $$ A_{2,2} = \int_{\xno{1}}^{\xno{2}} \left(\frac{x - \xno{1}}{h}\right)^2\dx + \int_{\xno{2}}^{\xno{3}} \left(1 - \frac{x - \xno{2}}{h}\right)^2\dx = \frac{2h}{3}\tp $$ The entry \( A_{2,1} \) has an the integral that is geometrically similar to the situation in Figure 22, so we get \( A_{2,1}=h/6 \).

Calculating a general row in the matrix

We can now generalize the calculation of matrix entries to a general row number \( i \). The entry \( A_{i,i-1}=\int_\Omega\basphi_i\basphi_{i-1}\dx \) involves hat functions as depicted in Figure 23. Since the integral is geometrically identical to the situation with specific nodes 2 and 3, we realize that \( A_{i,i-1}=A_{i,i+1}=h/6 \) and \( A_{i,i}=2h/3 \). However, we can compute the integral directly too: $$ \begin{align*} A_{i,i-1} &= \int_\Omega \basphi_i\basphi_{i-1}\dx\\ &= \underbrace{\int_{\xno{i-2}}^{\xno{i-1}} \basphi_i\basphi_{i-1}\dx}_{\basphi_i=0} + \int_{\xno{i-1}}^{\xno{i}} \basphi_i\basphi_{i-1}\dx + \underbrace{\int_{\xno{i}}^{\xno{i+1}} \basphi_i\basphi_{i-1}\dx}_{\basphi_{i-1}=0}\\ &= \int_{\xno{i-1}}^{\xno{i}} \underbrace{\left(\frac{x - x_{i}}{h}\right)}_{\basphi_i(x)} \underbrace{\left(1 - \frac{x - \xno{i-1}}{h}\right)}_{\basphi_{i-1}(x)} \dx = \frac{h}{6} \tp \end{align*} $$ The particular formulas for \( \basphi_{i-1}(x) \) and \( \basphi_i(x) \) on \( [\xno{i-1},\xno{i}] \) are found from (25): \( \basphi_i \) is the linear function with positive slope, corresponding to the interval \( [\xno{i-1},\xno{i}] \) in (25), while \( \phi_{i-1} \) has a negative slope so the definition in interval \( [\xno{i},\xno{i+1}] \) in (25) must be used. (The appearance of \( i \) in (25) and the integral might be confusing, as we speak about two different \( i \) indices.)


Figure 23: Illustration of two neighboring linear (hat) functions with general node numbers.

The first and last row of the coefficient matrix lead to slightly different integrals: $$ A_{0,0} = \int_\Omega \basphi_0^2\dx = \int_{\xno{0}}^{\xno{1}} \left(1 - \frac{x-x_0}{h}\right)^2\dx = \frac{h}{3}\tp $$ Similarly, \( A_{N,N} \) involves an integral over only one element and equals hence \( h/3 \).


Figure 24: Right-hand side integral with the product of a basis function and the given function to approximate.

The general formula for \( b_i \), see Figure 24, is now easy to set up $$ \begin{equation} b_i = \int_\Omega\basphi_i(x)f(x)\dx = \int_{\xno{i-1}}^{\xno{i}} \frac{x - \xno{i-1}}{h} f(x)\dx + \int_{x_{i}}^{\xno{i+1}} \left(1 - \frac{x - x_{i}}{h}\right) f(x) \dx\tp \tag{26} \end{equation} $$ We need a specific \( f(x) \) function to compute these integrals. With two equal-sized elements in \( \Omega=[0,1] \) and \( f(x)=x(1-x) \), one gets $$ \begin{equation*} A = \frac{h}{6}\left(\begin{array}{ccc} 2 & 1 & 0\\ 1 & 4 & 1\\ 0 & 1 & 2 \end{array}\right),\quad b = \frac{h^2}{12}\left(\begin{array}{c} 2 - 3h\\ 12 - 14h\\ 10 -17h \end{array}\right)\tp \end{equation*} $$ The solution becomes $$ \begin{equation*} c_0 = \frac{h^2}{6},\quad c_1 = h - \frac{5}{6}h^2,\quad c_2 = 2h - \frac{23}{6}h^2\tp \end{equation*} $$ The resulting function $$ \begin{equation*} u(x)=c_0\basphi_0(x) + c_1\basphi_1(x) + c_2\basphi_2(x)\end{equation*} $$ is displayed in Figure 25 (left). Doubling the number of elements to four leads to the improved approximation in the right part of Figure 25.


Figure 25: Least squares approximation of a parabola using 2 (left) and 4 (right) P1 elements.

Assembly of elementwise computations

The integrals above are naturally split into integrals over individual elements since the formulas change with the elements. This idea of splitting the integral is fundamental in all practical implementations of the finite element method.

Let us split the integral over \( \Omega \) into a sum of contributions from each element: $$ \begin{equation} A_{i,j} = \int_\Omega\basphi_i\basphi_j \dx = \sum_{e} A^{(e)}_{i,j},\quad A^{(e)}_{i,j}=\int_{\Omega^{(e)}} \basphi_i\basphi_j \dx \tp \tag{27} \end{equation} $$ Now, \( A^{(e)}_{i,j}\neq 0 \) if and only if \( i \) and \( j \) are nodes in element \( e \) (look at Figure 23 to realize this property). Introduce \( i=q(e,r) \) as the mapping of local node number \( r \) in element \( e \) to the global node number \( i \). This is just a short mathematical notation for the expression i=elements[e][r] in a program. Let \( r \) and \( s \) be the local node numbers corresponding to the global node numbers \( i=q(e,r) \) and \( j=q(e,s) \). With \( d \) nodes per element, all the nonzero elements in \( A^{(e)}_{i,j} \) arise from the integrals involving basis functions with indices corresponding to the global node numbers in element number \( e \): $$ \begin{equation*} \int_{\Omega^{(e)}}\basphi_{q(e,r)}\basphi_{q(e,s)} \dx, \quad r,s=0,\ldots, d\tp \end{equation*} $$ These contributions can be collected in a \( (d+1)\times (d+1) \) matrix known as the element matrix. Let \( \Ifd=\{0,\ldots,d\} \) be the valid indices of \( r \) and \( s \). We introduce the notation $$ \begin{equation*} \tilde A^{(e)} = \{ \tilde A^{(e)}_{r,s}\},\quad r,s\in\Ifd, \end{equation*} $$ for the element matrix. For the case \( d=2 \) we have $$ \begin{equation*} \tilde A^{(e)} = \left\lbrack\begin{array}{lllll} \tilde A^{(e)}_{0,0} & \tilde A^{(e)}_{0,1} & \tilde A^{(e)}_{0,2}\\ \tilde A^{(e)}_{1,0} & \tilde A^{(e)}_{1,1} & \tilde A^{(e)}_{1,2}\\ \tilde A^{(e)}_{2,0} & \tilde A^{(e)}_{2,1} & \tilde A^{(e)}_{2,2} \end{array}\right\rbrack \tp \end{equation*} $$ Given the numbers \( \tilde A^{(e)}_{r,s} \), we should according to (27) add the contributions to the global coefficient matrix by $$ \begin{equation} A_{q(e,r),q(e,s)} := A_{q(e,r),q(e,s)} + \tilde A^{(e)}_{r,s},\quad r,s\in\Ifd\tp \end{equation} $$ This process of adding in elementwise contributions to the global matrix is called finite element assembly or simply assembly. Figure 26 illustrates how element matrices for elements with two nodes are added into the global matrix. More specifically, the figure shows how the element matrix associated with elements 1 and 2 assembled, assuming that global nodes are numbered from left to right in the domain. With regularly numbered P3 elements, where the element matrices have size \( 4\times 4 \), the assembly of elements 1 and 2 are sketched in Figure 27.


Figure 26: Illustration of matrix assembly: regularly numbered P1 elements.


Figure 27: Illustration of matrix assembly: regularly numbered P3 elements.

After assembly of element matrices corresponding to regularly numbered elements and nodes are understood, it is wise to study the assembly process for irregularly numbered elements and nodes. Figure 17 shows a mesh where the elements array, or \( q(e,r) \) mapping in mathematical notation, is given as

elements = [[2, 1], [4, 5], [0, 4], [3, 0], [5, 2]]

The associated assembly of element matrices 1 and 2 is sketched in Figure 28.

These three assembly processes can also be animated.


Figure 28: Illustration of matrix assembly: irregularly numbered P1 elements.

The right-hand side of the linear system is also computed elementwise: $$ \begin{equation} b_i = \int_\Omega f(x)\basphi_i(x) \dx = \sum_{e} b^{(e)}_{i},\quad b^{(e)}_{i}=\int_{\Omega^{(e)}} f(x)\basphi_i(x)\dx \tp \end{equation} $$ We observe that \( b_i^{(e)}\neq 0 \) if and only if global node \( i \) is a node in element \( e \) (look at Figure 24 to realize this property). With \( d \) nodes per element we can collect the \( d+1 \) nonzero contributions \( b_i^{(e)} \), for \( i=q(e,r) \), \( r\in\Ifd \), in an element vector $$ \begin{equation*} \tilde b_r^{(e)}=\{ \tilde b_r^{(e)}\},\quad r\in\Ifd\tp \end{equation*} $$ These contributions are added to the global right-hand side by an assembly process similar to that for the element matrices: $$ \begin{equation} b_{q(e,r)} := b_{q(e,r)} + \tilde b^{(e)}_{r},\quad r\in\Ifd\tp \end{equation} $$

Mapping to a reference element

Instead of computing the integrals $$ \begin{equation*} \tilde A^{(e)}_{r,s} = \int_{\Omega^{(e)}}\basphi_{q(e,r)}(x)\basphi_{q(e,s)}(x)\dx\end{equation*} $$ over some element \( \Omega^{(e)} = [x_L, x_R] \), it is convenient to map the element domain \( [x_L, x_R] \) to a standardized reference element domain \( [-1,1] \). (We have now introduced \( x_L \) and \( x_R \) as the left and right boundary points of an arbitrary element. With a natural, regular numbering of nodes and elements from left to right through the domain, we have \( x_L=\xno{e} \) and \( x_R=\xno{e+1} \) for P1 elements.)

Let \( X\in [-1,1] \) be the coordinate in the reference element. A linear or affine mapping from \( X \) to \( x \) reads $$ \begin{equation} x = \half (x_L + x_R) + \half (x_R - x_L)X\tp \tag{28} \end{equation} $$ This relation can alternatively be expressed by $$ \begin{equation} x = x_m + {\half}hX, \tag{29} \end{equation} $$ where we have introduced the element midpoint \( x_m=(x_L+x_R)/2 \) and the element length \( h=x_R-x_L \).

Integrating on the reference element is a matter of just changing the integration variable from \( x \) to \( X \). Let $$ \begin{equation} \refphi_r(X) = \basphi_{q(e,r)}(x(X)) \end{equation} $$ be the basis function associated with local node number \( r \) in the reference element. The integral transformation reads $$ \begin{equation} \tilde A^{(e)}_{r,s} = \int_{\Omega^{(e)}}\basphi_{q(e,r)}(x)\basphi_{q(e,s)}(x)\dx = \int_{-1}^1 \refphi_r(X)\refphi_s(X)\frac{dx}{dX}\dX \tp \end{equation} $$ The stretch factor \( dx/dX \) between the \( x \) and \( X \) coordinates becomes the determinant of the Jacobian matrix of the mapping between the coordinate systems in 2D and 3D. To obtain a uniform notation for 1D, 2D, and 3D problems we therefore replace \( dx/dX \) by \( \det J \) already now. In 1D, \( \det J = dx/dX = h/2 \). The integration over the reference element is then written as $$ \begin{equation} \tilde A^{(e)}_{r,s} = \int_{-1}^1 \refphi_r(X)\refphi_s(X)\det J\,dX \tag{30} \tp \end{equation} $$ The corresponding formula for the element vector entries becomes $$ \begin{equation} \tilde b^{(e)}_{r} = \int_{\Omega^{(e)}}f(x)\basphi_{q(e,r)}(x)dx = \int_{-1}^1 f(x(X))\refphi_r(X)\det J\,dX \tag{31} \tp \end{equation} $$

Since we from now on will work in the reference element, we need explicit mathematical formulas for the basis functions \( \basphi_i(x) \) in the reference element only, i.e., we only need to specify formulas for \( \refphi_r(X) \). This is a very convenient simplification compared to specifying piecewise polynomials in the physical domain.

The \( \refphi_r(x) \) functions are simply the Lagrange polynomials defined through the local nodes in the reference element. For \( d=1 \) and two nodes per element, we have the linear Lagrange polynomials $$ \begin{align} \refphi_0(X) &= \half (1 - X) \tag{32}\\ \refphi_1(X) &= \half (1 + X) \tag{33} \end{align} $$ Quadratic polynomials, \( d=2 \), have the formulas $$ \begin{align} \refphi_0(X) &= \half (X-1)X\\ \refphi_1(X) &= 1 - X^2\\ \refphi_2(X) &= \half (X+1)X \end{align} $$ In general, $$ \begin{equation} \refphi_r(X) = \prod_{s=0,s\neq r}^d \frac{X-\Xno{s}}{\Xno{r}-\Xno{s}}, \end{equation} $$ where \( \Xno{0},\ldots,\Xno{d} \) are the coordinates of the local nodes in the reference element. These are normally uniformly spaced: \( \Xno{r} = -1 + 2r/d \), \( r\in\Ifd \).

Why reference elements? The great advantage of using reference elements is that the formulas for the basis functions, \( \refphi_r(X) \), are the same for all elements and independent of the element geometry (length and location in the mesh). The geometric information is "factored out" in the simple mapping formula and the associated \( \det J \) quantity, but this information is (here taken as) the same for element types. Also, the integration domain is the same for all elements.

Example: Integration over a reference element

To illustrate the concepts from the previous section in a specific example, we now consider calculation of the element matrix and vector for a specific choice of \( d \) and \( f(x) \). A simple choice is \( d=1 \) (P1 elements) and \( f(x)=x(1-x) \) on \( \Omega =[0,1] \). We have the general expressions (30) and (31) for \( \tilde A^{(e)}_{r,s} \) and \( \tilde b^{(e)}_{r} \). Writing these out for the choices (32) and (33), and using that \( \det J = h/2 \), we can do the following calculations of the element matrix entries: $$ \begin{align} \tilde A^{(e)}_{0,0} &= \int_{-1}^1 \refphi_0(X)\refphi_0(X)\frac{h}{2} dX\nonumber\\ &=\int_{-1}^1 \half(1-X)\half(1-X) \frac{h}{2} dX = \frac{h}{8}\int_{-1}^1 (1-X)^2 dX = \frac{h}{3}, \tag{34}\\ \tilde A^{(e)}_{1,0} &= \int_{-1}^1 \refphi_1(X)\refphi_0(X)\frac{h}{2} dX\nonumber\\ &=\int_{-1}^1 \half(1+X)\half(1-X) \frac{h}{2} dX = \frac{h}{8}\int_{-1}^1 (1-X^2) dX = \frac{h}{6},\\ \tilde A^{(e)}_{0,1} &= \tilde A^{(e)}_{1,0}, \tag{35}\\ \tilde A^{(e)}_{1,1} &= \int_{-1}^1 \refphi_1(X)\refphi_1(X)\frac{h}{2} dX\nonumber\\ &=\int_{-1}^1 \half(1+X)\half(1+X) \frac{h}{2} dX = \frac{h}{8}\int_{-1}^1 (1+X)^2 dX = \frac{h}{3} \tag{36} \tp \end{align} $$

The corresponding entries in the element vector becomes $$ \begin{align} \tilde b^{(e)}_{0} &= \int_{-1}^1 f(x(X))\refphi_0(X)\frac{h}{2} dX\nonumber\\ &= \int_{-1}^1 (x_m + \half hX)(1-(x_m + \half hX)) \half(1-X)\frac{h}{2} dX \nonumber\\ &= - \frac{1}{24} h^{3} + \frac{1}{6} h^{2} x_{m} - \frac{1}{12} h^{2} - \half h x_{m}^{2} + \half h x_{m} \tag{37}\\ \tilde b^{(e)}_{1} &= \int_{-1}^1 f(x(X))\refphi_1(X)\frac{h}{2} dX\nonumber\\ &= \int_{-1}^1 (x_m + \half hX)(1-(x_m + \half hX)) \half(1+X)\frac{h}{2} dX \nonumber\\ &= - \frac{1}{24} h^{3} - \frac{1}{6} h^{2} x_{m} + \frac{1}{12} h^{2} - \half h x_{m}^{2} + \half h x_{m} \tp \end{align} $$ In the last two expressions we have used the element midpoint \( x_m \).

Integration of lower-degree polynomials above is tedious, and higher-degree polynomials involve very much more algebra, but sympy may help. For example, we can easily calculate (34), (34), and (37) by

>>> import sympy as sp
>>> x, x_m, h, X = sp.symbols('x x_m h X')
>>> sp.integrate(h/8*(1-X)**2, (X, -1, 1))
h/3
>>> sp.integrate(h/8*(1+X)*(1-X), (X, -1, 1))
h/6
>>> x = x_m + h/2*X
>>> b_0 = sp.integrate(h/4*x*(1-x)*(1-X), (X, -1, 1))
>>> print b_0
-h**3/24 + h**2*x_m/6 - h**2/12 - h*x_m**2/2 + h*x_m/2

For inclusion of formulas in documents (like the present one), sympy can print expressions in LaTeX format:

>>> print sp.latex(b_0, mode='plain')
- \frac{1}{24} h^{3} + \frac{1}{6} h^{2} x_{m}
- \frac{1}{12} h^{2} - \half h x_{m}^{2}
+ \half h x_{m}