Fundamentals / Numerical mathematics / Numerical partial differential equations

Weak forms and finite elements

Derive a variational statement by integration by parts, assemble element matrices for a one-dimensional rod and explain what the Galerkin solution minimises.

From first principles

Obtain the weak diffusion problem by moving one derivative

For a stationary diffusion problem \(-\nabla\cdot(k\nabla u)=f\), suppose temperature is prescribed on part of the boundary and the outward flux \(q_n=-k\nabla u\cdot\mathbf n\) on the remainder. A classical second derivative may not exist across a material interface. Instead multiply the conservative equation by a smooth test function \(v\) that vanishes where \(u\) is prescribed. The divergence theorem moves one derivative from the flux onto \(v\):

\[\int_\Omega k\nabla u\cdot\nabla v\,d\Omega=\int_\Omega fv\,d\Omega-\int_{\Gamma_N}vq_n\,d\Gamma .\]

Approximate \(u\) with continuous element shape functions \(N_j\) that are differentiable inside each element. Choose each allowable shape function as the test function. Linearity then yields element contributions

\[K_{ij}^{(e)}=\int_e k\nabla N_i\cdot\nabla N_j\,d\Omega,\]
\[f_i^{(e)}=\int_e fN_i\,d\Omega .\]

Neighbouring elements contribute to the same global unknown at a shared node. Adding their contributions enforces weak equilibrium. The flux sign in the boundary term follows from an outward-positive definition; switching to prescribed inward heat supply reverses that sign in the load vector.

Background · Weak form
\[\int k\nabla u\cdot\nabla v\,dV=\int fv\,dV+\text{boundary terms}\]

Integration by parts lowers the derivative requirement. Essential and natural boundary data enter the discrete system differently.

Derivation and applications

How a Robin boundary enters both the stiffness matrix and the load

A convecting boundary couples an unknown surface temperature to an ambient temperature. Unlike a prescribed heat source it contributes to both sides of the assembled equations. Consider steady diffusion \(-\nabla\cdot(k\nabla T)=s\) in a two-dimensional region of unit thickness, with \(k>0\). On a straight boundary segment impose the empirical film law \(-k\nabla T\cdot\mathbf n=h(T-T_\infty)\) using the outward normal. Multiply the field equation by a test function \(v\) vanishing on prescribed-temperature boundaries, integrate, and apply the divergence theorem:

\[\int_\Omega k\nabla v\cdot\nabla T\,dA-\int_{\partial\Omega}v k\nabla T\cdot\mathbf n\,ds=\int_\Omega vs\,dA .\]

Substitution on the film boundary \(\Gamma\) places the unknown surface term on the left and the ambient contribution on the right:

\[\int_\Omega k\nabla v\cdot\nabla T\,dA+\int_\Gamma hvT\,ds=\int_\Omega vs\,dA+\int_\Gamma hvT_\infty\,ds\]

with any other natural boundary terms added separately. To assemble one linear edge of length \(\ell\), use edge functions \(N_1=1-\xi\), \(N_2=\xi\), \(\xi\in[0,1]\). Insert the nodal interpolation for \(T\) and choose each \(N_i\) as test function. Constant \(h,T_\infty\) give

\[K_\Gamma=\frac{h\ell}{6}\begin{pmatrix}2&1\\1&2\end{pmatrix},\qquad f_\Gamma=\frac{h\ell T_\infty}{2}\begin{pmatrix}1\\1\end{pmatrix}.\]

The off-diagonal term is present because both nodes influence temperature across the same edge. In the limit \(h=0\) the boundary is insulated. A very large film coefficient constrains the temperature toward ambient but can introduce a large stiffness contrast.

Worked example

Assemble a convecting edge by hand

Use a one-metre out-of-plane thickness, \(\ell=0.300\) m, \(h=20.0\) W/(m² K) and \(T_\infty=290\) K. Then \(K_\Gamma\) has diagonals \(2.00\) W/K and off-diagonals \(1.00\) W/K. The load is \((870,870)\) W. At nodal temperatures \(300\) and \(310\) K the film residual \(K_\Gamma T-f_\Gamma\) is \((40,50)\) W. The sum \(90\) W agrees with \(h\ell[(300+310)/2-290]\).

