MATLAB Function Reference    

Solve two-point boundary value problems (BVPs) for ordinary differential equations



A function that evaluates the differential equations . It can have the form
  • dydx = odefun(x,y)
    dydx = odefun(x,y,p1,p2,...)
    dydx = odefun(x,y,parameters)
    dydx = odefun(x,y,parameters,p1,p2,...)
where x is a scalar corresponding to , and y is a column vector corresponding to . parameters is a vector of unknown parameters, and p1,p2,... are known parameters. The output dydx is a column vector.
A function that computes the residual in the boundary conditions . It can have the form
  • res = bcfun(ya,yb)
    res = bcfun(ya,yb,p1,p2,...)
    res = bcfun(ya,yb,parameters)
    res = bcfun(ya,yb,parameters,p1,p2,...)
where ya and yb are column vectors corresponding to and . parameters is a vector of unknown parameters, and p1,p2,... are known parameters. The output res is a column vector.
A structure with fields:

Ordered nodes of the initial mesh. Boundary conditions are imposed at = solinit.x(1) and  = solinit.x(end).

Initial guess for the solution such that solinit.y(:,i) is a guess for the solution at the node solinit.x(i).

Optional. A vector that provides an initial guess for unknown parameters.

The structure can have any name, but the fields must be named x, y, and parameters. You can form solinit with the helper function bvpinit. See bvpinit for details.
Optional integration argument. A structure you create using the bvpset function. See bvpset for details.
Optional. Known parameters that the solver passes to odefun, bcfun, and all the functions specified in options.


sol = bvp4c(odefun,bcfun,solinit) integrates a system of ordinary differential equations of the form

on the interval [a,b] subject to general two-point boundary conditions

The bvp4c solver can also find unknown parameters for problems of the form

where corresponds to parameters. You provide bvp4c an initial guess for any unknown parameters in solinit.parameters. The bvp4c solver returns the final values of these unknown parameters in sol.parameters.

bvp4c produces a solution that is continuous on [a,b] and has a continuous first derivative there. Use the function deval and the output sol of bvp4c to evaluate the solution at specific points xint in the interval [a,b].

The structure sol returned by bvp4c has the following fields:

Mesh selected by bvp4c
Approximation to at the mesh points of sol.x
Approximation to at the mesh points of sol.x
Values returned by bvp4c for the unknown parameters, if any

The structure sol can have any name, and bvp4c creates the fields x, y, yp, parameters, and solver.

sol = bvp4c(odefun,bcfun,solinit,options) solves as above with default integration properties replaced by the values in options, a structure created with the bvpset function. See bvpset for details.

sol = bvp4c(odefun,bcfun,solinit,options,p1,p2...) passes constant known parameters, p1, p2, ..., to odefun, bcfun, and all the functions the user specifies in options. Use options = [] as a placeholder if no options are set.


Example 1. Boundary value problems can have multiple solutions and one purpose of the initial guess is to indicate which solution you want. The second order differential equation

has exactly two solutions that satisfy the boundary conditions

Prior to solving this problem with bvp4c, you must write the differential equation as a system of two first order ODEs

Here and . This system has the required form

The function and the boundary conditions are coded in MATLAB as functions twoode and twobc.

Form a guess structure consisting of an initial mesh of five equally spaced points in [0,4] and a guess of constant values and with the command

Now solve the problem with

Evaluate the numerical solution at 100 equally spaced points and plot with

You can obtain the other solution of this problem with the initial guess

Example 2. This boundary value problem involves an unknown parameter. The task is to compute the fourth () eigenvalue of Mathieu's equation

Because the unknown parameter is present, this second order differential equation is subject to three boundary conditions

It is convenient to use subfunctions to place all the functions required by bvp4c in a single M-file.

The differential equation (converted to a first order system) and the boundary conditions are coded as subfunctions mat4ode and mat4bc, respectively. Because unknown parameters are present, these functions must accept three input arguments, even though some of the arguments are not used.

The guess structure solinit is formed with bvpinit. An initial guess for the solution is supplied in the form of a function mat4init. We chose because it satisfies the boundary conditions and has the correct qualitative behavior (the correct number of sign changes). In the call to bvpinit, the third argument (lambda = 15) provides an initial guess for the unknown parameter .

After the problem is solved with bvp4c, the field sol.parameters returns the value , and the plot shows the eigenfunction associated with this eigenvalue.


bvp4c is a finite difference code that implements the three-stage Lobatto IIIa formula. This is a collocation formula and the collocation polynomial provides a C1-continuous solution that is fourth order accurate uniformly in [a,b]. Mesh selection and error control are based on the residual of the continuous solution.

See Also

@ (function_handle), bvpget, bvpinit, bvpset, deval


[1]  Shampine, L.F., M.W. Reichelt, and J. Kierzenka, "Solving Boundary Value Problems for Ordinary Differential Equations in MATLAB with bvp4c," available at

  builtin bvpget