The Galerkin method Tobin A. Driscoll Richard J. Braun 
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 ) , a ≤ x ≤ b , 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. u ′′ = p ( x ) u ′ + q ( x ) u + r ( x ) , a ≤ x ≤ b , u ( a ) = 0 , u ( b ) = 0. However, we will assume that the linear problem is presented in the equivalent form
− d d x [ 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. − d x d  [ c ( x ) u ′ ( x ) ] + s ( x ) u ( x ) = f ( x ) , u ( a ) = 0 , u ( b ) = 0. Such a transformation is always possible, at least in principle (see Exercise 10.6.5 
Let (10.6.2) ψ ( x ) \psi(x) ψ ( x ) test function , and then integrate both sides in x x x 
∫ a b f ( x ) ψ ( x )   d x = ∫ a b [ − ( c ( x ) u ′ ( x ) ) ′ ψ ( x ) + s ( x ) u ( x ) ψ ( x ) ]   d x = [ − c ( x ) u ′ ( x ) ψ ( x ) ]   a   b + ∫ a b [ c ( x ) u ′ ( x ) ψ ′ ( x ) + s ( x ) u ( x ) ψ ( x ) ]   d x . \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} ∫ a b  f ( x ) ψ ( x ) d x  = ∫ a b  [ − ( c ( x ) u ′ ( x ) ) ′ ψ ( x ) + s ( x ) u ( x ) ψ ( x ) ] d x = [ − c ( x ) u ′ ( x ) ψ ( x ) ] a b  + ∫ a b  [ c ( x ) u ′ ( x ) ψ ′ ( x ) + s ( x ) u ( x ) ψ ( x ) ] d x .  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) ψ ( a ) = ψ ( b ) = 0 \psi(a)=\psi(b)=0 ψ ( a ) = ψ ( b ) = 0 
∫ a b [ c ( x ) u ′ ( x ) ψ ′ ( x ) + s ( x ) u ( x ) ψ ( x ) ]   d x = ∫ a b f ( x ) ψ ( x )   d x ,   \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, ∫ a b  [ c ( x ) u ′ ( x ) ψ ′ ( x ) + s ( x ) u ( x ) ψ ( x ) ] d x = ∫ a b  f ( x ) ψ ( x ) d x , which is known as the weak form  of the differential equation (10.6.2) 
Every solution of (10.6.2) (10.6.2) 
10.6.2 Galerkin 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 ϕ 0  , ϕ 1  , … , ϕ m  ϕ i ( a ) = ϕ i ( b ) = 0 \phi_i(a)=\phi_i(b)=0 ϕ i  ( a ) = ϕ i  ( b ) = 0 
ψ ( x ) = ∑ i = 1 m z i ϕ i ( x ) , \psi(x) = \sum_{i=1}^m z_i \phi_i(x), ψ ( x ) = i = 1 ∑ m  z i  ϕ i  ( x ) , then (10.6.4) 
∑ i = 1 m z i [ ∫ a b [ c ( x ) u ′ ( x ) ϕ i ′ ( x )   d x + s ( x ) u ( x ) ϕ i ( x ) − f ( x ) ϕ i ( x ) ]   d x ] = 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} i = 1 ∑ m  z i  [ ∫ a b  [ c ( x ) u ′ ( x ) ϕ i ′  ( x ) d x + s ( x ) u ( x ) ϕ i  ( x ) − f ( x ) ϕ i  ( x ) ] d x ] = 0.  One way to satisfy this condition is to ensure that the term inside the brackets is zero for each possible value of i i i 
∫ a b [ c ( x ) u ′ ( x ) ϕ i ′ ( x ) + s ( x ) u ( x ) ϕ i ( x ) ]   d x = ∫ a b f ( x ) ϕ i ( x )   d x   \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 ∫ a b  [ c ( x ) u ′ ( x ) ϕ i ′  ( x ) + s ( x ) u ( x ) ϕ i  ( x ) ] d x = ∫ a b  f ( x ) ϕ i  ( x ) d x for i = 1 , … , m i=1,\ldots,m i = 1 , … , m ϕ i \phi_i ϕ i  only  possibility, so we no longer need to consider the z i z_i z 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) u ( x ) ψ \psi ψ ϕ j \phi_j ϕ j  
u ( x ) = ∑ j = 1 m w j ϕ j ( x ) .   u(x) = \sum_{j=1}^m w_j \phi_j(x). u ( x ) = j = 1 ∑ m  w j  ϕ j  ( x ) . Substituting (10.6.8) (10.6.7) 
∫ a b { c ( x ) [ ∑ j = 1 m w j ϕ j ′ ( x ) ] ϕ i ′ ( x ) + s ( x ) [ ∑ j = 1 m w j ϕ j ( x ) ] ϕ i ( x ) }   d x = ∫ a b f ( x ) ϕ i ( x )   d x \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 ∫ a b  { c ( x ) [ j = 1 ∑ m  w j  ϕ j ′  ( x ) ] ϕ i ′  ( x ) + s ( x ) [ j = 1 ∑ m  w j  ϕ j  ( x ) ] ϕ i  ( x ) } d x = ∫ a b  f ( x ) ϕ i  ( x ) d x for i = 1 , … , m i=1,\ldots,m i = 1 , … , m 
∑ j = 1 m w j [ ∫ a b c ( x ) ϕ i ′ ( x ) ϕ j ′ ( x )   d x + ∫ a b s ( x ) ϕ i ( x ) ϕ j ( x )   d x ] = ∫ a b f ( x ) ϕ i ( x )   d x ,   \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, j = 1 ∑ m  w j  [ ∫ a b  c ( x ) ϕ i ′  ( x ) ϕ j ′  ( x ) d x + ∫ a b  s ( x ) ϕ i  ( x ) ϕ j  ( x ) d x ] = ∫ a b  f ( x ) ϕ i  ( x ) d x , still for each i = 1 , … , m i=1,\ldots,m i = 1 , … , m Galerkin conditions BVP  and the choice of the ϕ i \phi_i ϕ i  
The conditions (10.6.10) w j w_j w j  m × m m\times m m × m K \mathbf{K} K M \mathbf{M} M f \mathbf{f} f 
K i j = ∫ a b c ( x ) ϕ i ′ ( x ) ϕ j ′ ( x )   d x , i , j = 0 , … , m , M i j = ∫ a b s ( x ) ϕ i ( x ) ϕ j ( x )   d x , i , j = 0 , … , m , f i = ∫ a b f ( x ) ϕ i ( x )   d x i = 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} K ij  M ij  f i   = ∫ a b  c ( x ) ϕ i ′  ( x ) ϕ j ′  ( x ) d x , i , j = 0 , … , m , = ∫ a b  s ( x ) ϕ i  ( x ) ϕ j  ( x ) d x , i , j = 0 , … , m , = ∫ a b  f ( x ) ϕ i  ( x ) d x i = 0 , … , m .  Then (10.6.10) 
( K + M ) w = f .   (\mathbf{K}+\mathbf{M})\mathbf{w} = \mathbf{f}. ( K + M ) w = f . The matrix K \mathbf{K} K stiffness matrix  and M \mathbf{M} M 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 ϕ 1  , … , ϕ m  
Suppose we are given − u ′ ′ + 4 u = x -u''+4u=x − u ′′ + 4 u = x u ( 0 ) = u ( π ) = 0 u(0)=u(\pi)=0 u ( 0 ) = u ( π ) = 0 ϕ k = sin  ( k x ) \phi_k=\sin(kx) ϕ k  = sin ( k x ) k = 1 , 2 , 3 k=1,2,3 k = 1 , 2 , 3 
M i j = 4 ∫ 0 π sin  ( i x ) sin  ( j x )   d x , K i j = i j ∫ 0 π cos  ( i x ) cos  ( j x )   d x , f i = ∫ 0 π x sin  ( i x )   d x . \begin{split}
  M_{ij} & = 4 \int_0^\pi \sin(ix) \sin(jx)\, dx, \\
  K_{ij} & = ij \int_0^\pi \cos(ix) \cos(jx)\, dx, \\
  f_i &= \int_0^\pi x \sin(ix)\, dx.
