Sei sulla pagina 1di 6

MATLAB bvp4c

Boundary value problems (BVP) for ordinary differential equations (ODE) can
be solved using MATLAB bvp4c solver.
The solver uses finite difference method.
It starts solution with an initial guess supplied at an initial mesh points and
changes step-size (hence changes mesh) to get the specified accuracy.
Let us assume that the given order of BVP are defined on the interval [a, b]
subject to two-point boundary conditions as,
y(a)=Ya

and

y(b)=Yb

To solve the given BVP using MATLAB, it is needed to convert the given
equation to an equivalent system of first order ordinary differential equations
in the form of

dy
= y = f ( x, y )
dx

MATLAB bvp4c
Matlab Syntax:
sol = bvp4c(@odefun,@bcfun,solinit,options)
odefun
A function that evaluates the BVP in the form of an equivalent system of first
order differential equations. It can have the form
function dydx = odefun(x,y),
where x is a scalar, and dydx and y are column vectors.
bcfun
A function that computes the residual in the boundary conditions. bcfun can
have the form
function res = bcfun(ya,yb)
where ya and yb are column vectors corresponding to the numerical solution at
x=a and x=b, respectively.
The first elements ya(1) and yb(1) are the values of y at x=a and x=b.
The second elements ya(2) and yb(2) are the values of dy/dx at x=a and x=b.
Output argument res is a column vector with the values of residuals.

MATLAB bvp4c
Residuals can be calculated as
(i) Dirichlet BC: if BC has the form y(a)=Ya, and y(b)=Yb
res=[ ya(1)-Ya
yb(1)-Yb];
(ii) Neumann BC: if BC has the form dy/dx(a)=Da, and dy/dx(b)=Db
res=[ ya(2)-Da
yb(2)-Db];
(iii) Mixed BC: if BC has the form y(a)=Ya, and dy/dx(b)=Db
res=[ ya(1)-Ya
yb(2)-Db];
if BC has the form dy/dx(a)=Da, and y(b)=Yb
res=[ ya(2)-Da
yb(1)-Yb];
General form: c1*dy/dx(a)+c2*y(a)=Ca, c3*dy/dx(b)+c4*y(b)=Cb
res=[ ya(2)+c2/c1*ya(1)-Ca/c1
yb(2)+c4/c3*yb(1)-Cb/c3];

MATLAB bvp4c
solinit
A structure containing the initial guess for a solution.
solinit can be created using the function bvpinit.
solinit = bvpinit(x,yinit);
where vector x is a guess of the initial mesh points that the solution method
in BVp4c should initially use.
For a BVP with domain [a, b], the first element of x is a, and the last
element is b.
Initial mesh may be chosen by using
x=linspace(a,b,N);or x=[a:increment:b];
The vector yinit is the initial guess for the solution, it can be constant or
functions of x.

MATLAB bvp4c
sol
sol is a structure containing the solution. It is returned by bvp4c. It has the
following fields:
sol.x Mesh selected by bvp4c. Final number of mesh points is determined
by the BVP4c solver during the solution process.
sol.y

Solution filed at the mesh points of sol.x

if we have a 3rd order BVP, then


sol.y(1,:) = solution field for
sol.y(2,:) = solution field for
sol.y(3,:) = solution field for

sol.y will have,


function y
1st derivative of function
2nd derivative of the function

The structure sol can have any name. It is convenient to use subfunctions to
place all the functions required by bvp4c in a single M-file

MATLAB bvp4c
Options:
The solver seeks to control the error in the solution through the residuals in
the boundary conditions, both for each subinterval and at the system
boundaries. Provisions are made for the specification of absolute error in
the solutions and the relative error allowed to propagate between
iterations [1].
options = bvpset('RelTol',10^(-6), 'Abstol',10^(-6),
'Nmax',5000,'Stats','off','Vectorized','on');
where, Relative tolerance, RelTol=10^(-6)
Absolute tolerance, Abstol=10^(-6)
Maximum number of mesh points allowed, Nmax=5000
Display computational cost statistics, Stats = off
Vectorized=on

Note: Vectorized ON helps the ODE function to pass to the solver a whole
array of column vectors at once. This allows the solver to reduce the
number of function evaluations, and may significantly reduce solution
time.

MATLAB bvp4c:Example
Let us consider the following second-order differential equation

y + y = 0

(1)

and the boundary conditions are,


i) Derichlet/function BC: y(x =1) = 1.5;

y(x = -1) = -2

dy
ii) Neuman/derivative BC:
(x =1) = 1.5; y(x = -1) = -2
dx

dy

+ y ( x = 1) = 1.5 ;
dx

iii) Mixed BC:

y(x = -1) = -2

MATLAB bvp4c:Example
N=50;
y + y = 0
j=0:N;
x=cos(pi*j/N);
options = bvpset('RelTol',10^(-6),'Abstol',10^(-6),
'Nmax',5000,'Stats','off','Vectorized','on');
solinit = bvpinit(x,[1 0]);
sol = bvp4c(@odefun,@bcfun1,solinit,options);
plot(sol.x,sol.y(1,:));
function dydx = odefun(x,y)
dydx = [ y(2)
-abs(y(1))];

function res = bcfun2(ya,yb)


res = [ya(2)-1.5
yb(1) + 2];

function res = bcfun1(ya,yb)


res = [ ya(1) -1.5
yb(1) + 2];

function res = bcfun3(ya,yb)


res = [ya(2)+ya(1)-1.5
yb(1) + 2];

MATLAB bvp4c:Reference
Reference:
[1] Shampine, L.F., Kierzenka, J. & Reichelt, M. W. 2000, Solving Boundary
Value Problems for Ordinary Differential Equations in Matlab with bvp4c.
ftp://ftp.mathworks.com/pub/doc/papers/bvp/

MATLAB dde23:Example
Solve the following system using dde23 on the interval, t=[0 1]

History for t0:

y1(t)=exp(t+1)
y2(t)=exp(t+0.5)
y3(t)=sin(t+1)
y4(t)=y1(y)
y5(t)=y1(t)

MATLAB dde23:Reference
Reference:
[1] Shampine, L.F., Thompson, S. 2000, Solving Delay Differential Equations
with dde23.
[2] http://www.radford.edu/thompson/webddes/etable.html

Potrebbero piacerti anche