```
```{raw} latex
%%start demo%%
```
We solve linear BVP
$$ u'' - (\cos x) u' + (\sin x) u = 0, \quad u(0)=1, \; u\left(\frac{3\pi}{2}\right)=\frac{1}{e}. $$
Its exact solution is known:
```{code-cell}
exact = x -> exp(sin(x));
```
The problem is presented above in our standard form, so we can identify the coefficient functions in the ODE. Each should be coded as a function.
```{code-cell}
p = x -> -cos(x);
q = sin;
r = x -> 0; # function, not value
```
We solve the BVP and compare the result to the exact solution.
```{code-cell}
x,u = FNC.bvplin(p,q,r,[0,3π/2],1,exp(-1),30);
```
```{code-cell}
:tags: [hide-input]
plot(exact,0,3π/2,layout=(2,1),label="exact")
scatter!(x,u,m=:o,subplot=1,label="numerical",
yaxis=("solution"),title="Solution of a linear BVP")
plot!(x,exact.(x)-u,subplot=2,xaxis=L"x",yaxis=("error"))
```
```{raw} html

```
```{raw} latex
%%end demo%%
```
## Accuracy and stability
We revisit {numref}`Demo %s
```
```{raw} latex
%%start demo%%
```
The BVP is
$$
u'' - \lambda^2 u = \lambda^2, \quad u(0)=-1, \; u(1)=0,
$$
with exact solution $\sinh(\lambda x)/\sinh(\lambda) - 1$.
```{code-cell}
λ = 10
exact = x -> sinh(λ*x)/sinh(λ) - 1;
```
The following functions define the ODE.
```{code-cell}
p = x -> 0
q = x -> -λ^2
r = x -> λ^2;
```
We compare the computed solution to the exact one for increasing $n$.
```{code-cell}
n = 5*[round(Int,10^d) for d in 0:.25:3]
err = zeros(size(n))
for (k,n) in enumerate(n)
x,u = FNC.bvplin(p,q,r,[0,1],-1,0,n)
err[k] = norm(exact.(x)-u,Inf)
end
data = (n=n[1:4:end],err=err[1:4:end])
pretty_table(data,["n","max-norm error"])
```
Each factor of 10 in $n$ reduces error by a factor of 100, which is indicative of second-order convergence.
```{code-cell}
plot(n,err,m=:o,label="observed",
xaxis=(:log10,L"n"), yaxis=(:log10,"max-norm error"),
title="Convergence for a linear BVP")
plot!(n,0.25*n.^(-2),l=(:dash,:gray),label="2nd order")
```
```{raw} html

```
```{raw} latex
%%end demo%%
```
If we write the solution $\mathbf{u}$ of Equation {eq}`fdlinbc` as the exact solution minus an error vector $\mathbf{e}$, i.e., $\mathbf{u} = \hat{\mathbf{u}} - \mathbf{e}$, we obtain
\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*}
```{index} stability; of collocation
```
where $\boldsymbol{\tau}$ is the truncation error of the finite differences (except at the boundary rows, where it is zero). It follows that $\|\mathbf{e}\|$ vanishes at the same rate as the truncation error if $\| \mathbf{A}^{-1}\|$ is bounded above as $h\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.
## Exercises
(problem-linear-fdlin1)=
1. ✍ For each boundary-value problem, verify that the given solution is correct. Then write out by hand for $n=3$ the matrices $\mathbf{D}_{xx}$, $\mathbf{D}_x$, $\mathbf{P}$, and $\mathbf{Q}$, and the vector $\mathbf{r}$.
**(a)** $u'' + u = 0, \quad u(0) =0, \; u(3) = \sin 3$
Solution: $u(x) = \sin x$
**(b)** $u'' - \frac{3}{x} u' + \frac{4}{x^2} u = 0, \quad u(1) =0,\; u(4) = 32 \log 2$
Solution: $u(x) = x^2 \log x$
**(c)**
$u'' - \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) = \left(x+\frac{1}{2}\right)^{-2}$
(problem-linear-fdlin2)=
2. ⌨ For each of the cases in the previous exercise, use {numref}`Function {number}