7.3: Numerical Methods - Boundary Value Problem
- Page ID
- 96066
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)7.3.1. Finite difference method
We consider first the differential equation
\[-\frac{d^{2} y}{d x^{2}}=f(x), \quad 0 \leq x \leq 1 \nonumber \]
with two-point boundary conditions
\[y(0)=A, \quad y(1)=B \text {. } \nonumber \]
Equation (7.8) can be solved by quadrature, but here we will demonstrate a numerical solution using a finite difference method.
We begin by discussing how to numerically approximate derivatives. Consider the Taylor series approximation for \(y(x+h)\) and \(y(x-h)\), given by
\[\begin{aligned} &y(x+h)=y(x)+h y^{\prime}(x)+\frac{1}{2} h^{2} y^{\prime \prime}(x)+\frac{1}{6} h^{3} y^{\prime \prime \prime}(x)+\frac{1}{24} h^{4} y^{\prime \prime \prime \prime}(x)+\ldots, \\ &y(x-h)=y(x)-h y^{\prime}(x)+\frac{1}{2} h^{2} y^{\prime \prime}(x)-\frac{1}{6} h^{3} y^{\prime \prime \prime}(x)+\frac{1}{24} h^{4} y^{\prime \prime \prime \prime}(x)+\ldots \end{aligned} \nonumber \]
The standard definitions of the derivatives give the first-order approximations
\[\begin{aligned} &y^{\prime}(x)=\frac{y(x+h)-y(x)}{h}+\mathrm{O}(h) \\ &y^{\prime}(x)=\frac{y(x)-y(x-h)}{h}+\mathrm{O}(h) \end{aligned} \nonumber \]
The more widely-used second-order approximation is called the central difference approximation and is given by
\[y^{\prime}(x)=\frac{y(x+h)-y(x-h)}{2 h}+\mathrm{O}\left(h^{2}\right) \nonumber \]
The finite difference approximation to the second derivative can be found from considering
\[y(x+h)+y(x-h)=2 y(x)+h^{2} y^{\prime \prime}(x)+\frac{1}{12} h^{4} y^{\prime \prime \prime \prime}(x)+\ldots, \nonumber \]
from which we find
\[y^{\prime \prime}(x)=\frac{y(x+h)-2 y(x)+y(x-h)}{h^{2}}+\mathrm{O}\left(h^{2}\right) \text {. } \nonumber \]
Sometimes a second-order method is required for \(x\) on the boundaries of the domain. For a boundary point on the left, a second-order forward difference method requires the additional Taylor series
\[y(x+2 h)=y(x)+2 h y^{\prime}(x)+2 h^{2} y^{\prime \prime}(x)+\frac{4}{3} h^{3} y^{\prime \prime \prime}(x)+\ldots \nonumber \]
We combine the Taylor series for \(y(x+h)\) and \(y(x+2 h)\) to eliminate the term proportional to \(h^{2}\) :
\[y(x+2 h)-4 y(x+h)=-3 y(x)-2 h y^{\prime}(x)+\mathrm{O}\left(\mathrm{h}^{3}\right) \nonumber \]
Therefore,
\[y^{\prime}(x)=\frac{-3 y(x)+4 y(x+h)-y(x+2 h)}{2 h}+\mathrm{O}\left(h^{2}\right) \nonumber \]
For a boundary point on the right, we send \(h \rightarrow-h\) to find
\[y^{\prime}(x)=\frac{3 y(x)-4 y(x-h)+y(x-2 h)}{2 h}+\mathrm{O}\left(h^{2}\right) \nonumber \]
We now write a finite difference scheme to solve (7.8). We discretize \(x\) by defining \(x_{i}=i h, i=0,1, \ldots, n+1\). Since \(x_{n+1}=1\), we have \(h=1 /(n+1)\). The functions \(y(x)\) and \(f(x)\) are discretized as \(y_{i}=y\left(x_{i}\right)\) and \(f_{i}=f\left(x_{i}\right) .\) The second-order differential equation (7.8) then becomes for the interior points of the domain
\[-y_{i-1}+2 y_{i}-y_{i+1}=h^{2} f_{i}, \quad i=1,2, \ldots n, \nonumber \]
with the boundary conditions \(y_{0}=A\) and \(y_{n+1}=B\). We therefore have a linear system of equations to solve. The first and \(n\)th equation contain the boundary conditions and are given by
\[\begin{aligned} 2 y_{1}-y_{2} &=h^{2} f_{1}+A \\ -y_{n-1}+2 y_{n} &=h^{2} f_{n}+B \end{aligned} \nonumber \]
The second and third equations, etc., are
\[\begin{aligned} &-y_{1}+2 y_{2}-y_{3}=h^{2} f_{2} \\ &-y_{2}+2 y_{3}-y_{4}=h^{2} f_{3} \end{aligned} \nonumber \]
In matrix form, we have
\[\left(\begin{array}{rrrrrrrr} 2 & -1 & 0 & 0 & \cdots & 0 & 0 & 0 \\ -1 & 2 & -1 & 0 & \cdots & 0 & 0 & 0 \\ 0 & -1 & 2 & -1 & \cdots & 0 & 0 & 0 \\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots \\ 0 & 0 & 0 & 0 & \cdots & -1 & 2 & -1 \\ 0 & 0 & 0 & 0 & \cdots & 0 & -1 & 2 \end{array}\right)\left(\begin{array}{c} y_{1} \\ y_{2} \\ y_{3} \\ \vdots \\ y_{n-1} \\ y_{n} \end{array}\right)=\left(\begin{array}{c} h^{2} f_{1}+A \\ h^{2} f_{2} \\ h^{2} f_{3} \\ \vdots \\ h^{2} f_{n-1} \\ h^{2} f_{n}+B \end{array}\right) \nonumber \]
The matrix is tridiagonal, and a numerical solution by Guassian elimination can be done quickly. The matrix itself is easily constructed using the MATLAB function diag.m and ones.m. As excerpted from the MATLAB help page, the function call ones \((\mathrm{m}, \mathrm{n})\) returns an m-by-n matrix of ones, and the function call \(\operatorname{diag}(\mathrm{v}, \mathrm{k})\), when \(\mathrm{v}\) is a vector with \(\mathrm{n}\) components, is a square matrix of order \(\mathrm{n}+\mathrm{abs}(\mathrm{k})\) with the elements of \(\mathrm{v}\) on the \(\mathrm{k}\)-th diagonal: \(k=0\) is the main diagonal, \(k>0\) is above the main diagonal and \(k<0\) is below the main diagonal. The \(n \times n\) matrix above can be constructed by the MATLAB code
\[M=\operatorname{diag}(-\text { ones }(n-1,1),-1)+\operatorname{diag}\left(2^{*} \text { ones }(n, 1), 0\right)+\operatorname{diag}(-\text { ones }(n-1,1), 1) ; \nonumber \]
The right-hand-side, provided \(\mathrm{f}\) is a given n-by-1 vector, can be constructed by the MATLAB code
\[\mathrm{b}=\mathrm{h}^{\wedge} 2^{*} \mathrm{f} ; \mathrm{b}(1)=\mathrm{b}(1)+\mathrm{A} ; \mathrm{b}(\mathrm{n})=\mathrm{b}(\mathrm{n})+\mathrm{B} \nonumber \]
and the solution for \(u\) is given by the MATLAB code
\[\mathrm{y}=\mathrm{M} \backslash \mathrm{b} \nonumber \]
7.3.2. Shooting method
The finite difference method can solve linear odes. For a general ode of the form
\[\frac{d^{2} y}{d x^{2}}=f(x, y, d y / d x) \nonumber \]
with \(y(0)=A\) and \(y(1)=B\), we use a shooting method. First, we formulate the ode as an initial value problem. We have
\[\begin{aligned} &\frac{d y}{d x}=z \\ &\frac{d z}{d x}=f(x, y, z) \end{aligned} \nonumber \]
The initial condition \(y(0)=A\) is known, but the second initial condition \(z(0)=b\) is unknown. Our goal is to determine \(b\) such that \(y(1)=B\).
In fact, this is a root-finding problem for an appropriately defined function. We define the function \(F=F(b)\) such that
\[F(b)=y(1)-B \nonumber \]
In other words, \(F(b)\) is the difference between the value of \(y(1)\) obtained from integrating the differential equations using the initial condition \(z(0)=b\), and \(B\). Our root-finding routine will want to solve \(F(b)=0\). (The method is called shooting because the slope of the solution curve for \(y=y(x)\) at \(x=0\) is given by \(b\), and the solution hits the value \(y(1)\) at \(x=1\). This looks like pointing a gun and trying to shoot the target, which is \(B\).)
To determine the value of \(b\) that solves \(F(b)=0\), we iterate using the Secant method, given by
\[b_{n+1}=b_{n}-F\left(b_{n}\right) \frac{b_{n}-b_{n-1}}{F\left(b_{n}\right)-F\left(b_{n-1}\right)} \nonumber \]
We need to start with two initial guesses for \(b\), solving the ode for the two corresponding values of \(y(1)\). Then the Secant Method will give us the next value of \(b\) to try, and we iterate until \(|y(1)-B|<\) tol, where tol is some specified tolerance for the error