\end{split} M ij  K ij  f i   = 4 ∫ 0 π  sin ( i x ) sin ( j x ) d x , = ij ∫ 0 π  cos ( i x ) cos ( j x ) d x , = ∫ 0 π  x sin ( i x ) d x .  With some calculation, we find
M = 2 π [ 1 0 0 0 1 0 0 0 1 ] , K = π 2 [ 1 0 0 0 4 0 0 0 9 ] , f = π [ 1 − 1 / 2 1 / 3 ] . \mathbf{M} = 2\pi
\begin{bmatrix}
  1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1
\end{bmatrix}, \qquad
  \mathbf{K} = \frac{\pi}{2}
\begin{bmatrix}
  1 & 0 & 0 \\ 0 & 4 & 0 \\ 0 & 0 & 9
\end{bmatrix}, \qquad
\mathbf{f} = \pi
\begin{bmatrix}
  1 \\ -1/2 \\ 1/3
\end{bmatrix}. M = 2 π ⎣ ⎡  1 0 0  0 1 0  0 0 1  ⎦ ⎤  , K = 2 π  ⎣ ⎡  1 0 0  0 4 0  0 0 9  ⎦ ⎤  , f = π ⎣ ⎡  1 − 1/2 1/3  ⎦ ⎤  . Upon solving the resulting diagonal linear system, the approximate solution is
2 5 sin  ( x ) − 1 8 sin  ( 2 x ) + 2 39 sin  ( 3 x ) . \frac{2}{5}\sin(x) - \frac{1}{8} \sin(2x) + \frac{2}{39}\sin(3x). 5 2  sin ( x ) − 8 1  sin ( 2 x ) + 39 2  sin ( 3 x ) . 10.6.3 Finite elements ¶ One useful and general choice for the ϕ i \phi_i ϕ i  Piecewise linear interpolation . As usual, we select nodes a = t 0 < t 1 < ⋯ < t n = b a=t_0 < t_1 < \cdots < t_n=b a = t 0  < t 1  < ⋯ < t n  = b 
h i = t i − t i − 1 , i = 1 , … , n . h_i = t_i - t_{i-1}, \qquad i=1,\ldots,n. h i  = t i  − t i − 1  , i = 1 , … , n . Then we set m = n − 1 m=n-1 m = n − 1 ϕ i \phi_i ϕ i  (10.6.8) 
ϕ i ( x ) = H i ( x ) = { x − t i − 1 h i if  x ∈ [ t i − 1 , t i ] , t i + 1 − x h i + 1 if  x ∈ [ t i , t i + 1 ] , 0 otherwise .   \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} ϕ i  ( x ) = H i  ( x ) = ⎩ ⎨ ⎧  h i  x − t i − 1   h i + 1  t i + 1  − x  0  if  x ∈ [ t i − 1  , t i  ] , if  x ∈ [ t i  , t i + 1  ] , otherwise .  Recall that these functions are cardinal, i.e., H i ( t i ) = 1 H_i(t_i)=1 H i  ( t i  ) = 1 H i ( t j ) = 0 H_i(t_j)=0 H i  ( t j  ) = 0 i ≠ j i\neq j i  = j 
u ( x ) = ∑ j = 1 m w j ϕ j ( x ) = ∑ j = 1 n − 1 u j H j ( x ) ,   u(x) = \sum_{j=1}^m w_j \phi_j(x) = \sum_{j=1}^{n-1} u_j H_j(x), u ( x ) = j = 1 ∑ m  w j  ϕ j  ( x ) = j = 1 ∑ n − 1  u j  H j  ( x ) , where, as usual, u j u_j u j  t j t_j t j  H 0 H_0 H 0  H n H_n H n  u u u 
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) I k = [ t k − 1 , t k ] I_k=[t_{k-1},t_k] I k  = [ t k − 1  , t k  ] 
K i j = ∑ k = 1 n [ ∫ I k c ( x ) H i ′ ( x ) H j ′ ( x )   d x ] , i , j = 1 , … , n − 1 , M i j = ∑ k = 1 n [ ∫ I k s ( x ) H i ( x ) H j ( x )   d x ] , i , j = 1 , … , n − 1 , f i = ∑ k = 1 n [ ∫ I k f ( x ) H i ( x )   d x ] i = 1 , … , n − 1.     \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} K ij  M ij  f i   = k = 1 ∑ n  [ ∫ I k   c ( x ) H i ′  ( x ) H j ′  ( x ) d x ] , i , j = 1 , … , n − 1 , = k = 1 ∑ n  [ ∫ I k   s ( x ) H i  ( x ) H j  ( x ) d x ] , i , j = 1 , … , n − 1 , = k = 1 ∑ n  [ ∫ I k   f ( x ) H i  ( x ) d x ] i = 1 , … , n − 1.  Start with the first subinterval, I 1 I_1 I 1  I 1 I_1 I 1  H 1 ( x ) H_1(x) H 1  ( x ) I 1 I_1 I 1  i = j = 1 i=j=1 i = j = 1 
∫ I 1 c ( x ) H 1 ′ ( x ) H 1 ′ ( x )   d x , ∫ I 1 s ( x ) H 1 ( x ) H 1 ( x )   d x , ∫ I 1 f ( x ) H 1 ( x )   d x , \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, ∫ I 1   c ( x ) H 1 ′  ( x ) H 1 ′  ( x ) d x , ∫ I 1   s ( x ) H 1  ( x ) H 1  ( x ) d x , ∫ I 1   f ( x ) H 1  ( x ) d x , which contribute to the sums for K 11 K_{11} K 11  M 11 M_{11} M 11  f 1 f_1 f 1  
Before writing more formulas, we make one more very useful simplification. Unless the coefficient functions c ( x ) c(x) c ( x ) s ( x ) s(x) s ( x ) f ( x ) f(x) f ( x ) 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 I k I_k I k  
c ( x ) ≈ c ‾ k = c ( t k − 1 ) + c ( t k ) 2 for  x ∈ I k . c(x) \approx \overline{c}_k = \frac{c(t_{k-1})+c(t_k)}{2} \quad \text{for $x\in I_k$}. c ( x ) ≈ c k  = 2 c ( t k − 1  ) + c ( t k  )  for  x ∈ I k  . Thus, the integrals in (10.6.16) 
∫ I 1 c ( x ) H 1 ′ ( x ) H 1 ′ ( x )   d x ≈ c ‾ 1 ∫ t 0 t 1 h 1 − 2   d x = c ‾ 1 h 1 . \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}. ∫ I 1   c ( x ) H 1 ′  ( x ) H 1 ′  ( x ) d x ≈ c 1  ∫ t 0  t 1   h 1 − 2  d x = h 1  c 1   . Now consider interval I 2 = [ t 1 , t 2 ] I_2=[t_1,t_2] I 2  = [ t 1  , t 2  ] H 1 H_1 H 1  H 2 H_2 H 2  K 11 K_{11} K 11  K 12 = K 21 K_{12}=K_{21} K 12  = K 21  K 22 K_{22} K 22  M 11 M_{11} M 11  M 12 = M 21 M_{12}=M_{21} M 12  = M 21  M 22 M_{22} M 22  f 1 f_1 f 1  f 2 f_2 f 2  I 2 I_2 I 2  H 2 ′ = h 2 − 1 H_2'= h_2^{-1} H 2 ′  = h 2 − 1  H 1 ′ = − h 2 − 1 H_{1}'= -h_2^{-1} H 1 ′  = − h 2 − 1  K 11 K_{11} K 11  K 22 K_{22} K 22  (10.6.16) c ‾ 2 / h 2 \overline{c}_2/h_2 c 2  / h 2  K 12 = K 21 K_{12}=K_{21} K 12  = K 21  − c ‾ 2 / h 2 -\overline{c}_2/h_2 − c 2  / h 2  
c ‾ k h k [ 1 − 1 − 1 1 ] ⇝ [ K 11 K 12 K 21 K 22 ] , \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}, h k  c k   [ 1 − 1  − 1 1  ] ⇝ [ K 11  K 21   K 12  K 22   ] , where the squiggly arrow is meant to show that the values of the 2 × 2 2\times 2 2 × 2 K \mathbf{K} K M \mathbf{M} M f \mathbf{f} f (10.6.16) 
In general, over I k I_k I k  1 < k < n 1<k<n 1 < k < n H k ′ = h k − 1 H_k'= h_k^{-1} H k ′  = h k − 1  H k − 1 ′ = − h k − 1 H_{k-1}'=-h_k^{-1} H k − 1 ′  = − h k − 1  I k I_k I k  
c ‾ k h k [ 1 − 1 − 1 1 ] ⇝ [ K k − 1 , k − 1 K k − 1 , k K k , k − 1 K k , k ] , k = 2 , … , n − 1.   \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. h k  c k   [ 1 − 1  − 1 1  ] ⇝ [ K k − 1 , k − 1  K k , k − 1   K k − 1 , k  K k , k   ] , k = 2 , … , n − 1. One finds the contributions to the other structures by similar computations:
s ‾ k h k 6 [ 2 1 1 2 ] ⇝ [ M k − 1 , k − 1 M k − 1 , k M k , k − 1 M k , k ] , k = 2 , … , n − 1 ,   \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, 6 s k  h k   [ 2 1  1 2  ] ⇝ [ M k − 1 , k − 1  M k , k − 1   M k − 1 , k  M k , k   ] , k = 2 , … , n − 1 , and
f ‾ k h k 2 [ 1 1 ] ⇝ [ f k − 1 f k ] , k = 2 , … , n − 1.   \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. 2 f  k  h k   [ 1 1  ] ⇝ [ f k − 1  f k   ] , k = 2 , … , n − 1. The contribution from I n I_n I n  K n − 1 , n − 1 K_{n-1,n-1} K n − 1 , n − 1  M n − 1 , n − 1 M_{n-1,n-1} M n − 1 , n − 1  f n − 1 f_{n-1} f n − 1  I 1 I_1 I 1  
Each I k I_k I k  f \mathbf{f} f I 1 I_1 I 1  I n I_n I n  f \mathbf{f} f finite element method  (FEM).  Putting together all the contributions to (10.6.12) assembly  process.
10.6.4 Implementation and convergence ¶ Function 10.6.1 (10.6.4) 
BVP 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 """
    fem(c, s, f, a, b, n)
Use a piecewise linear finite element method to solve a two-point
boundary value problem. The ODE is (`c`(x)u')' + `s`(x)u = `f`(x) on
the interval [`a`,`b`], and the boundary values are zero. The
discretization uses `n` equal subintervals.
Return vectors for the nodes and the values of u.
"""
function fem(c, s, f, a, b, n)
    # Define the grid.
    h = (b - a) / n
    x = @. a + h * (0:n)
    # Templates for the subinterval matrix and vector contributions.
    Ke = [1 -1; -1 1]
    Me = (1 / 6) * [2 1; 1 2]
    fe = (1 / 2) * [1; 1]
    # Evaluate coefficent functions and find average values.
    cval = c.(x)
    cbar = (cval[1:n] + cval[2:n+1]) / 2
    sval = s.(x)
    sbar = (sval[1:n] + sval[2:n+1]) / 2
    fval = f.(x)
    fbar = (fval[1:n] + fval[2:n+1]) / 2
    # Assemble global system, one interval at a time.
    K = zeros(n - 1, n - 1)
    M = zeros(n - 1, n - 1)
    f = zeros(n - 1)
    K[1, 1] = cbar[1] / h
    M[1, 1] = sbar[1] * h / 3
    f[1] = fbar[1] * h / 2
    K[n-1, n-1] = cbar[n] / h
    M[n-1, n-1] = sbar[n] * h / 3
    f[n-1] = fbar[n] * h / 2
    for k in 2:n-1
        K[k-1:k, k-1:k] += (cbar[k] / h) * Ke
        M[k-1:k, k-1:k] += (sbar[k] * h) * Me
        f[k-1:k] += (fbar[k] * h) * fe
    end
    # Solve system for the interior values.
    u = (K + M) \ f
    u = [0; u; 0]      # put the boundary values into the result
    return x, u