Second application

Test a constant solution before using the boundary routine

Set both temperatures to \(290\) K, the ambient value. Each row has sum \(3.00\) W/K, so every residual is \(3(290)-870=0\). If the code includes the ambient load but forgets the matrix, it predicts \(1740\) W of heating at thermal equilibrium. This constant-field patch test detects sign errors and omission of the surface matrix independently of the volume mesh.

Reduce the differentiability required by equilibrium

A rod is fixed at \(x=0\), pushed by a distributed load \(f\) and by an end force \(t\) at \(x=L\). Axial displacement \(u\) satisfies \(-(EAu')'=f\), \(u(0)=0\) and \(EAu'(L)=t\). The product \(EA\) is positive. A trial profile assembled from linear pieces has jumps in its derivative, so differentiating it twice pointwise is unsuitable. Instead multiply the strong equation by a test displacement \(v\) vanishing at the fixed end and integrate by parts once.

The resulting equation is

\[\int_0^L EA\,u'v'\,dx=\int_0^L fv\,dx+t v(L).\]

Only first derivatives are required. The fixed displacement is imposed directly on trial and test spaces; the end traction appears in the load functional. This clarifies why prescribing both force and displacement independently at the same end can overconstrain a second-order problem. The test functions represent allowable virtual displacements, and the integral identity expresses equality of internal and external virtual work.

Background · Why can the derivative jump while the displacement stays continuous?

At a bonded interface with no concentrated force, displacement and axial force are continuous. If \(EA\) jumps, the strain \(u'\) can jump inversely even though the product \(EAu'\) is continuous. For a continuous piecewise linear displacement, the weak first derivative is the ordinary piecewise constant slope. The squared-gradient energy is therefore finite.

A displacement jump is different. Replacing a jump of size \(d\) by a straight ramp over width \(\delta\) costs strain energy \(EA d^2/(2\delta)\) for constant positive stiffness, which diverges as the ramp shrinks. A standard conforming displacement space encodes this distinction. If the mechanics really allow slip, the interface needs its own model and a space which permits the two traces to differ.

Assemble one element and connect it to its neighbour

On an element from \(x_i\) to \(x_{i+1}\) of length \(h\), use shape functions \(N_1=1-s,\ N_2=s\) with \(s=(x-x_i)/h\). Their physical derivatives are \(-1/h,\ 1/h\). For constant \(EA\), integrating products \(EA N'_aN'_b\) gives the matrix \((EA/h)\left(\begin{smallmatrix}1&-1\\-1&1\end{smallmatrix}\right)\). A constant distributed load contributes \(fh(1,1)^T/2\). Summing element contributions for a shared global node enforces balance there without prescribing the internal traction manually.

Take \(L=1,\ EA=1,\ f=2,\ t=0\) in consistent units and use two equal elements. After eliminating the fixed displacement, the stiffness matrix is \(\left(\begin{smallmatrix}4&-2\\-2&2\end{smallmatrix}\right)\) and the load is \((1,0.5)^T\). Solving gives

\[u(0.5)=0.75,\ u(1)=1.\]

The exact solution is \(2x-x^2\) and agrees at both nodes. Inside the first element at \(x=0.25\), the linear interpolant gives 0.375, whereas the exact value is 0.4375. Nodal agreement can coexist with a substantial interior error.

Assemble a physical triangle from a reference element

A line element has one length scale. A triangle can stretch, rotate and shear, so the element area alone cannot determine its diffusion matrix. Start with the reference vertices \((0,0),(1,0),(0,1)\) and the linear shape functions \(1-\xi-\eta,\xi,\eta\). Send the reference triangle to a physical cell with vertices \(P_0,P_1,P_2\) by \(x=P_0+J(\xi,\eta)^T\), where the columns of \(J\) are \(P_1-P_0\) and \(P_2-P_0\). For a nondegenerate triangle, the chain rule and area transformation give

\[\nabla_x N_i=J^{-T}\nabla_{\xi,\eta}\widehat N_i,\]
\[dx\,dy=|\det J|\,d\xi\,d\eta .\]

To see why the inverse transpose occurs, take an arbitrary displacement \(d\widehat x\). Both gradients must describe the same change in a shape function. Thus \((\nabla_xN_i)^TJ\,d\widehat x=(\nabla\widehat N_i)^Td\widehat x\) for every such displacement. For unit isotropic conductivity, the transformed gradients are constant and the physical stiffness entries reduce to

\[K_{ij}=|T|\nabla N_i\cdot\nabla N_j,\]

with \(|T|=|\det J|/2\). A constant cell load \(f\) contributes \(f|T|/3\) at each vertex because each linear shape function has mean \(1/3\).

A sheared cell and an exact affine-field check

Take \(P_0=(0,0), P_1=(2,0), P_2=(1,1)\). The columns of \(J\) are \((2,0)\) and \((1,1)\), so the cell area is \(1\). Applying \(J^{-T}\) to the three reference gradients gives \((-1/2,-1/2),(1/2,-1/2),(0,1)\). Their dot products and the load \(f=3\) yield

\[K=\begin{pmatrix}1/2&0&-1/2\\0&1/2&-1/2\\-1/2&-1/2&1\end{pmatrix},\qquad b=(1,1,1)^T.\]

Every row sums to zero because a constant field has zero gradient. For a more discriminating test use \(u=x+2y\), with vertex values \(U=(0,2,3)^T\). Weighting the three gradients by these values gives \((1,2)\). The physical integral of its squared gradient equals \(1(1^2+2^2)=5\), and direct multiplication gives \(U^TKU=5\). A transposed mapping error can pass the constant test and fail this second one.

Practise on sheet 1 · Two triangles of the same area. Further reading · Heidelberg University · Lecture Notes on Scientific Computing with Partial Differential Equations · §7.4 Pₖ Finite Elements and §7.6 Construction of the Finite Element Stiffness Matrix.

Identify the energy that is best approximated

Let \(a(u,v)=\int EA\,u'v'\) and denote the external work by \(\ell(v)\). The discrete solution \(u_h\) satisfies \(a(u_h,v_h)=\ell(v_h)\) for every test function from its finite-dimensional space. The continuous solution satisfies the same equation for every such test, so \(a(u-u_h,v_h)=0\). This Galerkin orthogonality means the error is perpendicular to the trial space in the energy inner product. For any other admissible discrete \(w_h\), write \(u-w_h=(u-u_h)+(u_h-w_h)\) and expand its squared energy norm. The cross term vanishes and \(\|u-w_h\|_a^2=\|u-u_h\|_a^2+\|u_h-w_h\|_a^2\).

Thus the conforming Galerkin solution is the best energy approximation in its space for this symmetric linear problem and exact integration. The statement concerns a specific norm and model. It does not guarantee pointwise optimal stresses or carry unchanged into nonsymmetric transport problems. In the rod example the computed strain is constant on each element, replacing a linearly varying exact strain. Refinement improves this derivative field even when a table of nodal displacements already looks exact.

Recover a support reaction

The reaction at a prescribed-displacement node is hidden if one inspects only the reduced interior system. Assemble all element internal forces \(Ku_h\) before discarding constrained rows, then subtract the full external load vector. The entries at free degrees of freedom should be near zero; the entry at a prescribed degree of freedom gives the reaction in the chosen sign convention.

In the uniformly loaded rod, the first element strain is

\[(0.75-0)/0.5=1.5.\]

Its internal nodal force at the left is \(-1.5\), while the first node receives an external distributed-load share of \(+0.5\). Their difference \(-2\) is the support reaction. It balances the total applied load \(2\cdot1=2\). Looking only at the derivative in the first element would have reported magnitude 1.5, missing that load share. A reaction from assembled balance and a stress recovered by pointwise differentiation are related but distinct quantities on a finite mesh.

Obtain actual norm errors from the Galerkin property

For \(-u''=f\) on the unit interval with homogeneous Dirichlet values, the elementary energy inner product is \(a(u,v)=\int u'v'\). Let \(I_hu\) be the continuous piecewise-linear interpolant of a sufficiently smooth exact solution at mesh nodes. On each element its slope is the average of \(u'\). Every linear finite-element test function \(v_h\) has constant derivative on that element. Consequently

\[\int_E(u-I_hu)'v_h'=v_h'[u-I_hu]_{\partial E}=0.\]

Summing proves global energy orthogonality.

The Galerkin solution satisfies the same orthogonality, and positive definiteness gives uniqueness. Thus \(u_h=I_hu\) in this one-dimensional, constant-coefficient problem with exactly integrated load and matching boundary values. Nodal errors vanish, although the field and derivative between nodes can still be inaccurate. Variable coefficients inside an element generally destroy the simple telescoping argument because the factor multiplying \(v_h'\) is no longer constant. The result also should not be transferred to multidimensional triangulations without proof.

Compare nodal, field and flux accuracy for one solution

Choose \(u=x(1-x)\), so \(f=2\). On an element starting at \(a\) write \(s=x-a\in[0,h]\). Subtract the straight interpolant from the parabola to get \(u-u_h=s(h-s)\), vanishing at both nodes and attaining \(h^2/4\) at the element centre. Its derivative is \(h-2s\); the exact and numerical slopes agree at the midpoint but differ by magnitude \(h\) at each endpoint taken from that element. Testing only the midpoint slope or the nodal values would entirely conceal these other discrepancies.

Compute the squared energy error on this element:

\[\int_0^h(h-2s)^2ds=h^3/3.\]

With \(1/h\) equal elements, the global energy error is \(h/\sqrt3\). Similarly the element squared field error is \(\int_0^hs^2(h-s)^2ds=h^5/30\), giving global \(L^2\) error \(h^2/\sqrt{30}\). For \(h=1/4\), the two errors are approximately 0.14434 and 0.01141; the maximum field error is 0.015625. Halving the mesh width halves the energy error but divides the field error by four. All these measurements concern the very same numerical solution whose nodal error is exactly zero.

For a physical diffusion flux with constant conductivity one, flux error equals minus derivative error. A cell-averaged flux test is also special here: the numerical slope equals the exact average slope on each element for the same reason used in the proof. To verify local flux resolution, compare at more than a midpoint or a cell average and retain the measure used for convergence. A reported order is a statement about an observable and a norm as well as a finite-element space.

These formulas also identify a misleading manufactured test: if an automated comparison stores only nodal differences, the estimated order is undefined because both coarse and fine errors vanish. Adding independently evaluated quadrature points inside elements exposes the field error immediately. To check a flux implementation, use a quadrature rule capable of resolving the squared linear derivative error; a one-point midpoint rule would again report zero despite the nonzero exact energy norm.

Distribute a moving point source in a weak form

A one-dimensional Poisson problem has a concentrated source \(Q\delta(x-a)\). Its weak load against a linear hat function is \(Q\phi_i(a)\), so the two adjacent degrees of freedom share it in proportion to their shape functions.

Distribute a moving point source in a weak form. Horizontal axis: Source position within one element. Vertical axis: Nodal load / total point load. Data follow the figure.
At 30% of the element length the split is 70% left, 30% right. The loads sum to Q and reproduce the correct first moment. Placing the whole source at the nearest node preserves total load but jumps abruptly at mid-element.
Read values from the plot
Nodal load / total point load at selected values of Source position within one element.
Source position within one elementLeft basis functionRight basis function
010
0.250.750.25
0.50.50.5
0.750.250.75
101

Download all 241 plotted samples (CSV)

Change parameters · Distribute a moving point source in a weak form

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.

Study the full derivation

Bound diffusion error by balancing a reconstructed flux · Spatial discretisation and field equations

A temperature field from a finite-element calculation looks smooth. Reconstruct a heat flux which exactly balances the source, then derive an upper bound for the true energy error without knowing the exact temperature.