Learn R Programming

comphy (version 1.0.5)

BVPshoot2: Solves a second-order BVP using the shooting method

Description

Solves \(y^{('')} = f(t,y,y^{'})\) on the interval \([t_0,t_f]\) with boundary conditions \(y(t_0) = y_0\), \(y(t_f) = y_f\), using the shooting method and a root finder. The associated IVP is solved using 4th-order Runge-Kutta with RK4ODE. The second initial value is found using the bisection method with roots_bisec.

Usage

BVPshoot2(f, t0, tf, y0, yf, h, s_guess = 1, tol = 1e-09, ...)

Value

A list with elements t (time grid) and y (solution matrix), where y[,1] contains \(y(t)\) and y[,2]

its derivative.

Arguments

f

A function of the form f(t,y,dy) returning a numeric scalar. This defines the second-order ODE.

t0

Initial time.

tf

Final time.

y0

Boundary value at \(t_0\), i.e. \(y(t_0)=y_0\).

yf

Boundary value at \(t_f\), i.e. \(y(t_f)=y_f\).

h

Step size for the RK4 integration.

s_guess

A numeric starting guess for \(y'(t_0)\) (default is 1), or a 2-element numeric vector giving a bracketing interval.

tol

A numeric value that tells the bisection algorithms when to stop. Default is 1e-9.

...

Additional arguments passed to f.

Details

It is important to consider the uniqueness of the solution of a BVP. If the BVP admits infinitely many solutions (a family of solutions), BVPshoot2 will find only one of them, depending on what initial condition for the first derivative of the associated IVP, was found using the bisection method.

Examples

Run this code
        
# y"+ y = 9*sin(2*t); y(0)=-1, y(3*pi/2)=0
# Unique solution: y(t) = 3*sin(2*t) - cos(t)

# Define y"=f(t,y,y')
f <- function(t,y,dy) {ff <- -9*sin(2*t)-y; return(ff)}

# Solution interval
t0 <- 0
tf <- 3*pi/2

# Boundary values
y0 <- -1
yf <- 0

# Step size
h <- 0.01

# Solution
ltmp <- BVPshoot2(f,t0,tf,y0,yf,h)

# Check
# Number of steps
n <- length(ltmp$t)-1
print(c(ltmp$t[1],ltmp$t[n+1]))
print(c(ltmp$y[1,1],ltmp$y[n+1,1]))

Run the code above in your browser using DataLab