endBVP 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 function [x, u] = fem(c, s, f, a, b, n)
    %FEM     Piecewise linear finite elements for a linear BVP.
    % Input:
    %   c,s,f    coefficient functions of x describing the ODE (functions) 
    %   a,b      domain of the independent variable (scalars)
    %   n        number of grid subintervals (scalar) 
    % Output:
    %   x        grid points (vector, length n+1)
    %   u        solution values at x (vector, length n+1)
    % Define the grid.
    h = (b - a)/n;
    x = a + h * (0:n)';  
    % Templates for the subinterval matrix and vector contributions.
    Ke = [1 -1; -1 1];
    Me = (1/6) * [2 1; 1 2];
    fe = (1/2) * [1; 1];
    % Evaluate coefficent functions and find average values.
    cval = c(x);   cbar = (cval(1:n) + cval(2:n+1)) / 2;
    sval = s(x);   sbar = (sval(1:n) + sval(2:n+1)) / 2;
    fval = f(x);   fbar = (fval(1:n) + fval(2:n+1)) / 2;
    % Assemble global system, one interval at a time.
    K = zeros(n-1, n-1);  M = zeros(n-1, n-1);  f = zeros(n-1, 1);
    K(1, 1) = cbar(1) / h;
    M(1,1)  = sbar(1) * h / 3;  
    f(1)    = fbar(1) * h / 2;
    K(n-1, n-1) = cbar(n) / h;
    M(n-1, n-1) = sbar(n) * h / 3;
    f(n-1)      = fbar(n) * h / 2;
    for k = 2:n-1
        K(k-1:k, k-1:k) = K(k-1:k, k-1:k) + (cbar(k) / h) * Ke;
        M(k-1:k, k-1:k) = M(k-1:k, k-1:k) + (sbar(k) * h) * Me;
        f(k-1:k)        = f(k-1:k)        + (fbar(k) * h) * fe;
    end  
    % Solve system for the interior values.
    u = (K + M) \ f;
    u = [0; u; 0];      % put the boundary values into the result
