Learn R Programming

comphy (version 1.0.5)

BVPlinshoot2: Linear shooting method for second-order linear BVPs

Description

Solves a second-order linear boundary value problem using the linear shooting method and superposition of two initial value problems.

Usage

BVPlinshoot2(f, t0, tf, y0, yf, h, ...)

Value

A list with elements t (time points) and y

(solution matrix). The first column of matrix y is the solution, \(y(t)\), the second is its first derivative,

\(y'(t)\).

Arguments

f

A function of the form \(f(t,y,y')\) representing the second-order ODE: \(y''=f(t,y,y')\).

t0

Initial time.

tf

Final time.

y0

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

yf

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

h

Step size.

...

Optional parameters passed to the gradient function f.

Details

If the solution of the associated homogeneous IVP is very small (close to zero) at the second boundary (tf), the solution becomes unstable and the function stops with a warning. Other methods must be used in those cases.

Examples

Run this code

# Solve: y'' - (3/x)y' + (4/x^2)y = x
# with y(1) = 0, y(2) = 4*(log(2) + 1)
# Exact solution: y(x) = x^2*(log(x) - 1) + x^3

# Gradient
f <- function(x,y,dy,...) {
  (3/x)*dy-(4/x^2)*y+x
}

t0 <- 1
tf <- 2
y0 <- 0
yf <- 4*(log(2)+1)
h <- 0.01

ltmp <- BVPlinshoot2(f,t0,tf,y0,yf,h)

# Checks
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