The first step is to select nodes x0=a<x1<⋯<xn=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:
where u^ is the exact solution of (10.4.1). If we so desire, we can use interpolation to convert the values (xi,ui) into a function after the solution is found.
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,
which is a linear system of n+1 equations in n+1 unknowns.
We have not yet incorporated the boundary conditions. Those take the form of the additional linear conditions u0=α and un=β. 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) identity with the first and last rows deleted:
where as always ek is the kth column (here starting from k=0) of an identity matrix. The product EA deletes the first and last rows of A, leaving a matrix that is (n−1)×(n+1). Similarly, Er deletes the first and last rows of r.
Finally, we note that u^(a)=e0Tu and u^(b)=enTu, so the linear system including both the ODE and the boundary condition collocations is
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.
where τ is the truncation error of the finite differences (except at the boundary rows, where it is zero). It follows that ∥e∥ vanishes at the same rate as the truncation error if ∥A−1∥ is bounded above as h→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.
✍ For each boundary-value problem, verify that the given solution is correct. Then write out by hand for n=3 the matrices Dxx, Dx, P, and Q, and the vector r.
⌨ For each of the cases in the previous exercise, use Function 10.4.1 to solve the problem with n=60 and make a plot of its error as a function of x. Then, for each n=10,20,40,…,640, find the infinity norm of the error. Make a log-log plot of error versus n and include a graphical comparison to second-order convergence.
⌨ 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,…,40, finding the infinity norm of the error in each case. Make a log-linear plot of error versus n.
⌨ The Airy equation is u′′=xu. Its solution is exponential for x>0 and oscillatory for x<0. The exact solution is given by u=c1Ai(x)+c2Bi(x), where Ai and Bi are Airy functions. In Julia they are computed by airyai and airybi, respectively.
(a) Suppose that u(−10)=−1, u(2)=1. By setting up and solving a 2×2 linear system, find numerical values for c1 and c2. Plot the resulting exact solution.
(b) Use Function 10.4.1 with n=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=800.
Consider the boundary-value problem ϵu′′+(1+ϵ)u′+u=0 over x∈(0,1), with u(0)=0, u(1)=1. As the parameter ε is decreased, the solution gets a thin region of high activity near x=0 called a boundary layer.
(a) ✍ Verify that the exact solution to the problem is
(b) ⌨ Define N(ϵ) as the smallest integer value of n needed to make the max-norm error of the result of Function 10.4.1 less than 10-4. For each of the values ϵ=21,41,81…,641, estimate N(ϵ) by starting with n=50 and incrementing by 25 until the measured error is sufficiently small.
(c) ⌨ Plot the error as a function of x for ϵ=641 and n=N(ϵ). Compare the peak of the error to the graph from part (a).
(d) ⌨ Develop a hypothesis for the leading-order behavior of N(ϵ). Plot the observed N(ϵ) and your hypothesis together on a log-log plot.
(e) ✍ Finite-difference errors depend on the solution as well as on n. Given that this error decreases as O(n−2), what does your hypothesis for N(ϵ) suggest about the behavior of the error for fixed n as ϵ→0?