shooting | R Documentation |
The shooting method solves the boundary value problem for second-order differential equations.
shooting(f, t0, tfinal, y0, h, a, b,
itermax = 20, tol = 1e-6, hmax = 0)
f |
function in the differential equation |
t0 , tfinal |
start and end points of the interval. |
y0 |
starting value of the solution. |
h |
function defining the boundary condition as a function at the end point of the interval. |
a , b |
two guesses of the derivative at the start point. |
itermax |
maximum number of iterations for the secant method. |
tol |
tolerance to be used for stopping and in the |
hmax |
maximal step size, to be passed to the solver. |
A second-order differential equation is solved with boundary conditions
y(t0) = y0
at the start point of the interval, and
h(y(tfinal), dy/dt(tfinal)) = 0
at the end. The zero of
h
is found by a simple secant approach.
Returns a list with two components, t
for grid (or ‘time’)
points between t0
and tfinal
, and y
the solution
of the differential equation evaluated at these points.
Replacing secant with Newton's method would be an easy exercise.
The same for replacing ode45
with some other solver.
L. V. Fausett (2008). Applied Numerical Analysis Using MATLAB. Second Edition, Pearson Education Inc.
bvp
#-- Example 1
f <- function(t, y1, y2) -2*y1*y2
h <- function(u, v) u + v - 0.25
t0 <- 0; tfinal <- 1
y0 <- 1
sol <- shooting(f, t0, tfinal, y0, h, 0, 1)
## Not run:
plot(sol$t, sol$y[, 1], type='l', ylim=c(-1, 1))
xs <- linspace(0, 1); ys <- 1/(xs+1)
lines(xs, ys, col="red")
lines(sol$t, sol$y[, 2], col="gray")
grid()
## End(Not run)
#-- Example 2
f <- function(t, y1, y2) -y2^2 / y1
h <- function(u, v) u - 2
t0 <- 0; tfinal <- 1
y0 <- 1
sol <- shooting(f, t0, tfinal, y0, h, 0, 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.