```
```{raw} latex
%%start demo%%
```
Suppose a damped pendulum satisfies the nonlinear equation $\theta'' + 0.05\theta'+\sin \theta =0$. We want to start the pendulum at $\theta=2.5$ and give it the right initial velocity so that it reaches $\theta=-2$ at exactly $t=5$. This is a boundary-value problem with Dirichlet conditions $\theta(0)=2.5$ and $\theta(5)=-2$.
The first step is to define the function $\phi$ that equals $\theta''$.
```{code-cell}
ϕ = (t,θ,ω) -> -0.05*ω - sin(θ);
```
Next, we define the boundary conditions.
```{code-cell}
g₁(u,du) = u - 2.5
g₂(u,du) = u + 2;
```
```{index} ! Julia; collect
```
::::{panels}
:column: col-7 left-side
:card: border-0 shadow-none
```{raw} latex
\begin{minipage}[t]{0.5\textwidth}
```
The last ingredient is an initial estimate of the solution. Here we choose $n=100$ and a linear function between the endpoint values.
```{raw} latex
\end{minipage}\hfill
```
---
:column: col-5 right-side
:card: shadow-none comment
```{raw} latex
\begin{minipage}[t]{0.4\textwidth}\begin{mdframed}[default]\small
```
The `collect` function turns a range object into a true vector.
```{raw} latex
\end{mdframed}\end{minipage}
```
::::
```{code-cell}
init = collect( range(2.5,-2,length=101) );
```
We find a solution with negative initial slope, i.e., the pendulum is initially pushed back toward equilibrium.
```{code-cell}
t,θ = FNC.bvp(ϕ,[0,5],g₁,g₂,init)
plot(t,θ,xaxis=(L"t"),yaxis=(L"\theta(t)"),
title="Pendulum over [0,5]")
```
If we extend the time interval longer for the same boundary values, then the initial slope must adjust.
```{code-cell}
t,θ = FNC.bvp(ϕ,[0,8],g₁,g₂,init)
plot(t,θ,xaxis=(L"t"),yaxis=(L"\theta(t)"),
title="Pendulum over [0,8]")
```
This time, the pendulum is initially pushed toward the unstable equilibrium in the upright vertical position before gravity pulls it back down.
```{raw} html

```
```{raw} latex
%%end demo%%
```
The initial solution estimate can strongly influence how quickly a solution is found, or whether the quasi-Newton iteration converges at all. In situations where multiple solutions exist, the initialization can determine which is found.
(demo-nonlinear-mems)=
```{proof:demo}
```
```{raw} html
```
```{raw} latex
%%start demo%%
```
We look for a solution to the parameterized membrane deflection problem from {numref}`Example {number} `,
$$
w''+ \frac{1}{r}w'= \frac{\lambda}{w^2},\quad w'(0)=0,\; w(1)=1.
$$
Here is the problem definition. We use a truncated domain to avoid division by zero at $r=0$.
```{code-cell}
domain = [eps(),1]
λ = 0.5
ϕ = (r,w,dwdr) -> λ/w^2 - dwdr/r
g₁(w,dw) = dw
g₂(w,dw) = w-1;
```
First we try a constant function as the initialization.
```{code-cell}
init = ones(301)
r,w₁ = FNC.bvp(ϕ,domain,g₁,g₂,init)
plot(r,w₁,xaxis=(L"r"),yaxis=(L"w(r)"),
title="Solution of the membrane problem")
```
It's not necessary that the initialization satisfy the boundary conditions. In fact, by choosing a different constant function as the initial guess, we arrive at another valid solution.
```{code-cell}
init = 0.5*ones(301)
r,w₂ = FNC.bvp(ϕ,domain,g₁,g₂,init)
plot!(r,w₂,title="Two solutions of the membrane problem")
```
```{raw} html

```
```{raw} latex
%%end demo%%
```
## Parameter continuation
```{index} ! parameter continuation
```
Sometimes the best way to get a useful initialization is to use the solution of a related easier problem, a technique known as **parameter continuation**. In this approach, one solves the problem at an easy parameter value, and gradually changes the parameter value to the desired value. After each change, the most recent solution is used to initialize the iteration at the new parameter value.
```{index} Allen–Cahn equation
```
(demo-nonlinear-allencahn)=
```{proof:demo}
```
```{raw} html
```
```{raw} latex
%%start demo%%
```
We solve the stationary **Allen–Cahn equation**,
$$
\epsilon u'' = u^3-u, \quad 0 \le x \le 1, \quad u'(0)=0, \; u(1)=1.
$$
```{code-cell}
ϕ = (x,u,dudx) -> (u^3 - u) / ϵ;
g₁(u,du) = du
g₂(u,du) = u-1;
```
Finding a solution is easy at larger values of $\epsilon$.
```{code-cell}
ϵ = 0.02
init = collect( range(-1,1,length=141) )
x,u₁ = FNC.bvp(ϕ,[0,1],g₁,g₂,init)
plot(x,u₁,label=L"\epsilon = 0.02",leg=:bottomright,
xaxis=(L"x"),yaxis=(L"u(x)"),title="Allen–Cahn solution")
```
However, finding a good initialization is not trivial for smaller values of $\epsilon$. Note below that the iteration stops without converging to a solution.
```{code-cell}
ϵ = 0.002;
x,z = FNC.bvp(ϕ,[0,1],g₁,g₂,init);
```
The iteration succeeds if we use the first solution instead as the initialization here.
```{code-cell}
x,u₂ = FNC.bvp(ϕ,[0,1],g₁,g₂,u₁)
plot!(x,u₂,label=L"\epsilon = 0.002")
```
In this case we can continue further.
```{code-cell}
ϵ = 0.0005
x,u₃ = FNC.bvp(ϕ,[0,1],g₁,g₂,u₂)
plot!(x,u₃,label=L"\epsilon = 0.0005")
```
```{raw} html

```
```{raw} latex
%%end demo%%
```
## Exercises
(problem-nonlinear-byhand)=
1. ✍ This exercise is about the nonlinear boundary-value problem
$$
u'' = \frac{3(u')^2}{u} , \quad u(-1) = 1, \; u(2) = \frac{1}{2}.
$$
**(a)** Verify that the exact solution is $u(x) = ( x+2 )^{-1/2}$.
**(b)** Write out the finite-difference approximation {eq}`fdbvp` with a single interior point ($n=2$).
**(c)** Solve the equation of part (b) for the lone interior value $u_1$.
2. ⌨
**(a)** Use {numref}`Function {number}