from numpy import *
from scipy import linalg
from scipy.linalg import norm
from matplotlib.pyplot import *
from prettytable import PrettyTable
from timeit import default_timer as timer
import sys
sys.path.append('fncbook/')
import fncbook as FNC
# This (optional) block is for improving the display of plots.
# from IPython.display import set_matplotlib_formats
# set_matplotlib_formats("svg","pdf")
# %config InlineBackend.figure_format = 'svg'
rcParams["figure.figsize"] = [7, 4]
rcParams["lines.linewidth"] = 2
rcParams["lines.markersize"] = 4
rcParams['animation.html'] = "jshtml" # or try "html5"
One way to attack the TPBVP (10.1.1) is to adapt our IVP solving techniques from Chapter 6 to it. Those techniques work only when we know the entire initial state, but we can allow that state to vary in order to achieve the stated conditions.
This is the idea behind the shooting method. Imagine adjusting your aiming point and power to sink a basketball shot from the free-throw line. The way in which you miss—too long, flat trajectory, etc.—informs how you will adjust for your next attempt.
We can do much better than trial-and-error for the unknown part of the initial state. As usual, we can rewrite the ODE u′′(x)=ϕ(x,u,u′) in first-order form as
We turn this into an IVP by specifying y(a)=s1, y′(a)=s2, for a vector s to be determined by the boundary conditions. Define the residual function v(s) by
The dependence of v2 on s is indirect, through the solution of the IVP for y(x). We now have a standard rootfinding problem that can be solved via the methods of Chapter 4.
Our implementation of shooting is given in Function 10.2.1. Note the structure: we use a rootfinding method that in turn relies on an IVP solver. This sort of arrangement is what makes us concerned with minimizing the number of objective function calls when rootfinding.
The accuracy of the shooting method should be comparable to those of the component pieces, the rootfinding method, and the IVP solver. However, the shooting method is unstable for some problems. An example illustrates the trouble.
The essence of the instability is that errors can grow exponentially away from the boundary at x=a, where the state is arbitrarily being set (see Theorem 6.1.2). Using shooting, acceptable accuracy near x=b therefore means requiring extraordinarily high accuracy near x=a.
The instability of shooting can be circumvented by breaking the interval into smaller pieces and thus limiting the potential for error growth. However, we do not go into these details. Instead, the methods in the rest of this chapter treat both ends of the domain symmetrically and solve over the whole domain at once.