Qu

bvp

Builtin index · Signal processing

result = bvp(f, bc, x, y0, [tol=1e-6], [max_nodes=10000])

Input: f, bc, x, y0, tol, max_nodes · Output: result

What it is

Solves the two-point boundary value problem y' = f(x, y) with boundary conditions bc(y(a), y(b)) = 0 for a = x[0], b = x[end]. f: a function (or its name) of the position x and the state y returning dy/dx; bc: a function of the two end states ya, yb returning the residual, one entry per unknown; x: the initial mesh (a strictly increasing vector, at least 2 nodes; more nodes where the solution varies fast make a better start); y0: the initial guess, either a matrix with one row per unknown and one column per mesh node (zeros(2, length(x)) is a legitimate start for a well-posed linear problem), or a function of x returning the guess vector, or, for a single unknown, a vector with one entry per node. With a single unknown (y0 a vector, or a matrix with one row) f and bc receive and return plain numbers; with more, y, ya, yb are vectors (a second-order equation y'' = g(x, y, y') is written as the system [y[1], g(x, y[0], y[1])]). tol: bound on the relative residual of the solution (the true residual y_S' - f(x, y_S), scaled by 1 + abs(f), of the piecewise-cubic interpolant y_S), also the tolerance on the boundary residual; max_nodes: the largest mesh allowed. Method: fourth-order collocation (Lobatto IIIA, the scheme of SciPy's solve_bvp and MATLAB's bvp4c): cubic Hermite collocation at the interval midpoints, damped Newton iteration with a finite-difference Jacobian and a banded direct solve, then mesh refinement driven by the residual until it is below tol. Cost is linear in the number of nodes.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = bvp(f, bc, x, y0, [tol=1e-6], [max_nodes=10000])
f —f: a function (or its name) of the position x and the state y returning dy/dx.
bc —bc: a function of the two end states ya, yb returning the residual, one entry per unknown.
x vectorx: the initial mesh (a strictly increasing vector, at least 2 nodes.
y0 matrix or vectory0: the initial guess, either a matrix with one row per unknown and one column per mesh node (zeros(2, length(x)) is a legitimate start for a well-posed linear problem), or a function of x returning the guess vector, or, for a single unknown, a vector with one entry per node.
tol optional (default 1e-6)tol: bound on the relative residual of the solution (the true residual y_S' - f(x, y_S), scaled by 1 + abs(f), of the piecewise-cubic interpolant y_S), also the tolerance on the boundary residual.
max_nodes optional (default 10000)max_nodes: the largest mesh allowed.
x —the final mesh
y —the solution, one row per unknown, one column per node
yp —y' at the nodes
nodes —the mesh size
max_residual —the largest normalised residual estimate, NaN when none could be computed
x —the message says when it looks transposed
bc —passed through
Returns a record with x (the final mesh), y (the solution, one row per unknown, one column per node), yp (y' at the nodes), status, nodes (the mesh size) and max_residual (the largest normalised residual estimate, NaN when none could be computed). status is "converged", "max_nodes" (the mesh would have to exceed max_nodes), "singular" (the collocation Jacobian is singular, or so close to it that a small residual only means a huge solution: a problem with no unique solution such as y' = 0 with y(0) = y(1), or with no solution such as y'' + y = 0, y(0) = 0, y(pi) = 1; the test is a pivot below 1e-6 of the largest Jacobian entry once the residual is below tol, so a very coarse tol can miss it) or "no_convergence" (Newton or the refinement did not reach tol or the boundary tolerance, or the solution became non-finite). A status other than "converged" is not an error: the last iterate is still returned in x/y; do not trust it. Evaluate the solution anywhere with bvp_eval. Errors: a non-monotonic mesh, a y0 whose columns do not match x (the message says when it looks transposed), a bc that returns the wrong number of values, a non-finite initial guess, more than 20 million calls of f, and any error raised inside f or bc (passed through). Unknown parameters (p= in SciPy) are not supported.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

# y'' + y = 0, y(0) = 0, y(pi/2) = 1 (y = sin x), as the system y' = [y1, -y0]

function osc(x, y)
    return [y[1], -y[0]]
end function
function ends(ya, yb)
    return [ya[0], yb[0] - 1]
end function
b = bvp(osc, ends, linspace(0, pi/2, 11), zeros(2, 11))
print("{b.status} on {b.nodes} nodes")                  # converged on 41 nodes

print(bvp_eval(b, pi/4)[0] - sin(pi/4))                 # -1.83e-09


c = chebdiff(16)
print(max(abs(c.D * exp(c.x) - exp(c.x))))              # 5.8e-14, spectral accuracy

print(integral2((x, y) := 1, -1, 1, (x) := -sqrt(1 - x^2), (x) := sqrt(1 - x^2)))   # 3.141593, the unit disc

r = fminbnd((x) := cos(x), 3, 4)
print("{r.params} {r.converged}")                       # 3.141593 true

output
converged on 41 nodes
-1.834181e-9
5.817569e-14
3.141593
3.141593 true

from signal-processing