Skip to article frontmatterSkip to article content

The Galerkin method

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

u=p(x)u+q(x)u+r(x),axb,u(a)=0,  u(b)=0.u'' = p(x)\,u' + q(x)\,u + r(x), \quad a \le x \le b, \quad u(a)=0,\; u(b)=0.

However, we will assume that the linear problem is presented in the equivalent form

ddx[c(x)u(x)]+s(x)u(x)=f(x),u(a)=0,  u(b)=0. - \frac{d }{d x} \Bigl[ c(x)\, u'(x) \Bigr] + s(x) \, u(x) = f(x), \quad u(a)=0,\; u(b)=0.

Such a transformation is always possible, at least in principle (see Exercise 3), and the case when u(a)u(a) and u(b)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.

10.6.1Weak formulation

Let (10.6.2) be multiplied by a generic function ψ(x)\psi(x) called the test function, and then integrate both sides in xx:

abf(x)ψ(x)dx=ab[(c(x)u(x))ψ(x)+s(x)u(x)ψ(x)]dx=[c(x)u(x)ψ(x)]ab+ab[c(x)u(x)ψ(x)+s(x)u(x)ψ(x)]dx.\begin{split} \int_a^b f(x)\psi(x) \,dx &= \int_a^b \bigl[ -(c(x)u'(x))'\psi(x) + s(x)u(x)\psi(x) \bigr] \,dx \\ &= \Bigl[-c(x)u'(x)\psi(x) \Bigr]_{\,a}^{\,b} + \int_a^b \bigl[ c(x)u'(x)\psi'(x) + s(x)u(x)\psi(x)\bigr] \, dx. \end{split}

The last line above used an integration by parts.

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\psi(a)=\psi(b)=0. Doing so leads to

ab[c(x)u(x)ψ(x)+s(x)u(x)ψ(x)]dx=abf(x)ψ(x)dx, \int_a^b \bigl[ c(x)u'(x)\psi'(x) + s(x)u(x)\psi(x)\bigr] \,dx = \int_a^b f(x)\psi(x) \,dx,

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).

10.6.2Galerkin conditions

Our goal is to solve a finite-dimensional problem that approximates the weak form of the BVP. Let ϕ0,ϕ1,,ϕm\phi_0,\phi_1,\ldots,\phi_m be linearly independent functions satisfying ϕi(a)=ϕi(b)=0\phi_i(a)=\phi_i(b)=0. If we require

ψ(x)=i=1mziϕi(x),\psi(x) = \sum_{i=1}^m z_i \phi_i(x),

then (10.6.4) becomes, after some rearrangement,

i=1mzi[ab[c(x)u(x)ϕi(x)dx+s(x)u(x)ϕi(x)f(x)ϕi(x)]dx]=0.\begin{split} \sum_{i=1}^m z_i \left[ \int_a^b \bigl[ c(x)u'(x)\phi_i'(x)\,dx + s(x)u(x)\phi_i(x) - f(x) \phi_i(x)\bigr] \, d x \right] = 0. \end{split}

One way to satisfy this condition is to ensure that the term inside the brackets is zero for each possible value of ii, that is,

ab[c(x)u(x)ϕi(x)+s(x)u(x)ϕi(x)]dx=abf(x)ϕi(x)dx \int_a^b \bigl[ c(x)u'(x)\phi_i'(x) + s(x)u(x)\phi_i(x)\bigr] \,dx = \int_a^b f(x)\phi_i(x) \,dx

for i=1,,mi=1,\ldots,m. The independence of the ϕi\phi_i furthermore guarantees that this is the only possibility, so we no longer need to consider the ziz_i.

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)u(x) the same way as we did the test function ψ, where the ϕj\phi_j form a basis for representing the solution:

u(x)=j=1mwjϕj(x). u(x) = \sum_{j=1}^m w_j \phi_j(x).

Substituting (10.6.8) into (10.6.7) implies

ab{c(x)[j=1mwjϕj(x)]ϕi(x)+s(x)[j=1mwjϕj(x)]ϕi(x)}dx=abf(x)ϕi(x)dx\int_a^b \left\{ c(x) \Bigl[ \sum_{j=1}^m w_j \phi_j'(x) \Bigr] \phi_i'(x) + s(x)\Bigl[ \sum_{j=1}^m w_j \phi_j(x) \Bigr]\phi_i(x) \right\} \,dx = \int_a^b f(x)\phi_i(x) \,dx

for i=1,,mi=1,\ldots,m. This rearranges easily into

j=1mwj[abc(x)ϕi(x)ϕj(x)dx+abs(x)ϕi(x)ϕj(x)dx]=abf(x)ϕi(x)dx, \sum_{j=1}^m w_j \left[ \int_a^b c(x)\phi_i'(x)\phi_j'(x) \,dx + \int_a^b s(x)\phi_i(x)\phi_j(x) \,dx \right] = \int_a^b f(x)\phi_i(x) \,dx,

still for each i=1,,mi=1,\ldots,m. These are the Galerkin conditions defining a numerical solution. They follow entirely from the BVP and the choice of the ϕi\phi_i.

The conditions (10.6.10) are a linear system of equations for the unknown coefficients wjw_j. Define m×mm\times m matrices K\mathbf{K} and M\mathbf{M}, and the vector f\mathbf{f}, by

Kij=abc(x)ϕi(x)ϕj(x)dx,i,j=0,,m,Mij=abs(x)ϕi(x)ϕj(x)dx,i,j=0,,m,fi=abf(x)ϕi(x)dxi=0,,m.\begin{split} K_{ij} &= \int_a^b c(x)\phi_i'(x)\phi_j'(x) \,dx, \quad i,j=0,\ldots,m,\\ M_{ij} &= \int_a^b s(x)\phi_i(x)\phi_j(x) \,dx, \quad i,j=0,\ldots,m, \\ f_i &= \int_a^b f(x)\phi_i(x) \,dx \quad i=0,\ldots,m. \end{split}

Then (10.6.10) is simply

(K+M)w=f. (\mathbf{K}+\mathbf{M})\mathbf{w} = \mathbf{f}.

The matrix K\mathbf{K} is called the stiffness matrix and M\mathbf{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\phi_1,\ldots,\phi_m and obtain a fully specified algorithm.

10.6.3Finite elements

One useful and general choice for the ϕi\phi_i are the piecewise linear hat functions constructed in Piecewise linear interpolation. As usual, we select nodes a=t0<t1<<tn=ba=t_0 < t_1 < \cdots < t_n=b. Also define

hi=titi1,i=1,,n.h_i = t_i - t_{i-1}, \qquad i=1,\ldots,n.

Then we set m=n1m=n-1, and the ϕi\phi_i in (10.6.8) are

ϕi(x)=Hi(x)={xti1hiif x[ti1,ti],ti+1xhi+1if x[ti,ti+1],0otherwise. \phi_i(x) = H_i(x) = \begin{cases} \dfrac{x-t_{i-1}}{h_i} & \text{if $x\in[t_{i-1},t_i]$},\\[2.5ex] \dfrac{t_{i+1}-x}{h_{i+1}} & \text{if $x\in[t_{i},t_{i+1}]$},\\[2.5ex] 0 & \text{otherwise}. \end{cases}

Recall that these functions are cardinal, i.e., Hi(ti)=1H_i(t_i)=1 and Hi(tj)=0H_i(t_j)=0 if iji\neq j. Hence

u(x)=j=1mwjϕj(x)=j=1n1ujHj(x), u(x) = \sum_{j=1}^m w_j \phi_j(x) = \sum_{j=1}^{n-1} u_j H_j(x),

where as usual uju_j is the value of the numerical solution at tjt_j. Note that we omit H0H_0 and HnH_n, which equal one at the endpoints of the interval, because the boundary conditions on uu 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=[tk1,tk]I_k=[t_{k-1},t_k]. Specifically, we use

Kij=k=1n[Ikc(x)Hi(x)Hj(x)dx],i,j=1,,n1,Mij=k=1n[Iks(x)Hi(x)Hj(x)dx],i,j=1,,n1,fi=k=1n[Ikf(x)Hi(x)dx]i=1,,n1. \begin{split} K_{ij} &= \sum_{k=1}^{n} \left[ \int_{I_k} c(x) H_i'(x) H_j'(x) \,dx\right], \qquad i,j=1,\ldots,n-1, \\ M_{ij} &= \sum_{k=1}^{n} \left[ \int_{I_k} s(x)H_i(x)H_j(x) \,dx\right], \qquad i,j=1,\ldots,n-1, \\ f_i &= \sum_{k=1}^{n} \left[ \int_{I_k} f(x) H_i(x) \,dx\right] \qquad i=1,\ldots,n-1. \end{split}

Start with the first subinterval, I1I_1. The only hat function that is nonzero over I1I_1 is H1(x)H_1(x). Thus the only integrals we need to consider over I1I_1 have i=j=1i=j=1:

I1c(x)H1(x)H1(x)dx,I1s(x)H1(x)H1(x)dx,I1f(x)H1(x)dx,\int_{I_1} c(x) H_1'(x) H_1'(x) \,dx, \qquad \int_{I_1} s(x) H_1(x) H_1(x) \,dx, \qquad \int_{I_1} f(x) H_1(x) \,dx,

which contribute to the sums for K11K_{11}, M11M_{11}, and f1f_1, respectively.

Before writing more formulas, we make one more very useful simplification. Unless the coefficient functions c(x)c(x), s(x)s(x), and f(x)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 IkI_k, namely the average of the endpoint values:

c(x)ck=c(tk1)+c(tk)2for xIk.c(x) \approx \overline{c}_k = \frac{c(t_{k-1})+c(t_k)}{2} \quad \text{for $x\in I_k$}.

Thus the integrals in (10.6.19) can be evaluated solely from the node locations. For instance,

I1c(x)H1(x)H1(x)dxc1t0t1h12dx=c1h1.\int_{I_1} c(x) H_1'(x) H_1'(x) \,dx \approx \overline{c}_1 \int_{t_0}^{t_1} h_1^{-2} \, dx = \frac{\overline{c}_1}{h_1}.

Now consider interval I2=[t1,t2]I_2=[t_1,t_2]. Here both H1H_1 and H2H_2 are nonzero, so there are contributions to all of the matrix elements K11K_{11}, K12=K21K_{12}=K_{21}, K22K_{22}, to M11M_{11}, M12=M21M_{12}=M_{21}, M22M_{22}, and to f1f_1 and f2f_2. Over I2I_2 we have H2=h21H_2'= h_2^{-1} and H1=h21H_{1}'= -h_2^{-1}. Hence the contributions to K11K_{11} and K22K_{22} in (10.6.19) are c2/h2\overline{c}_2/h_2, and the contributions to K12=K21K_{12}=K_{21} are c2/h2-\overline{c}_2/h_2. We summarize the relationship by

ckhk[1111][K11K12K21K22],\frac{\overline{c}_k}{h_k} \begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix} \rightsquigarrow \begin{bmatrix} K_{11} & K_{12} \\ K_{21} & K_{22} \end{bmatrix},

where the squiggly arrow is meant to show that the values of the 2×22\times 2 matrix on the left are added to the appropriate submatrix of K\mathbf{K} on the right. Similar expressions are obtained for contributions to M\mathbf{M} and f\mathbf{f} in (10.6.19); see below.

In general, over IkI_k for 1<k<n1<k<n, we have Hk=hk1H_k'= h_k^{-1} and Hk1=hk1H_{k-1}'=-h_k^{-1}. The stiffness matrix contributions over IkI_k become

ckhk[1111][Kk1,k1Kk1,kKk,k1Kk,k],k=2,,n1. \frac{\overline{c}_k}{h_k} \begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix} \rightsquigarrow \begin{bmatrix} K_{k-1,k-1} & K_{k-1,k} \\ K_{k,k-1} & K_{k,k} \end{bmatrix}, \qquad k=2,\ldots,n-1.

One finds the contributions to the other structures by similar computations:

skhk6[2112][Mk1,k1Mk1,kMk,k1Mk,k],k=2,,n1, \frac{\overline{s}_k h_k}{6} \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} \rightsquigarrow \begin{bmatrix} M_{k-1,k-1} & M_{k-1,k} \\ M_{k,k-1} & M_{k,k} \end{bmatrix}, \qquad k=2,\ldots,n-1,

and

fkhk2[11][fk1fk],k=2,,n1. \frac{\overline{f}_k h_k}{2} \begin{bmatrix} 1 \\ 1 \end{bmatrix} \rightsquigarrow \begin{bmatrix} f_{k-1} \\ f_{k} \end{bmatrix}, \qquad k=2,\ldots,n-1.

The contribution from InI_n affects just Kn1,n1K_{n-1,n-1}, Mn1,n1M_{n-1,n-1}, and fn1f_{n-1}, and it produces formulas similar to those for I1I_1.

Each IkI_k contributes to four elements of each matrix and two of the vector f\mathbf{f}, except for I1I_1 and InI_n, which each contribute to just one element of each matrix and f\mathbf{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.

10.6.4Implementation and convergence

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 hh is O(h2)O(h^2) accurate, the accuracy of the FEM method based on linear interpolation as implemented here is similar to the second-order finite-difference method.

10.6.5Exercises

  1. ⌨ For each linear BVP, use Function 10.6.1 to solve the problem and plot the solution for n=40n=40. Then for each n=10,20,40,,640n=10,20,40,\ldots,640, compute the norm of the error. Make a log-log convergence plot of error versus nn and compare graphically to second-order convergence.

    (a) u+u=8+16x2x4,u(0)=u(2)=0-u''+u=-8 + 16 x^2 - x^4, \quad u(0) =u(2) =0

    Exact solution: x2(4x2)x^2(4-x^2)

    (b) [(2+x)u]+11xu=ex(12x3+7x2+1),u(1)=u(1)=0[(2+x)u']' +11x u = -e^x \left(12 x^3+7 x^2+1\right), \quad u(-1) =u(1) =0

    Exact solution: ex(1x2)e^x \left(1-x^2\right)

    (c) u+x(u+u)=x[4sin(x)+5xcos(x)],u(0)=u(2π)=0u''+x(u'+u) = -x[4 \sin(x)+5 x \cos(x)], \quad u(0) =u(2\pi) =0

    Exact solution: x2sin(x)-x^2\sin(x)

  1. ✍ Suppose you want to solve the differential equation [c(x)u]+d(x)u=f(x)-[c(x)u']'+d(x)u = f(x), as in Equation (10.6.2), except with the boundary conditions u(a)=αu(a)=\alpha, u(b)=βu(b)=\beta. Find constants pp and qq such that if v(x)=u(x)+px+qv(x)=u(x)+px+q, then vv satisfies the same BVP, except that v(a)=v(b)=0v(a)=v(b)=0 and ff is replaced by a different function.
  1. ✍ Suppose p(x)u(x)+q(x)u(x)+r(x)=0p(x)u''(x)+q(x)u'(x)+r(x)=0, and assume that p(x)0p(x)\neq 0 for all xx in [a,b][a,b]. Let z(x)z(x) be any function satisfying z=q/pz'=q/p. Show that the differential equation is equivalent to one in the form (10.6.2), and find the functions c(x)c(x), d(x)d(x), and f(x)f(x) in that equation. (Hint: Start by multiplying through the equation by exp(z)\exp(z).)

  2. ✍ Derive (10.6.25), starting from the mass matrix definition in (10.6.19). You should replace s(x)s(x) by the constant sk\overline{s}_k within interval IkI_k.

  1. Suppose the Dirichlet boundary conditions u(a)=u(b)=0u(a)=u(b)=0 are replaced by the homogeneous Neumann conditions u(a)=u(b)=0u'(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

    u(x)=j=0nujHj(x)u(x) = \sum_{j=0}^{n} u_j H_j(x)

    and making (10.6.19) hold for all i,ji,j from 0 to nn. Modify Function 10.6.1 to do this and thereby solve the Neumann problem. (Note that I1I_1 and InI_n now each make multiple contributions, like all the other integration subintervals.)

    (c) ⌨ Test your function on the problem

    u+u=2sin(x),u(0)=u(1)=0,u''+u = -2 \sin(x), \quad u'(0)=u'(1)=0,

    whose exact solution is (x1)cos(x)sin(x)(x-1)\cos(x) - \sin(x). Show second-order convergence.