Piecewise-linear hat function (source code)

= Piecewise-linear hat function
{title2=$\phi_i(x_j)=\delta_{ij}$}

= Hat function
{synonym}

= Chapeau function
{synonym}

A <piecewise-linear hat function> is a continuous <piecewise linear function> that selects one node of a <finite element mesh>: it is one there and zero at every other node. For an interior node $x_i$, its exact formula on a possibly nonuniform <mesh> is
$$
\phi_i(x)=\begin{cases}
(x-x_{i-1})/(x_i-x_{i-1}),&x_{i-1}\leq x\leq x_i,\\
(x_{i+1}-x)/(x_{i+1}-x_i),&x_i\leq x\leq x_{i+1},\\
0,&\text{otherwise}.
\end{cases}
$$
Its <support of a function> is $[x_{i-1},x_{i+1}]$. It rises on the left <interval> and falls on the right, producing its hat-shaped graph. The interior <hat functions> form a <basis> of the continuous $P_1$ <conforming finite element space> with homogeneous <Dirichlet boundary conditions>. Chen's author notes define this nodal <basis> and illustrate it in one and two dimensions.https://webapps.math.uci.edu/~chenlong/226/Ch2FEM.pdf[Chen, Introduction to Finite Element Methods, §3.1, pp. 5–6 and Figure 5: nodal hat basis]{ref} The nodal property $\phi_i(x_j)=\delta_{ij}$ makes the <coefficients> directly readable: if $v_h=\sum_i c_i\phi_i$, then $v_h(x_i)=c_i$. Unlike the global <Lagrange cardinal polynomials>, each of these functions is confined to two neighboring elements.

For example, with $x_j=j/4$, the middle <hat function> is $\phi_2(x)=4x-1$ on $[1/4,1/2]$, $3-4x$ on $[1/2,3/4]$, and zero elsewhere. Hence $\phi_2(3/8)=1/2$, while $\phi_1(3/8)=1/2$ and $\phi_3(3/8)=0$. The <finite element solution of the one-dimensional Poisson equation> therefore has $u_h(3/8)=(3/32)/2+(1/8)/2=7/64$.

Local assembly makes this locality computational. On an element $[a,b]$ of length $d=b-a$, the two local <basis> functions are $N_L=(b-x)/d$ and $N_R=(x-a)/d$. For the <Poisson equation> with unit forcing, their local <stiffness matrix> and load vector are
$$
K^{(e)}=\frac1d\begin{pmatrix}1&-1\\-1&1\end{pmatrix},\qquad
F^{(e)}=\frac d2\begin{pmatrix}1\\1\end{pmatrix}.
$$
Add these four matrix entries and two loads to the rows and columns belonging to the element's endpoints, then impose the prescribed boundary values. An interior node receives contributions from its two neighboring elements. Writing $d_L=x_i-x_{i-1}$ and $d_R=x_{i+1}-x_i$ gives
$$
K_{ii}=\frac1{d_L}+\frac1{d_R},\qquad
K_{i,i-1}=-\frac1{d_L},\qquad K_{i,i+1}=-\frac1{d_R},\qquad
F_i=\frac{d_L+d_R}{2},
$$
with off-diagonal entries included only for interior neighbors. On a uniform <mesh> these reduce to $2/h$, $-1/h$ and $h$. Nonadjacent <hat functions> have no derivative overlap of positive length, so all distant entries vanish. This produces a <sparse matrix> with only three diagonals: the global coupling is built entirely from local two-node interactions.

\Image[/finite-element-hat-assembly.png]
{title=Three interior hat functions on a four-interval mesh, with local stiffness and load contributions assembled into the three-unknown Poisson system}