After digging in the Matlab documentation for a little bit, I think the more elegant way is to use the bvp4c
function. bvp4c
is a function specifically designed to handle boundary value problems like this, as opposed to ode**
, which are really for initial value problems only. In fact, there's a whole set of other functions such as deval
and bvpinit
in Matlab that really facilitate the use of bvp4c
. Here's the link to the Matlab documentation.
I'll post a brief (and perhaps a bit contrived) example here:
function [y1, y2, y3] = test(start,T)
solinit = bvpinit(linspace(0,3,10), [1,1,0]);
sol = bvp4c(@odefun,@bvpbc,solinit);
tspan = linspace(start,T,100);
S = deval(sol, tspan);
y1 = S(1,:);
y2 = S(2,:);
y3 = S(3,:);
plot (tspan,y1)
figure
plot (tspan,y2)
figure
plot (tspan,y3)
%% system definition & BVCs
function dydx = odefun(t,y)
dydx(1) = y(1) + y(2) + t;
dydx(2) = 2*y(1) + y(2);
dydx(3) = 3 * y(1) - y(2);
end
function res = bvpbc(y0,yT)
res= [y0(3) yT(2) yT(3)];
end
end
The test
function outputs 3 vectors of solutions points for y1
, y2
and y3
respectively, and also plots them.
Here are the variable paths plotted by Matlab:
Also, I found this video by professor Jake Blanchard from WMU to be very helpful.