Skip to article frontmatterSkip to article content

Collocation for linear problems

Let us now devise a numerical method based on finite differences for the linear TPBVP

u+p(x)u+q(x)u=r(x),u(a)=α,  u(b)=β. u'' + p(x)u' + q(x)u = r(x), \quad u(a)=\alpha,\; u(b)=\beta.

The first step is to select nodes x0=a<x1<<xn=bx_0=a < x_1< \cdots < x_n=b. For finite differences these will most likely be equally spaced, but for spectral differentiation they will be Chebyshev points.

Rather than solving for a function, we will solve for a vector of its approximate values at the nodes:

u=[u0u1un1un][u^(x0)u^(x1)u^(xn1)u^(xn)], \mathbf{u} = \begin{bmatrix} u_0 \\ u_1 \\ \vdots \\ u_{n-1} \\ u_n \end{bmatrix} \approx \begin{bmatrix} \hat{u}(x_0) \\ \hat{u}(x_1) \\ \vdots \\ \hat{u}(x_{n-1}) \\ \hat{u}(x_{n}) \end{bmatrix},

where u^\hat{u} is the exact solution of (10.4.1). If we so desire, we can use interpolation to convert the values (xi,ui)(x_i,u_i) into a function after the solution is found.

10.4.1Collocation

Having defined values at the nodes as our unknowns, we impose approximations to the ODE at the same nodes. This approach is known as collocation. Derivatives of the solution are found using differentiation matrices. For example,

[u^(x0)u^(x1)u^(xn)]u=Dxu, \begin{bmatrix} \hat{u}'(x_0) \\[1mm] \hat{u}'(x_1) \\ \vdots \\ \hat{u}'(x_n) \end{bmatrix} \approx \mathbf{u}' = \mathbf{D}_x \mathbf{u},

with an appropriately chosen differentiation matrix Dx\mathbf{D}_x. Similarly, we define

[u^(x0)u^(x1)u^(xn)]u=Dxxu, \begin{bmatrix} \hat{u}''(x_0) \\[1mm] \hat{u}''(x_1) \\ \vdots \\ \hat{u}''(x_n) \end{bmatrix} \approx \mathbf{u}'' = \mathbf{D}_{xx} \mathbf{u},

with Dxx\mathbf{D}_{xx} chosen in accordance with the node set.

The discrete form of (10.4.1) at the n+1n+1 chosen nodes is

u+Pu+Qu=r, \mathbf{u}'' + \mathbf{P}\mathbf{u}' + \mathbf{Q}\mathbf{u} = \mathbf{r},

where

P=[p(x0)p(xn)],Q=[q(x0)q(xn)],r=[r(x0)r(xn)]. \begin{split} \mathbf{P} &= \begin{bmatrix} p(x_0) & & \\ & \ddots & \\ & & p(x_{n}) \end{bmatrix}, \qquad \mathbf{Q} = \begin{bmatrix} q(x_0) & & \\ & \ddots & \\ & & q(x_{n}) \end{bmatrix},\\ \mathbf{r} &= \begin{bmatrix} r(x_0) \\ \vdots \\ r(x_n) \end{bmatrix}. \end{split}

If we apply the definitions of u\mathbf{u}' and u\mathbf{u}'' and rearrange, we obtain

Lu=r,L=Dxx+PDx+Q, \mathbf{L} \mathbf{u} = \mathbf{r}, \qquad \mathbf{L} = \mathbf{D}_{xx} + \mathbf{P}\mathbf{D}_x + \mathbf{Q},

which is a linear system of n+1n+1 equations in n+1n+1 unknowns.

We have not yet incorporated the boundary conditions. Those take the form of the additional linear conditions u0=αu_0=\alpha and un=βu_n=\beta. We might regard this situation as an overdetermined system, suitable for linear least-squares. However, it’s usually preferred to impose the boundary conditions and collocation conditions exactly, so we need to discard two of the collocation equations to keep the system square. The obvious candidates for deletion are the collocation conditions at the two endpoints. We may express these deletions by means of a matrix that is an (n+1)×(n+1)(n+1)\times(n+1) identity with the first and last rows deleted:

