Flow Lab

Syllabus / Module 10: Numerical Methods for PDEs

Lesson 10.13

Introduction to Finite Elements

Weak forms, basis 'hat' functions, assembling a stiffness matrix, 1D and 2D triangular elements, and when to choose FEM.

Written byClaude Sonnet

🌊
Why this matters for simulation: Structural analysis of an airframe (and some CFD) uses finite elements.

By the end of this lesson you can…

  • Derive the weak form of $-(au')' = f$ by integration by parts, and say which boundary conditions are essential and which natural.
  • Write the Galerkin system $K\mathbf{u} = \mathbf{b}$ with hat functions, and show that on a uniform mesh $K$ is $h$ times the finite difference matrix.
  • Assemble stiffness and load element by element on a nonuniform mesh and solve a small system by hand.
  • Prove and use nodal exactness: for $-u'' = f$ in one dimension the linear-element solution equals the exact solution at every node.
  • Read measured convergence orders ($1$ in $H^1$, $2$ in $L^2$) and say what a kink inside an element does to them.
  • Compute a triangle's element matrix and choose between FEM, finite differences and finite volumes.
On this page
·
  1. From the strong form to the weak form
  2. Galerkin: choosing the pieces
  3. Stiffness, mass and load
  4. Assembly, element by element
  5. Nodal exactness in one dimension
  6. The lab
  7. How fast does it converge?
  8. Rough problems: what the weak form buys
  9. Triangles in two dimensions
  10. FEM, finite differences and finite volumes
  11. The exercise: assembling and solving
  12. Concept checks
  13. Problems
  14. What comes next
  15. Key Takeaways

10.12 Spectral Methods & the FFT ended with a list of things its transform trick cannot do: odd geometry, jumping coefficients, rough solutions. The finite element method (FEM) is built for exactly those. Finite differences ask the equation to hold at grid points; FEM asks it to hold on average against a family of test functions, which allows a mesh of any shape and coefficients as rough as the physics. It is how an airframe is simulated, and the matrix we assemble is the kind of object 10.9 Multigrid learned to solve fast. We work in one dimension first, where everything can be checked by hand, then look at triangles.

▶ Video From the equation to its weak form, hat functions glued into a solution, a stiffness matrix assembled element by element, and the error shrinking as the mesh is refinedcaptions only
Transcript
  1. Take minus the derivative of a times u prime, equal to f, with u zero at both ends. Multiply by any test function v that vanishes at the ends, and integrate. Integration by parts moves one derivative onto v, and the boundary term disappears. What is left is the weak form: the integral of a u prime v prime equals the integral of f v, for every such v. It needs only one derivative of each function, so corners are allowed, and u minimizes an energy. A Dirichlet value is built into the space: essential. A flux condition appears as a boundary term, g times v at 1: natural.
  2. Galerkin's method searches a finite space, here continuous piecewise linear functions, and tests against the same space. Its basis is one hat function per interior node: height 1 at its own node, 0 at every other, so the unknowns are simply nodal values. Testing with each hat gives K u equals b, where K i j is the integral of a, phi i prime, phi j prime, and b i is the integral of f phi i. On a uniform mesh the diagonal is 2 over h and the neighbors are minus 1 over h: h times the finite difference matrix. Check an eigenvalue. With n equal to 10, the smallest is 9.951 for elements, 9.789 for differences, and the exact value is 9.870.
  3. Assembly visits each element once. An element of length h contributes a over h times the matrix 1, minus 1, minus 1, 1,. Take nodes 0, 0.25, 0.5 and 1, with a and f equal to 1. The three element lengths are 0.25, 0.25 and 0.5, so the scales are 4, 4 and 2. Node 1 touches elements 1 and 2, node 2 touches elements 2 and 3, so K is 8, minus 4, minus 4, 6. The load is 0.25 and 0.375. The determinant is 32, and the solution is 0.09375 and 0.125. The exact answer, x times 1 minus x, over 2, gives the same values at 0.25 and 0.5, to every digit, on an uneven mesh.
  4. Why? Let e be the exact solution minus its piecewise linear interpolant. It is zero at every node, and trial functions have constant slope on each element, so the integral of e prime times v prime is zero. The interpolant satisfies the Galerkin equations, so the finite element solution is exact at every node, on any mesh. For minus u double prime equals 1 at n equal to 8, the nodal error is 1.4 times 10 to the minus 17, uniform or graded. Load with nodal values h f of x i instead, and the error jumps to 1.3 times 10 to the minus 2, falling by 4.0 per doubling: the finite difference answer.
  5. How fast does the whole function converge? For sine pi x, a doubling of n divides the L2 error by 4, and the slope error, the H1 error, by 2. From n equal to 8 to 64, the L2 error falls from 9.92 times 10 to the minus 3 to 1.56 times 10 to the minus 4, order 2.00. The H1 error falls from 0.251 to 0.0315, order 1.00.
  6. Rough problems are where the weak form pays. A point load is simply b i equals phi i of c, and the nodal values are still exact. But with c equal to 0.4, inside an element, a straight line cannot follow the kink. From n equal to 16 to 64 the L2 error falls only from 2.17 times 10 to the minus 3 to 2.71 times 10 to the minus 4, order 1.5, and the H1 error from 0.1225 to 0.0612, order 0.5. A jumping coefficient, 1 then 10, does the same. With the interface at 0.5, a node, the nodal error is 2 times 10 to the minus 17. Move it to 0.4, inside an element, and the nodal error is 1.9 times 10 to the minus 3, and H1 falls only from 1.96 times 10 to the minus 2 to 1.03 times 10 to the minus 2. Align the mesh with material interfaces.
  7. In two dimensions an interval becomes a triangle. On a triangle of area A the element matrix is b i b j plus c i c j, over 4 A. Split a square grid by diagonals and assemble: an interior row has 4 on the diagonal and minus 1 for the four axis neighbors, the five-point stencil. On a square grid the method reproduces finite differences; on arbitrary triangles it generalizes them. Choose finite differences for simple geometry, finite volumes when conservation and shocks matter, and finite elements when geometry or coefficients are complicated. Next, Module 14 meets the same objects, mass and stiffness matrices for masses on springs, where K u equals lambda M u gives the normal modes.

From the strong form to the weak form

Take the model problem of 8.1 Two-Point Boundary Value Problems, with a variable coefficient a(x)>0a(x) \gt 0 (a conductivity, a stiffness, a viscosity):

−(a u′)′=fon (0,1),u(0)=u(1)=0.-(a\,u')' = f \quad \text{on } (0,1), \qquad u(0) = u(1) = 0 .

This strong form asks for two derivatives of uu. Multiply by any smooth test function vv with v(0)=v(1)=0v(0) = v(1) = 0 and integrate; integration by parts moves one derivative onto vv:

∫01a u′ v′ dx=∫01f v dxfor every such v.\int_0^1 a\,u'\,v'\,dx = \int_0^1 f\,v\,dx \qquad \text{for every such } v .

The boundary term −au′v ∣01-a u' v\,\big|_0^1 vanished because v=0v = 0 at both ends. This is the weak form. It needs one derivative of each function, so a continuous function with corners is allowed, yet for smooth uu it implies the strong form. It is also the stationarity condition of the energy

J(v)=12∫01a v′2 dx−∫01f v dx.J(v) = \tfrac12\int_0^1 a\,v'^2\,dx - \int_0^1 f\,v\,dx .

Perturbing, J(u+w)=J(u)+12∫a w′2 dxJ(u + w) = J(u) + \tfrac12\int a\,w'^2\,dx for admissible ww (the cross term is the weak form), so uu is the unique minimizer of JJ. That is why the method’s error will be a best-approximation error.

Essential and natural conditions. The Dirichlet condition u(0)=0u(0) = 0 is imposed by allowing only functions that satisfy it: it is essential. If instead the right end has au′(1)=ga u'(1) = g, the boundary term no longer vanishes and becomes g v(1)g\,v(1) on the right side; the minimizer satisfies the Neumann condition automatically: it is natural. In two and three dimensions the same step is Green’s first identity, from the divergence theorem of 2.10 Green, Stokes & the Divergence Theorem: ∫∇u⋅∇v dA=∫fv dA+∮v ∂nu ds\int \nabla u\cdot\nabla v\,dA = \int f v\,dA + \oint v\,\partial_n u\,ds.

Galerkin: choosing the pieces

Galerkin’s method picks a finite-dimensional space VhV_h, searches for uhu_h in it, and tests against the same space: find uh∈Vhu_h \in V_h with ∫auh′vh′=∫fvh\int a u_h' v_h' = \int f v_h for all vh∈Vhv_h \in V_h. The space used here is the continuous, piecewise-linear functions on a mesh 0=x0<x1<⋯<xn=10 = x_0 \lt x_1 \lt \dots \lt x_n = 1, zero at the ends. Its basis has one hat function per interior node:

ϕi(x)={(x−xi−1)/hi−1,xi−1≤x≤xi,(xi+1−x)/hi,xi≤x≤xi+1,0otherwise,hi=xi+1−xi.\phi_i(x) = \begin{cases} (x - x_{i-1})/h_{i-1}, & x_{i-1} \le x \le x_i,\\ (x_{i+1} - x)/h_i, & x_i \le x \le x_{i+1},\\ 0 & \text{otherwise,}\end{cases} \qquad h_i = x_{i+1} - x_i .

Its height is 11 at xix_i and 00 at every other node, so ϕi(xj)=δij\phi_i(x_j) = \delta_{ij}. Hence uh=∑jujϕju_h = \sum_j u_j\phi_j has uh(xj)=uju_h(x_j) = u_j: the unknowns are nodal values and the solution is the straight-line interpolation between them. Testing with vh=ϕiv_h = \phi_i gives

∑jKij uj=bi,Kij=∫01a ϕi′ϕj′ dx,bi=∫01f ϕi dx.\sum_j K_{ij}\,u_j = b_i, \qquad K_{ij} = \int_0^1 a\,\phi_i'\phi_j'\,dx, \qquad b_i = \int_0^1 f\,\phi_i\,dx .

KK is the stiffness matrix. It is symmetric, positive definite (a function with zero derivative and zero end values is zero), and tridiagonal, because hats overlap only with their neighbors. 10.8 Elliptic Solvers: Jacobi, Gauss–Seidel & SOR and multigrid solve such systems.

Stiffness, mass and load

On a uniform mesh ϕi′=±1/h\phi_i' = \pm1/h on the two elements under the hat. With a=1a = 1: Kii=1/h+1/h=2/hK_{ii} = 1/h + 1/h = 2/h, and Ki,i±1=−1/hK_{i,i\pm1} = -1/h, so

K=1h(2−1−12−1⋱⋱⋱−12).K = \frac1h\begin{pmatrix} 2 & -1 & & \\ -1 & 2 & -1 & \\ & \ddots & \ddots & \ddots \\ & & -1 & 2\end{pmatrix}.

This is hh times the finite difference matrix of 10.1 Finite Differences: Discretizing Derivatives. For smooth ff the load is bi=∫fϕi≈h f(xi)b_i = \int f\phi_i \approx h\,f(x_i), since a hat has area hh. Divide row ii by hh and you get (−ui−1+2ui−ui+1)/h2=f(xi)(-u_{i-1} + 2u_i - u_{i+1})/h^2 = f(x_i): the same equations, derived without a Taylor series. They differ only in how the right side is formed, and the lab shows that this is what separates them.

The same construction gives the mass matrix Mij=∫ϕiϕj dxM_{ij} = \int \phi_i\phi_j\,dx. On a uniform mesh ∫ϕi2=2h/3\int\phi_i^2 = 2h/3 and ∫ϕiϕi+1=h/6\int\phi_i\phi_{i+1} = h/6, so M=h6 tridiag(1,4,1)M = \tfrac h6\,\mathrm{tridiag}(1, 4, 1). It appears wherever there is no derivative: the heat equation becomes Mu˙+Ku=bM\dot{\mathbf u} + K\mathbf u = \mathbf b, and vibration the eigenproblem Ku=λMuK\mathbf u = \lambda M\mathbf u. Replacing MM by the diagonal of its row sums (hi−1+hi)/2(h_{i-1} + h_i)/2, mass lumping, gives back finite differences.

A check on eigenvalues (first playground below). For −u′′=λu-u'' = \lambda u on (0,1)(0,1) the smallest eigenvalue is π2=9.870\pi^2 = 9.870. With n=10n = 10 elements the generalized problem gives 9.9519.951 (FEM) and finite differences 9.7899.789. In closed form, with θ=πh\theta = \pi h,

λFEM=6h2 1−cos⁡θ2+cos⁡θ,λFD=2h2 (1−cos⁡θ),\lambda_{\rm FEM} = \frac{6}{h^2}\,\frac{1 - \cos\theta}{2 + \cos\theta}, \qquad \lambda_{\rm FD} = \frac{2}{h^2}\,(1 - \cos\theta),

with relative errors +θ2/12+\theta^2/12 and −θ2/12-\theta^2/12 at leading order (0.00820.0082 here). Finite element eigenvalues lie above the exact ones, finite difference ones below (the upper bound is the Rayleigh–Ritz principle, from memory).

Assembly, element by element

The standard loop visits each element once, builds a small matrix from its own geometry, and adds it into the global matrix at the element’s node numbers. For an element [xe,xe+1][x_e, x_{e+1}] of length heh_e with mean coefficient aˉe=1he∫a dx\bar a_e = \frac1{h_e}\int a\,dx,

Ke=aˉehe(1−1−11),be≈he2(ff).K^{e} = \frac{\bar a_e}{h_e}\begin{pmatrix} 1 & -1 \\ -1 & 1\end{pmatrix}, \qquad \mathbf b^{e} \approx \frac{h_e}{2}\begin{pmatrix} f \\ f\end{pmatrix} .

A shared node receives a contribution from each element (that is how 2/h2/h arises); a nonuniform mesh needs no special treatment.

Worked example: three elements, nodes 0, 0.25, 0.5, 10,\ 0.25,\ 0.5,\ 1, with a=1a = 1 and f=1f = 1. The lengths are h=0.25,0.25,0.5h = 0.25, 0.25, 0.5, so the element matrices have scale 4,4,24, 4, 2. The interior unknowns are u1u_1 at 0.250.25 and u2u_2 at 0.50.5. Node 11 touches elements 11 and 22; node 22 touches elements 22 and 33:

K=(4+4−4−44+2)=(8−4−46),b=((0.25+0.25)/2(0.25+0.5)/2)=(0.250.375).K = \begin{pmatrix} 4 + 4 & -4 \\ -4 & 4 + 2\end{pmatrix} = \begin{pmatrix} 8 & -4 \\ -4 & 6\end{pmatrix}, \qquad \mathbf b = \begin{pmatrix} (0.25 + 0.25)/2 \\ (0.25 + 0.5)/2\end{pmatrix} = \begin{pmatrix} 0.25 \\ 0.375\end{pmatrix}.

The determinant is 48−16=3248 - 16 = 32, so u1=(6⋅0.25+4⋅0.375)/32=3/32=0.09375u_1 = (6\cdot0.25 + 4\cdot0.375)/32 = 3/32 = 0.09375 and u2=(8⋅0.375+4⋅0.25)/32=0.125u_2 = (8\cdot0.375 + 4\cdot0.25)/32 = 0.125. The exact solution of −u′′=1-u'' = 1 is u=x(1−x)/2u = x(1-x)/2, giving u(0.25)=0.09375u(0.25) = 0.09375 and u(0.5)=0.125u(0.5) = 0.125. They agree to all digits, on a mesh with one coarse element and two fine ones. That is no accident.

Nodal exactness in one dimension

Let uu be the exact solution of −u′′=f-u'' = f (a=1a = 1) and IhuI_hu its piecewise-linear interpolant. The difference e=u−Ihue = u - I_hu is zero at every node. For any vh∈Vhv_h \in V_h, vh′v_h' is a constant cec_e on element ee, so

∫01e′ vh′ dx=∑ece∫xexe+1e′ dx=∑ece (e(xe+1)−e(xe))=0.\int_0^1 e'\,v_h'\,dx = \sum_e c_e\int_{x_e}^{x_{e+1}} e'\,dx = \sum_e c_e\,\bigl(e(x_{e+1}) - e(x_e)\bigr) = 0 .

Then ∫(Ihu)′vh′=∫u′vh′=∫fvh\int (I_hu)'v_h' = \int u'v_h' = \int f v_h, so IhuI_hu satisfies the Galerkin equations, and since they have one solution, uh=Ihuu_h = I_hu: the finite element solution is the exact solution at every node, on any mesh, for any ff. It used two things: aa constant on each element, and exact load integrals. The error between the nodes is then only the error of straight-line interpolation, at most h2/8h^2/8 times the largest ∣u′′∣|u''| (standard, from memory). In two dimensions no such miracle exists, so nodal values are merely good.

The lab

◆ Interactive Drag nodes, change the problem, and watch the error; then see the convergence

Loading simulation…

Drag the interior nodes of −u′′=1-u'' = 1 anywhere. The nodal error stays at round-off: 1.4×10−171.4\times10^{-17} for n=8n = 8 on the uniform mesh and on the mesh graded as xj=(j/n)2x_j = (j/n)^2. For −u′′=π2sin⁡πx-u'' = \pi^2\sin\pi x with exact load integrals it is 3×10−163\times10^{-16} at n=8n = 8 and 9×10−159\times10^{-15} at n=64n = 64. Set the load vector to nodal values hf(xi)h f(x_i). The nodal error jumps to 1.3×10−21.3\times10^{-2} at n=8n = 8 and falls by a factor 4.04.0 per doubling: 3.2×10−33.2\times10^{-3}, 8.0×10−48.0\times10^{-4}, 2.0×10−42.0\times10^{-4} at n=16,32,64n = 16, 32, 64. That is the finite difference answer. For the whole function, a mesh helps only where it is fine where the solution bends: on sin⁡πx\sin\pi x at n=8n = 8 the graded mesh has L2L^2 error 0.02090.0209 against 0.00990.0099 uniform, being coarse near x=1x = 1.

How fast does it converge?

Galerkin orthogonality, ∫a(u−uh)′vh′=0\int a(u - u_h)'v_h' = 0, says uhu_h is the energy-closest function in VhV_h to uu, so the error in (∫a e′2)1/2\bigl(\int a\,e'^2\bigr)^{1/2} is no larger than that of any other piecewise-linear function, in particular the interpolant. So for smooth uu:

  • the H1H^1 (slope) error ∥u′−uh′∥L2\lVert u' - u_h'\rVert_{L^2} is first order, O(h)O(h);
  • the L2L^2 error ∥u−uh∥L2\lVert u - u_h\rVert_{L^2} is second order, O(h2)O(h^2). Its proof (the Aubin–Nitsche duality argument) is omitted, but the numbers confirm it.

For u=sin⁡πxu = \sin\pi x the sim gives:

nn8163264
L2L^2 error9.92×10−39.92\times10^{-3}2.49×10−32.49\times10^{-3}6.22×10−46.22\times10^{-4}1.56×10−41.56\times10^{-4}
H1H^1 error0.2510.2510.1260.1260.06300.06300.03150.0315

Each doubling divides the L2L^2 error by 44 (order 2.002.00) and the H1H^1 error by 22 (order 1.001.00). For −u′′=1-u'' = 1 the errors are explicit: u−Ihu=s(h−s)/2u - I_hu = s(h-s)/2 on each element, so ∥e∥L2=h2/120\lVert e\rVert_{L^2} = h^2/\sqrt{120} and ∥e′∥L2=h/12\lVert e'\rVert_{L^2} = h/\sqrt{12}: 1.43×10−31.43\times10^{-3} and 3.61×10−23.61\times10^{-2} at n=8n = 8, as in the sim.

Rough problems: what the weak form buys

This keeps the previous lesson’s promise. A spectral method needs smoothness and a stencil needs two derivatives; the weak form needs neither.

A point load. Let f=δ(x−c)f = \delta(x - c), a concentrated force. The weak form is happy: bi=ϕi(c)b_i = \phi_i(c). The exact solution is a tent with a kink at cc, and by nodal exactness the finite element answer is exact at every node for any cc (below 10−1410^{-14} in the sim at c=0.4c = 0.4). If cc is a node the whole tent is represented and every error is round-off. If cc lies inside an element (always, for c=0.4c = 0.4 and 2k2^k elements) the straight line cannot follow the kink:

nn1664
L2L^2 error2.17×10−32.17\times10^{-3}2.71×10−42.71\times10^{-4}
H1H^1 error0.12250.12250.06120.0612

Over a factor 44 in nn the L2L^2 error falls by 8.08.0 (order 1.51.5) and the H1H^1 error by 2.02.0 (order 0.50.5): half an order lost to the one element holding the kink. Put a node on the kink and the loss disappears.

A jumping coefficient. Let a=1a = 1 for x<cx \lt c and a=10a = 10 for x>cx \gt c (two materials), f=1f = 1. The solution has a corner at the interface, since the flux au′a u' is continuous but u′u' is not. Stiffness uses the exact mean of aa over each element. With a node on the interface (c=0.5c = 0.5, nn even) the coefficient is constant on every element, nodal exactness applies, and the nodal error is 2×10−172\times10^{-17} at n=8,16,32n = 8, 16, 32, with orders 22 and 11 in the other norms (L2L^2 2.53×10−42.53\times10^{-4}, H1H^1 1.28×10−21.28\times10^{-2} at n=16n = 16). Move the interface to c=0.4c = 0.4, inside an element: the nodal error is 1.9×10−31.9\times10^{-3} at n=16n = 16 and 7.4×10−47.4\times10^{-4} at n=64n = 64, and the H1H^1 error falls only from 1.96×10−21.96\times10^{-2} to 1.03×10−21.03\times10^{-2}: the nodal argument’s hypothesis fails, and so does the rate. The rule: align the mesh with material interfaces.

Triangles in two dimensions

In two dimensions an interval becomes a triangle. The weak form of −∇2u=f-\nabla^2 u = f with u=0u = 0 on the boundary is ∫∇u⋅∇v dA=∫fv dA\int\nabla u\cdot\nabla v\,dA = \int f v\,dA. On a mesh of triangles the hat function ϕi\phi_i is the continuous function that is linear on each triangle, equals 11 at node ii and 00 at the others (a pyramid; its gradient is constant on each triangle). For a triangle with vertices (xk,yk)(x_k, y_k) and area AA, let b1=y2−y3b_1 = y_2 - y_3, b2=y3−y1b_2 = y_3 - y_1, b3=y1−y2b_3 = y_1 - y_2 and c1=x3−x2c_1 = x_3 - x_2, c2=x1−x3c_2 = x_1 - x_3, c3=x2−x1c_3 = x_2 - x_1. Then ∇ϕi=(bi,ci)/2A\nabla\phi_i = (b_i, c_i)/2A and

Kije=A ∇ϕi⋅∇ϕj=bibj+cicj4A.K^e_{ij} = A\,\nabla\phi_i\cdot\nabla\phi_j = \frac{b_ib_j + c_ic_j}{4A} .

For legs hh (vertices (0,0),(h,0),(0,h)(0,0), (h,0), (0,h)) this is (1−12−12−12120−12012)\begin{pmatrix} 1 & -\tfrac12 & -\tfrac12 \\ -\tfrac12 & \tfrac12 & 0 \\ -\tfrac12 & 0 & \tfrac12\end{pmatrix}, independent of hh. Split every square of a uniform grid by a diagonal and assemble. An interior node belongs to six triangles, and the row of the global matrix comes out as 44 on the diagonal and −1-1 for the four axis neighbors (the two diagonal neighbors cancel): the five-point stencil of 10.1 Finite Differences: Discretizing Derivatives, checked in the second playground cell below. On a square grid FEM reproduces finite differences; on arbitrary triangles it generalizes them. From memory: very small angles spoil the error bound, and Delaunay triangulation is the standard remedy.

FEM, finite differences and finite volumes

Finite differencesFinite volumesFinite elements
Unknownspoint valuescell averagescoefficients of basis functions
Built fromTaylor seriesflux balance on cellsweak form and test functions
Geometrygrid lines; awkward for curved wallsunstructured cells possibleunstructured meshes are the default
Rough coefficientsneed averaging choicesvia interface fluxesalign nodes with interfaces
Conservationnot built inexact, cell by cellglobal balance; local needs care
Shocks and advectionpossible with upwindingthe natural homeneeds stabilization (from memory)

Choose finite differences for simple geometry, finite volumes when a conservation law with shocks is the point (10.10 The Finite Volume Method), spectral methods for smooth periodic boxes, and finite elements when the domain or coefficients are complicated or the problem is structural.

The exercise: assembling and solving

First a playground that builds KK and MM for n=10n = 10 and compares the eigenvalues, and a second that assembles triangles.

🐍 PythonStiffness, mass and the smallest eigenvalues
⌘/Ctrl + Enter
🐍 PythonAssemble right triangles: the five-point stencil appears
⌘/Ctrl + Enter
🧪 Coding exerciseExercise: assemble and solve -u'' = f with linear elements
⌘/Ctrl + Enter

Concept checks

✅ Concept check

For $-(au')' = f$ solved by the weak form, which statement about boundary conditions is right?

✅ Concept check

With $a = 1$, an interior node has an element of length $0.1$ on its left and $0.4$ on its right. What is the diagonal entry $K_{ii}$ of the stiffness matrix?

✅ Concept check

At $n = 16$ elements the $H^1$ error of a smooth problem is $0.126$. What do you expect at $n = 64$?

✅ Concept checkselect all that apply

Select all conditions that the nodal-exactness argument for $-u'' = f$ needs, or that follow from it.

Problems

✏️ Pen & paperAssemble by hand●○○

Solve −u′′=2-u'' = 2 on (0,1)(0, 1), u(0)=u(1)=0u(0) = u(1) = 0, with linear elements on the nodes 0, 0.5, 0.75, 10,\ 0.5,\ 0.75,\ 1. Compare with the exact solution u=x(1−x)u = x(1-x).

Show worked solution

The element matrices have scale 2,4,42, 4, 4, so K=(2+4−4−44+4)=(6−4−48)K = \begin{pmatrix} 2 + 4 & -4 \\ -4 & 4 + 4\end{pmatrix} = \begin{pmatrix} 6 & -4 \\ -4 & 8\end{pmatrix} and b1=2 (0.5+0.25)/2=0.75b_1 = 2\,(0.5 + 0.25)/2 = 0.75, b2=2 (0.25+0.25)/2=0.5b_2 = 2\,(0.25 + 0.25)/2 = 0.5. The determinant is 48−16=3248 - 16 = 32. Then u1=(8⋅0.75+4⋅0.5)/32=0.25u_1 = (8\cdot0.75 + 4\cdot0.5)/32 = 0.25 and u2=(6⋅0.5+4⋅0.75)/32=0.1875u_2 = (6\cdot0.5 + 4\cdot0.75)/32 = 0.1875. The exact values are 0.5⋅0.5=0.250.5\cdot0.5 = 0.25 and 0.75⋅0.25=0.18750.75\cdot0.25 = 0.1875: nodal exactness again.

✏️ Pen & paperA natural boundary condition●●○

Solve −u′′=1-u'' = 1 with u(0)=0u(0) = 0 and u′(1)=0u'(1) = 0 using two equal elements, with unknowns at x=0.5x = 0.5 and x=1x = 1. Show where the Neumann condition enters, and compare with u=x−x2/2u = x - x^2/2.

Show worked solution

The node at x=1x = 1 is free, so the weak form gains the boundary term au′(1) v(1)=0a u'(1)\,v(1) = 0: the Neumann condition is used by adding nothing. With h=0.5h = 0.5 the stiffness is (4−2−22)\begin{pmatrix} 4 & -2 \\ -2 & 2\end{pmatrix} (the last node touches only one element) and the loads are b1=0.5b_1 = 0.5, b2=0.25b_2 = 0.25. The second row gives u2=u1+0.125u_2 = u_1 + 0.125; the first then gives 2u1−0.25=0.52u_1 - 0.25 = 0.5, so u1=0.375u_1 = 0.375 and u2=0.5u_2 = 0.5. The exact values are u(0.5)=0.375u(0.5) = 0.375 and u(1)=0.5u(1) = 0.5.

✏️ Pen & paperA triangle's element matrix●●●

Find KeK^e for the triangle with vertices (0,0)(0,0), (2,0)(2,0), (0,1)(0,1). Check that every row sums to zero, and say why.

Show worked solution

The area is A=1A = 1. The three hat functions are ϕ1=1−x/2−y\phi_1 = 1 - x/2 - y, ϕ2=x/2\phi_2 = x/2, ϕ3=y\phi_3 = y, with gradients (−12,−1)(-\tfrac12, -1), (12,0)(\tfrac12, 0), (0,1)(0, 1). Then K11=14+1=54K_{11} = \tfrac14 + 1 = \tfrac54, K12=−14K_{12} = -\tfrac14, K13=−1K_{13} = -1, K22=14K_{22} = \tfrac14, K23=0K_{23} = 0, K33=1K_{33} = 1, and the matrix is symmetric. The rows sum to 54−14−1=0\tfrac54 - \tfrac14 - 1 = 0, −14+14+0=0-\tfrac14 + \tfrac14 + 0 = 0 and −1+0+1=0-1 + 0 + 1 = 0. The hats sum to the constant 11, whose gradient is zero.

What comes next

This ends the module: finite differences, finite volumes, spectral transforms and finite elements all turn a PDE into a matrix, and the matrices are close relatives. For fluid and flight simulation, the pressure solve of 11.10 The Projection Method for Incompressible Flow is one of these systems and a wing structure is another. The next lesson in the build, 14.1 Coupled Oscillators & Normal Modes, meets the mass and stiffness matrices again, for masses on springs: the eigenproblem Ku=λMuK\mathbf u = \lambda M\mathbf u gives the normal modes, and its continuum limit leads back to the wave equation.

Key Takeaways

  • The weak form ∫au′v′=∫fv\int a u'v' = \int f v (test functions zero at Dirichlet ends) needs one derivative and is the minimizer of 12∫av′2−∫fv\tfrac12\int a v'^2 - \int f v. Dirichlet conditions are essential, Neumann natural.
  • Hat functions have ϕi(xj)=δij\phi_i(x_j) = \delta_{ij}, so unknowns are nodal values. Kij=∫aϕi′ϕj′K_{ij} = \int a\phi_i'\phi_j' is sparse, symmetric, positive definite; on a uniform mesh K=h×K = h\times(finite difference matrix), and mass lumping gives the finite difference scheme.
  • Assembly adds (aˉe/he)(1−1−11)(\bar a_e/h_e)\begin{pmatrix}1&-1\\-1&1\end{pmatrix} at each element’s nodes, on any spacing. At n=10n = 10 the smallest eigenvalue is 9.9519.951 (FEM), 9.7899.789 (FD), 9.8709.870 exact.
  • For −u′′=f-u'' = f in 1D the linear-element solution is exact at every node on any mesh when the load integrals are exact; with nodal-value loads it is the second order finite difference answer.
  • Smooth solutions converge at order 11 in H1H^1 and 22 in L2L^2. A kink or jump inside an element costs half an order (1.51.5 and 0.50.5 for a point load at c=0.4c = 0.4); a node on it removes the loss.
  • On triangles Kije=(bibj+cicj)/4AK^e_{ij} = (b_ib_j + c_ic_j)/4A, and right triangles assemble into the five-point stencil. FEM suits complex geometry, rough coefficients and structures.
  • Next: 14.1 Coupled Oscillators & Normal Modes, mass and stiffness matrices for masses on springs.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.