endBVP 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 def fem(c, s, f, a, b, n):
    """
    fem(c, s, f, a, b, n)
    Use a piecewise linear finite element method to solve a two-point boundary
    value problem. The ODE is (c(x)u')' + s(x)u = f(x) on the interval
    [a,b], and the boundary values are zero. The discretization uses n equal
    subintervals.
    Return vectors for the nodes and the values of u.
    """
    # Define the grid.
    h = (b - a) / n
    x = np.linspace(a, b, n + 1)
    # Templates for the subinterval matrix and vector contributions.
    Ke = np.array([[1, -1], [-1, 1]])
    Me = (1 / 6) * np.array([[2, 1], [1, 2]])
    fe = (1 / 2) * np.array([1, 1])
    # Evaluate coefficent functions and find average values.
    cval = c(x)
    cbar = (cval[:-1] + cval[1:]) / 2
    sval = s(x)
    sbar = (sval[:-1] + sval[1:]) / 2
    fval = f(x)
    fbar = (fval[:-1] + fval[1:]) / 2
    # Assemble global system, one interval at a time.
    K = np.zeros([n - 1, n - 1])
    M = np.zeros([n - 1, n - 1])
    f = np.zeros(n - 1)
    K[0, 0] = cbar[0] / h
    M[0, 0] = sbar[0] * h / 3
    f[0] = fbar[0] * h / 2
    K[-1, -1] = cbar[-1] / h
    M[-1, -1] = sbar[-1] * h / 3
    f[-1] = fbar[-1] * h / 2
    for k in range(1, n - 1):
        K[k - 1 : k + 1, k - 1 : k + 1] += (cbar[k] / h) * Ke
        M[k - 1 : k + 1, k - 1 : k + 1] += (sbar[k] * h) * Me
        f[k - 1 : k + 1] += (fbar[k] * h) * fe
    # Solve system for the interior values.
    u = np.linalg.solve(K + M, f)
    u = np.hstack([0, u, 0])  # put the boundary values into the result
    return x, uExample 10.6.2  (Finite element solution of a 
BVP )
We solve the equation
− ( x 2 u ′ ) ′ + 4 y = sin  ( π x ) , u ( 0 ) = u ( 1 ) = 0 ,   -(x^2u')' + 4 y = \sin(\pi x), \qquad u(0)=u(1)=0, − ( x 2 u ′ ) ′ + 4 y = sin ( π x ) , u ( 0 ) = u ( 1 ) = 0 , in which
c ( x ) = x 2 , s ( x ) = 4 , f ( x ) = sin  ( π x ) . c(x) = x^2, \qquad s(x) = 4, \qquad f(x)=\sin(\pi x). c ( x ) = x 2 , s ( x ) = 4 , f ( x ) = sin ( π x ) . Example 10.6.2 Here are the coefficient function definitions. Even though s s s Function 10.6.1 
c = x -> x^2;
q = x -> 4;
f = x -> sin(π * x);
using Plots
x, u = FNC.fem(c, q, f, 0, 1, 50)
plot(x, u;
    xaxis=(L"x"),  yaxis = (L"u"),
    title = "Solution by finite elements", legend=:none)Example 10.6.2 Here are the coefficient function definitions. Even though s s s Function 10.6.1 
c = @(x) x.^2;
q = @(x) 4 * ones(size(x));
f = @(x) sin(pi * x);
[x,u] = fem(c, q, f, 0, 1, 50);
clf,  plot(x, u)
xlabel('x'),  ylabel('u')
title('Solution by finite elements')Example 10.6.2 Here are the coefficient function definitions. Even though s s s Function 10.6.1 
c = lambda x: x**2
q = lambda x: 4 * ones(len(x))
f = lambda x: sin(pi * x)
x, u = FNC.fem(c, q, f, 0, 1, 50)
plot(x, u)
xlabel("$x$"),  ylabel("$u$")
title("Solution by finite elements");Because piecewise linear interpolation on a uniform grid of size h h h O ( h 2 ) O(h^2) O ( h 2 ) 
10.6.5 Exercises ¶ ⌨ For each linear BVP , use Function 10.6.1 n = 40 n=40 n = 40 n = 10 , 20 , 40 , … , 640 n=10,20,40,\ldots,640 n = 10 , 20 , 40 , … , 640 n n n 
(a)  − u ′ ′ + u = − 8 + 16 x 2 − x 4 , u ( 0 ) = u ( 2 ) = 0 -u''+u=-8 + 16 x^2 - x^4, \quad u(0) =u(2) =0 − u ′′ + u = − 8 + 16 x 2 − x 4 , u ( 0 ) = u ( 2 ) = 0 
Exact solution: x 2 ( 4 − x 2 ) x^2(4-x^2) x 2 ( 4 − x 2 ) 
(b)  [ ( 2 + x ) u ′ ] ′ + 11 x u = − e x ( 12 x 3 + 7 x 2 + 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 [( 2 + x ) u ′ ] ′ + 11 xu = − e x ( 12 x 3 + 7 x 2 + 1 ) , u ( − 1 ) = u ( 1 ) = 0 
Exact solution: e x ( 1 − x 2 ) e^x \left(1-x^2\right) e x ( 1 − x 2 ) 
(c)  u ′ ′ + x ( u ′ + u ) = − x [ 4 sin  ( x ) + 5 x cos  ( x ) ] , u ( 0 ) = u ( 2 π ) = 0 u''+x(u'+u) = -x[4 \sin(x)+5 x \cos(x)], \quad u(0) =u(2\pi) =0 u ′′ + x ( u ′ + u ) = − x [ 4 sin ( x ) + 5 x cos ( x )] , u ( 0 ) = u ( 2 π ) = 0 
Exact solution: − x 2 sin  ( x ) -x^2\sin(x) − x 2 sin ( x ) 
✍  Suppose you want to solve the differential equation − [ c ( x ) u ′ ] ′ + d ( x ) u = f ( x ) -[c(x)u']'+d(x)u = f(x) − [ c ( x ) u ′ ] ′ + d ( x ) u = f ( x ) (10.6.2) u ( a ) = α u(a)=\alpha u ( a ) = α u ( b ) = β u(b)=\beta u ( b ) = β p p p q q q v ( x ) = u ( x ) + p x + q v(x)=u(x)+px+q v ( x ) = u ( x ) + p x + q v v v BVP , except that v ( a ) = v ( b ) = 0 v(a)=v(b)=0 v ( a ) = v ( b ) = 0 f f f 
✍  Suppose p ( x ) u ′ ′ ( x ) + q ( x ) u ′ ( x ) + r ( x ) = 0 p(x)u''(x)+q(x)u'(x)+r(x)=0 p ( x ) u ′′ ( x ) + q ( x ) u ′ ( x ) + r ( x ) = 0 p ( x ) ≠ 0 p(x)\neq 0 p ( x )  = 0 x x x [ a , b ] [a,b] [ a , b ] z ( x ) z(x) z ( x ) z ′ = q / p z'=q/p z ′ = q / p (10.6.2) c ( x ) c(x) c ( x ) d ( x ) d(x) d ( x ) f ( x ) f(x) f ( x ) exp  ( z ) \exp(z) exp ( z ) 
Suppose the Dirichlet boundary conditions u ( a ) = u ( b ) = 0 u(a)=u(b)=0 u ( a ) = u ( b ) = 0 u ′ ( a ) = u ′ ( b ) = 0 u'(a)=u'(b)=0 u ′ ( a ) = u ′ ( b ) = 0 
(a)  ✍ Explain why the weak form (10.6.4) ψ \psi ψ 
(b)  ⌨ The result of part (a) suggests replacing (10.6.15) 
u ( x ) = ∑ j = 0 n u j H j ( x ) u(x) = \sum_{j=0}^{n} u_j H_j(x) u ( x ) = j = 0 ∑ n  u j  H j  ( x ) and making (10.6.16) i , j i,j i , j n n n Function 10.6.1 I 1 I_1 I 1  I n I_n I n  
(c)  ⌨ Test your function on the problem
u ′ ′ + u = − 2 sin  ( x ) , u ′ ( 0 ) = u ′ ( 1 ) = 0 , u''+u = -2 \sin(x), \quad u'(0)=u'(1)=0, u ′′ + u = − 2 sin ( x ) , u ′ ( 0 ) = u ′ ( 1 ) = 0 , whose exact solution is ( x − 1 ) cos  ( x ) − sin  ( x ) (x-1)\cos(x) - \sin(x) ( x − 1 ) cos ( x ) − sin ( x )