E=[01000010]=[e1Ten1T], \mathbf{E} = \begin{bmatrix} 0 & 1 & 0 & \cdots & 0 \\ \vdots & & \ddots & & \vdots \\ 0 & \cdots & 0 & 1 & 0 \end{bmatrix} = \begin{bmatrix} \mathbf{e}_1^T \\ \vdots \\ \mathbf{e}_{n-1}^T \end{bmatrix},

where as always ek\mathbf{e}_k is the kkth column (here starting from k=0k=0) of an identity matrix. The product EA\mathbf{E} \mathbf{A} deletes the first and last rows of A\mathbf{A}, leaving a matrix that is (n1)×(n+1)(n-1)\times(n+1). Similarly, Er\mathbf{E}\mathbf{r} deletes the first and last rows of r\mathbf{r}.

Finally, we note that u^(a)=e0Tu\hat{u}(a)= \mathbf{e}_0^T\mathbf{u} and u^(b)=enTu\hat{u}(b)= \mathbf{e}_n^T\mathbf{u}, so the linear system including both the ODE and the boundary condition collocations is

[e0TELenT]u=[αErβ]orAu=b. \begin{bmatrix} \mathbf{e}_0^T \\[1mm] \mathbf{E}\mathbf{L} \\[1mm] \mathbf{e}_n^T \end{bmatrix} \mathbf{u} = \begin{bmatrix} \alpha \\[1mm] \mathbf{E}\mathbf{r} \\[1mm] \beta \end{bmatrix} \qquad \text{or} \qquad \mathbf{A}\mathbf{u} = \mathbf{b}.

10.4.2Implementation

Our implementation of linear collocation is Function 10.4.1. It uses second-order finite differences but makes no attempt to exploit the sparsity of the matrices. It would be trivial to change the function to use spectral differentiation.

10.4.3Accuracy and stability

We revisit Demo 10.2.3, which exposed instability in the shooting method, in order to verify second-order convergence.

If we write the solution u\mathbf{u} of Equation (10.4.9) as the exact solution minus an error vector e\mathbf{e}, i.e., u=u^e\mathbf{u} = \hat{\mathbf{u}} - \mathbf{e}, we obtain

Au^Ae=b,e=A1[Au^b]=A1τ(h),\begin{gather*} \mathbf{A} \hat{\mathbf{u}} - \mathbf{A} \mathbf{e} = \mathbf{b}, \\ \mathbf{e} = \mathbf{A}^{-1} \left[ \mathbf{A} \hat{\mathbf{u}} - \mathbf{b} \right] = \mathbf{A}^{-1} \boldsymbol{\tau}(h), \end{gather*}

where τ\boldsymbol{\tau} is the truncation error of the finite differences (except at the boundary rows, where it is zero). It follows that e\|\mathbf{e}\| vanishes at the same rate as the truncation error if A1\| \mathbf{A}^{-1}\| is bounded above as h0h\to 0. In the present context, this property is known as stability. Proving stability is too technical to walk through here, but stability is guaranteed under some reasonable conditions on the BVP.

