A12 · Lesson 49 · Finite elements
Weak forms, hat functions and stiffness matrices
Finite differences replace derivatives with differences on a grid. The finite element method takes another route: it looks for the best approximation to the solution among simple functions, such as straight-line pieces between nodes, in the sense of the equation's energy. It handles uneven meshes, layered materials and awkward shapes naturally, which is why commercial field solvers for capacitance, magnetics and heat are almost all finite-element codes. In one dimension the whole method fits on a page, and its matrix turns out to be the nodal-analysis matrix of a resistor ladder.
- The smooth curve is the exact solution of with , the parabola .
- The straight-line pieces are the finite-element solution with N elements. In this problem its values at the nodes are exactly right, and it is wrong only in between.
- Raise N. The readout gives the largest error, at the middle of each element: . Doubling N cuts it by four.
Predict
With linear elements, the error in u falls as . How does the error in the slope fall?
As h. Each piece is a straight line, so its slope is constant across the element while the true slope changes, by about across the element. Halving the elements halves the slope error but quarters the value error. That matters in field solvers: potentials converge faster than fields.
Name it
The weak form
For on (0, L) with at the ends, multiply by any smooth test function v that is zero at the ends and integrate by parts:
This weak form asks for only one derivative of u, so piecewise-linear functions qualify. For it's also the condition for u to minimize the energy .
Hat functions
Put nodes at . The hat function is 1 at node i, 0 at every other node, and straight in between. Write and use each as a test function (Galerkin's method). That gives a linear system with
The element matrices
On one element of length h with constant k, the two hats have slopes , so the element contributes to K. Adding the elements up (assembly) gives a tridiagonal matrix. With a uniform mesh and constant k, an interior row is , the same as the finite-difference matrix. A constant f gives .
Each element is a conductance between two nodes, so K is exactly the nodal conductance matrix of a resistor ladder, and F is the current injected at each node. Layered materials just change the conductances.
Accuracy and eigenvalues
With linear elements the error is in u and in u'. For eigenvalue problems the method also needs the mass matrix , with element matrix ; solving gives eigenvalues that are always too high, by the Rayleigh quotient argument of lesson 34.
Worked example
Solve on (0, 1), with zero ends, using two elements.
Show the solution
- One unknown. With h = ½ there is one unknown, at x = ½.
- Its row of K. The neighbors' values are fixed at 0, so the row is
- Its load.
- Solve. giving .
- Compare with the exact solution. is also at the midpoint: the nodal value is exact. In one dimension with constant coefficients, linear elements always get the nodal values right for this kind of problem, and the error lives entirely between the nodes.
Answer at x = ½, the exact value.
Worked example
A wall has two layers, each 0.5 m thick, with conductivities 1 and 4 W/(m·K). One face is at 0 °C and the other at 1 °C. What is the temperature at the interface?
Show the solution
- Element conductances. Use one element per layer. Their conductances are
- Balance at the interface. The interface node's equation is the current balance so .
- Check with series resistances. That's exact: heat flows through two thermal resistances in series, 0.5 and 0.125 m²·K/W, so the flux is and it drops °C across the first layer.
- Why elements handle the jump. Finite differences across a jump in k need special care; finite elements get it right automatically, because each element keeps its own k.
Answer the interface is at 0.8 °C.
Worked example
Estimate the lowest eigenvalue of on (0, 1) with fixed ends using four linear elements, and compare with π².
Show the solution
- The discrete eigenvalue. For a uniform mesh, the discrete eigenvectors are samples of , and the generalized eigenvalue works out to
- Four elements. With h = ¼: against : 5.2% high.
- Lumped mass. Replacing M by a diagonal "lumped" matrix with h on the diagonal gives the finite-difference value 5.0% low.
- The pattern. The consistent mass matrix always errs high, as a Rayleigh quotient must, and the errors shrink as : with eight elements they are 1.3% each way.
Answer four elements give , 5.2% above .
Check
With 4 equal linear elements (consistent mass), what estimate of the lowest eigenvalue of on (0, 1) with fixed ends do you get?
Check
A slab has two layers of equal thickness, with conductivities 1 and 4. Its faces are held at 0 and 10. With one element per layer, what is the value at the interface?
Check
A uniform mesh of linear elements has element length 0.1 and constant coefficient k = 2. What is the diagonal entry of K for an interior node?
Check
For on a uniform mesh with element length 0.1, what is the load at the interior node = 0.5?
Check
A linear-element solution is refined by splitting every element into 5.
(a)
By what factor does the largest error in u shrink?
(b)
And the largest error in u'?
Check
In circuit terms, what is the stiffness matrix of 1D linear elements for ?
Check
Solve on (0, L) with zero ends using two equal elements.
Step 1
What is the stiffness entry of the middle node, with ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is its load, ?
Step 3
What is the computed value at the middle?
Check
Write fem(k, f, N) that assembles and solves the linear-element system for on (0, 1) with zero ends, N equal elements, k(e) the coefficient on element e and f a constant. Return the interior nodal values. Set err to the largest nodal error for k = 1, f = 1, N = 8 against .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why are finite-element eigenvalues always too high?
The lowest eigenvalue of is the smallest value of the Rayleigh quotient over all functions that satisfy the boundary conditions, reached only by the true eigenfunction. The finite-element method with a consistent mass matrix minimizes the same quotient, but only over the piecewise-linear functions on the mesh, which is a smaller set. A minimum over a smaller set can't be lower than the minimum over everything, so , with equality only if the eigenfunction happened to be piecewise linear. Refining the mesh enlarges the set, so the estimates fall toward the true value from above.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.