Using finite differences we defined a collocation method in which an approximation of the differential equation is required to hold at a finite set of nodes. In this section we present an alternative based on integration rather than differentiation. Our presentation will be limited to the linear BVP
Such a transformation is always possible, at least in principle (see Exercise 3), and the case when u(a) and u(b) are nonzero can also be incorporated (see Exercise 2). The approach is also adaptable to Neumann conditions (see Exercise 5). As with finite differences, a nonlinear problem is typically solved by using a Newton iteration to create a sequence of linear problems.
We now make an important and convenient assumption about the test function. The first term in (10.6.3), consisting of boundary evaluations, disappears if we require that ψ(a)=ψ(b)=0. Doing so leads to
which is known as the weak form of the differential equation (10.6.2).
Every solution of (10.6.2) (what we might now call the strong form of the problem) is a weak solution, but the converse is not always true. While the weak form might look odd, in many mathematical models it could be considered more fundamental than (10.6.2).
Our goal is to solve a finite-dimensional problem that approximates the weak form of the BVP. Let ϕ0,ϕ1,…,ϕm be linearly independent functions satisfying ϕi(a)=ϕi(b)=0. If we require
for i=1,…,m. The independence of the ϕi furthermore guarantees that this is the only possibility, so we no longer need to consider the zi.
Now that we have approximated the weak form of the BVP by a finite set of constraints, the next step is to represent the approximate solution by a finite set as well. A natural choice is to approximate u(x) the same way as we did the test function ψ, where the ϕj form a basis for representing the solution:
The matrix K is called the stiffness matrix and M is called the mass matrix. By their definitions, they are symmetric. The last piece of the puzzle is to make some selection of ϕ1,…,ϕm and obtain a fully specified algorithm.
One useful and general choice for the ϕi are the piecewise linear hat functions constructed in Piecewise linear interpolation. As usual, we select nodes a=t0<t1<⋯<tn=b. Also define
where as usual uj is the value of the numerical solution at tj. Note that we omit H0 and Hn, which equal one at the endpoints of the interval, because the boundary conditions on u render them irrelevant.
The importance of the hat function basis in the Galerkin method is that each one is nonzero in only two adjacent intervals. As a result, we shift the focus from integrations over the entire interval in (10.6.11) to integrations over each subinterval, Ik=[tk−1,tk]. Specifically, we use
Start with the first subinterval, I1. The only hat function that is nonzero over I1 is H1(x). Thus the only integrals we need to consider over I1 have i=j=1:
which contribute to the sums for K11, M11, and f1, respectively.
Before writing more formulas, we make one more very useful simplification. Unless the coefficient functions c(x), s(x), and f(x) specified in the problem are especially simple functions, the natural choice for evaluating all of the required integrals is numerical integration, say by the trapezoid formula. As it turns out, though, such integration is not really necessary. The fact that we have approximated the solution of the BVP by a piecewise linear interpolant makes the numerical method second-order accurate overall. It can be proven that the error is still second order if we replace each of the coefficient functions by a constant over Ik, namely the average of the endpoint values:
Now consider interval I2=[t1,t2]. Here both H1 and H2 are nonzero, so there are contributions to all of the matrix elements K11, K12=K21, K22, to M11, M12=M21, M22, and to f1 and f2. Over I2 we have H2′=h2−1 and H1′=−h2−1. Hence the contributions to K11 and K22 in (10.6.19) are c2/h2, and the contributions to K12=K21 are −c2/h2. We summarize the relationship by
where the squiggly arrow is meant to show that the values of the 2×2 matrix on the left are added to the appropriate submatrix of K on the right. Similar expressions are obtained for contributions to M and f in (10.6.19); see below.
In general, over Ik for 1<k<n, we have Hk′=hk−1 and Hk−1′=−hk−1. The stiffness matrix contributions over Ik become
The contribution from In affects just Kn−1,n−1, Mn−1,n−1, and fn−1, and it produces formulas similar to those for I1.
Each Ik contributes to four elements of each matrix and two of the vector f, except for I1 and In, which each contribute to just one element of each matrix and f. The spatially localized contributions to the matrices characterize a finite element method (FEM). Putting together all of the contributions to (10.6.12) to form the complete algebraic system is often referred to as the assembly process.
Function 10.6.1 implements the piecewise linear FEM on the linear problem as posed in (10.6.4), using an equispaced grid. The code closely follows the description above.
Because piecewise linear interpolation on a uniform grid of size h is O(h2) accurate, the accuracy of the FEM method based on linear interpolation as implemented here is similar to the second-order finite-difference method.
⌨ For each linear BVP, use Function 10.6.1 to solve the problem and plot the solution for n=40. Then for each n=10,20,40,…,640, compute the norm of the error. Make a log-log convergence plot of error versus n and compare graphically to second-order convergence.
(a)−u′′+u=−8+16x2−x4,u(0)=u(2)=0
Exact solution: x2(4−x2)
(b)[(2+x)u′]′+11xu=−ex(12x3+7x2+1),u(−1)=u(1)=0
Exact solution: ex(1−x2)
(c)u′′+x(u′+u)=−x[4sin(x)+5xcos(x)],u(0)=u(2π)=0
Exact solution: −x2sin(x)
✍ Suppose you want to solve the differential equation −[c(x)u′]′+d(x)u=f(x), as in Equation (10.6.2), except with the boundary conditions u(a)=α, u(b)=β. Find constants p and q such that if v(x)=u(x)+px+q, then v satisfies the same BVP, except that v(a)=v(b)=0 and f is replaced by a different function.
✍ Suppose p(x)u′′(x)+q(x)u′(x)+r(x)=0, and assume that p(x)=0 for all x in [a,b]. Let z(x) be any function satisfying z′=q/p. Show that the differential equation is equivalent to one in the form (10.6.2), and find the functions c(x), d(x), and f(x) in that equation. (Hint: Start by multiplying through the equation by exp(z).)
✍ Derive (10.6.25), starting from the mass matrix definition in (10.6.19). You should replace s(x) by the constant sk within interval Ik.
Suppose the Dirichlet boundary conditions u(a)=u(b)=0 are replaced by the homogeneous Neumann conditions u′(a)=u′(b)=0.
(a) ✍ Explain why the weak form (10.6.4) can be derived without any boundary conditions on the test function ψ.
(b) ⌨ The result of part (a) suggests replacing (10.6.18) with
and making (10.6.19) hold for all i,j from 0 to n. Modify Function 10.6.1 to do this and thereby solve the Neumann problem. (Note that I1 and In now each make multiple contributions, like all the other integration subintervals.)