10.4.4Exercises

  1. ✍ For each boundary-value problem, verify that the given solution is correct. Then write out by hand for n=3n=3 the matrices Dxx\mathbf{D}_{xx}, Dx\mathbf{D}_x, P\mathbf{P}, and Q\mathbf{Q}, and the vector r\mathbf{r}.

    (a) u+u=0,u(0)=0,  u(3)=sin3u'' + u = 0, \quad u(0) =0, \; u(3) = \sin 3

    Solution: u(x)=sinxu(x) = \sin x

    (b) u3xu+4x2u=0,u(1)=0,  u(4)=32log2u'' - \frac{3}{x} u' + \frac{4}{x^2} u = 0, \quad u(1) =0,\; u(4) = 32 \log 2

    Solution: u(x)=x2logxu(x) = x^2 \log x

    (c) u(x+12)1u+2(x+12)2u=10(x+12)4,u(x+12)=1,  u(x+52)=19u'' - \left(x+\frac{1}{2}\right)^{-1}\, u' + 2\left(x+\frac{1}{2}\right)^{-2}\, u = 10\left(x+\frac{1}{2}\right)^{-4}, \quad u\left(x+\frac{1}{2}\right)=1,\; u\left(x+\frac{5}{2}\right) = \frac{1}{9}

    Solution: u(x)=(x+12)2u(x) = \left(x+\frac{1}{2}\right)^{-2}

  2. ⌨ For each of the cases in the previous exercise, use Function 10.4.1 to solve the problem with n=60n=60 and make a plot of its error as a function of xx. Then, for each n=10,20,40,,640n=10,20,40,\ldots,640, find the infinity norm of the error. Make a log-log plot of error versus nn and include a graphical comparison to second-order convergence.

  3. ⌨ Modify Function 10.4.1 to use spectral differentiation rather than second-order finite differences. For each of the cases in Exercise 1, solve the problem with n=5,10,15,,40n=5,10,15,\ldots,40, finding the infinity norm of the error in each case. Make a log-linear plot of error versus nn.

  4. ⌨ Use Function 10.4.1 to solve Bessel’s equation,

    x2u+xu+x2y=0,u(0.5)=1,  u(8)=0.x^2 u'' + x u' + x^2 y = 0, \quad u(0.5)=1,\; u(8) = 0.

    Plot the solution for n=100n=100.

  5. ⌨ The Airy equation is u=xuu''=x u. Its solution is exponential for x>0x>0 and oscillatory for x<0x<0. The exact solution is given by u=c1Ai(x)+c2Bi(x)u=c_1 \operatorname{Ai}(x) + c_2 \operatorname{Bi}(x), where Ai and Bi are Airy functions. In Julia they are computed by airyai and airybi, respectively.

    (a) Suppose that u(10)=1u(-10) =-1, u(2)=1u(2) =1. By setting up and solving a 2×22\times 2 linear system, find numerical values for c1c_1 and c2c_2. Plot the resulting exact solution.

    (b) Use Function 10.4.1 with n=120n=120 to find the solution with the boundary conditions in part (a). In a 2-by-1 subplot array, plot the finite-difference solution and its error. (The solution is not very accurate.)

    (c) Repeat part (b) with n=800n=800.

  6. Consider the boundary-value problem ϵu+(1+ϵ)u+u=0\epsilon u''+(1+\epsilon)u'+u =0 over x(0,1)x\in (0,1), with u(0)=0u(0)=0, u(1)=1u(1)=1. As the parameter ε is decreased, the solution gets a thin region of high activity near x=0x=0 called a boundary layer.

    (a) ✍ Verify that the exact solution to the problem is

    u(x)=exex/ϵe1e1/ϵ.u(x) = \frac{e^{-x}-e^{-x/\epsilon}}{e^{\,-1}-e^{\,-1/\epsilon}}.

    On one graph, plot u(x)u(x) for ϵ=14,116,164\epsilon=\frac{1}{4},\frac{1}{16},\frac{1}{64}.

    (b) ⌨ Define N(ϵ)N(\epsilon) as the smallest integer value of nn needed to make the max-norm error of the result of Function 10.4.1 less than 10-4. For each of the values ϵ=12,14,18,164\epsilon = \frac{1}{2},\frac{1}{4},\frac{1}{8}\ldots,\frac{1}{64}, estimate N(ϵ)N(\epsilon) by starting with n=50n=50 and incrementing by 25 until the measured error is sufficiently small.

    (c) ⌨ Plot the error as a function of xx for ϵ=164\epsilon=\frac{1}{64} and n=N(ϵ)n=N(\epsilon). Compare the peak of the error to the graph from part (a).

    (d) ⌨ Develop a hypothesis for the leading-order behavior of N(ϵ)N(\epsilon). Plot the observed N(ϵ)N(\epsilon) and your hypothesis together on a log-log plot.

    (e) ✍ Finite-difference errors depend on the solution as well as on nn. Given that this error decreases as O(n2)O(n^{-2}), what does your hypothesis for N(ϵ)N(\epsilon) suggest about the behavior of the error for fixed nn as ϵ0\epsilon\to 0?