Get Started with Efficient BVP solving in Julia
When ordinary differential equations has constraints over the time span, we should model the differential equations as a boundary value problem which has the form of:
\[\frac{du}{dt} = f(u, p, t) \\ g(u(a),u(b)) = 0\]
BoundaryValueDiffEq.jl addresses three types of BVProblem.
- General boundary value problems:, i.e., differential equations with constraints applied over the time span. This is a system where you would like to obtain the solution of the differential equations and make sure the solution satisfy the boundary conditions simutanously.
- General second order boundary value problems, i.e., differential equations with constraints for both solution and derivative of solution applied over time span. This is a system where you would like to obtain the solution of the differential equations and make sure the solution satisfy the boundary conditions simutanously.
- Boundary value differential-algebraic equations, i.e., apart from constraints applied over the time span, BVDAE has additional algebraic equations which state the algebraic relationship of different states in BVDAE.
Solving Linear two-point boundary value problem
Consider the linear two-point boundary value problem from standard BVP test problem.
using BoundaryValueDiffEq
function f!(du, u, p, t)
du[1] = u[2]
du[2] = u[1]
return
end
function bc!(res, u, p, t)
res[1] = u(0.0)[1] - 1
res[2] = u(1.0)[1]
return
end
tspan = (0.0, 1.0)
u0 = [0.0, 0.0]
prob = BVProblem(f!, bc!, u0, tspan)
sol = solve(prob, MIRK4(), dt = 0.01)retcode: Success
Interpolation: MIRK Order 4 Interpolation
t: 101-element Vector{Float64}:
0.0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
⋮
0.92
0.93
0.94
0.95
0.96
0.97
0.98
0.99
1.0
u: 101-element Vector{Vector{Float64}}:
[1.0000000000000002, -1.3130352855093987]
[0.9869194287214476, -1.303100771153411]
[0.9739375502081996, -1.2932965679604558]
[0.961053066261587, -1.2836216955020439]
[0.9482646884224784, -1.274075186282867]
[0.9355711378424324, -1.2646560856440483]
[0.9229711451558132, -1.2553634516676733]
[0.9104634503528525, -1.2461963550826014]
[0.8980468026536466, -1.2371538791715346]
[0.8857199603830782, -1.2282351196793468]
⋮
[0.06814608517899785, -0.8536425188086404]
[0.05961292504921865, -0.8530037290807472]
[0.05108572626162191, -0.8524502404365982]
[0.04256363608922265, -0.8519819975268682]
[0.03404580231590203, -0.8515989535268759]
[0.02553137315118448, -0.8513010701319021]
[0.017019497145058217, -0.851088317553359]
[0.008509323102829352, -0.8509606745158118]
[1.889531523315415e-15, -0.8509181282548499]Since this problem only has constraints at the start and end of the time span, we can directly use TwoPointBVProblem:
function f!(du, u, p, t)
du[1] = u[2]
du[2] = u[1]
return
end
function bca!(res, ua, p)
res[1] = ua[1] - 1
return
end
function bcb!(res, ub, p)
res[1] = ub[1]
return
end
tspan = (0.0, 1.0)
u0 = [0.0, 0.0]
prob = TwoPointBVProblem(
f!, (bca!, bcb!), u0, tspan, bcresid_prototype = (zeros(1), zeros(1))
)
sol = solve(prob, MIRK4(), dt = 0.01)retcode: Success
Interpolation: MIRK Order 4 Interpolation
t: 101-element Vector{Float64}:
0.0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
⋮
0.92
0.93
0.94
0.95
0.96
0.97
0.98
0.99
1.0
u: 101-element Vector{Vector{Float64}}:
[1.0, -1.3130352855093865]
[0.9869194287214468, -1.303100771153399]
[0.9739375502081986, -1.293296567960444]
[0.9610530662615859, -1.283621695502032]
[0.9482646884224767, -1.2740751862828554]
[0.9355711378424306, -1.264656085644036]
[0.9229711451558111, -1.2553634516676613]
[0.9104634503528498, -1.2461963550825894]
[0.8980468026536435, -1.2371538791715229]
[0.8857199603830749, -1.228235119679335]
⋮
[0.068146085178995, -0.8536425188086301]
[0.059612925049215885, -0.8530037290807368]
[0.0510857262616192, -0.8524502404365879]
[0.04256363608922005, -0.851981997526858]
[0.034045802315899654, -0.8515989535268659]
[0.025531373151182202, -0.851301070131892]
[0.017019497145056163, -0.851088317553349]
[0.00850932310282742, -0.8509606745158016]
[5.69459472212872e-17, -0.8509181282548396]Solving second order boundary value problem
Consirder the test problem from example problems in MIRKN paper Muir and Adams [1].
\[\begin{align*} y_1'(x) &= y_2(x),\\ ε y_2'(x) &= -y_1(x) y_2'(x) - y_3(x) y_3'(x), \\ ε y_3'(x) &= y_1'(x) y_3(x) - y_1(x) y_3'(x) \end{align*}\]
with initial conditions:
\[\begin{align*} y_1(0) &= y_1'(0) = y_1(1) = y_1'(1) = 0, \\ y_3(0) &= -1, \\ y_3(1) &=1 \end{align*}\]
using BoundaryValueDiffEqMIRKN
function f!(ddu, du, u, p, t)
ε = 0.1
ddu[1] = u[2]
ddu[2] = (-u[1] * du[2] - u[3] * du[3]) / ε
ddu[3] = (du[1] * u[3] - u[1] * du[3]) / ε
return
end
function bc!(res, du, u, p, t)
res[1] = u(0.0)[1]
res[2] = u(1.0)[1]
res[3] = u(0.0)[3] + 1
res[4] = u(1.0)[3] - 1
res[5] = du(0.0)[1]
res[6] = du(1.0)[1]
return
end
u0 = [1.0, 1.0, 1.0]
tspan = (0.0, 1.0)
prob = SecondOrderBVProblem(f!, bc!, u0, tspan)
sol = solve(prob, MIRKN4(), dt = 0.01)retcode: Success
Interpolation: 1st order linear
t: 101-element Vector{Float64}:
0.0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
⋮
0.92
0.93
0.94
0.95
0.96
0.97
0.98
0.99
1.0
u: 101-element Vector{RecursiveArrayTools.ArrayPartition{Float64, Tuple{Vector{Float64}, Vector{Float64}}}}:
([0.0, 0.332894023156958, -1.0], [0.0, -4.010483700884779, 2.0236677823390985])
([1.5984690703048275e-5, 0.293794186609831, -0.9797638577347598], [0.003131771746364443, -3.8101677065840116, 2.02350899168225])
([6.13653218936359e-5, 0.2566769583280866, -0.9595307801446122], [0.00588249244461684, -3.6139647793072855, 2.023062062423978])
([0.00013242982733332128, 0.22150113124597434, -0.939303437900239], [0.008271782264850052, -3.4218896565078647, 2.0223685705331866])
([0.00022566027980827218, 0.18822536664928907, -0.9190841026599386], [0.010318848622478977, -3.23395398192032, 2.021466768925303])
([0.00033772875750294826, 0.15680822360096383, -0.8988746795140786], [0.012042485011375694, -3.0501666045811384, 2.020391744067036])
([0.00046549320012739755, 0.12720818547409438, -0.8786767378817363], [0.013461070116636064, -2.87053385845286, 2.0191755689450877])
([0.0006059932574530525, 0.09938368378241115, -0.8584915408955731], [0.014592567179008758, -2.695059823442804, 2.0178474524597827])
([0.0007564461326415885, 0.07329311949028493, -0.838320073310437], [0.015454523584878243, -2.5237465686096745, 2.016433885293567])
([0.0009142424224993007, 0.048894881976431596, -0.8181630679707326], [0.016064070657474474, -2.3565943783483396, 2.01495878229478])
⋮
([-0.0007564462683575229, -0.07329317370684822, 0.8383200245258973], [0.015454527178535878, -2.5237462679076486, 2.01643449257847])
([-0.0006059933598936757, -0.09938373506470051, 0.8584914981899895], [0.014592570245303756, -2.695059538521063, 2.0178480609060236])
([-0.0004654932744225576, -0.12720823401581421, 0.8786767012650246], [0.013461072684001653, -2.870533596393183, 2.01917617822382])
([-0.000337728808508998, -0.15680826966370073, 0.8988746489931869], [0.01204248710596376, -3.050166371882654, 2.0203923539134263])
([-0.00022566031213418715, -0.18822541055695102, 0.9190840782394801], [0.010318850267509633, -3.233953784565617, 2.0214673791367828])
([-0.00013242984537305033, -0.22150117338028052, 0.9393034195830932], [0.008271783480012168, -3.4218895001069085, 2.022369180964326])
([-6.136532986425008e-5, -0.25667699912586883, 0.9595307679324545], [0.00588249324550522, -3.6139646693137646, 2.023062672977649])
([-1.5984692688421936e-5, -0.29379422656277565, 0.9797638516284849], [0.0031317721439322213, -3.8101676485616305, 2.023509602296991])
([0.0, -0.3328940628140731, 1.0], [0.0, -4.0104837007750795, 2.0236683929729495])Solving semi-explicit boundary value differential-algebraic equations
Consider the nonlinear semi-explicit DAE of index at most 2 in COLDAE paper Ascher and Spiteri [2]
\[\begin{align*} x_1' &= (ε+ x_2 - \sin(t)) y + \cos(t) \\ x_2' &= \cos(t) \\ x_3' &= y \\ 0 &= [x_1-p_1(t)] \left(y - e^t\right) \end{align*}\]
with boundary conditions
\[\begin{align*} x_1(0) &= 0, \\ x_3(0) &= 1, \\ x_2(1) &= \sin(1) \end{align*}\]
using BoundaryValueDiffEqAscher
function f!(du, u, p, t)
du[1] = (1 + u[2] - sin(t)) * u[4] + cos(t)
du[2] = cos(t)
du[3] = u[4]
du[4] = (u[1] - sin(t)) * (u[4] - exp(t))
return
end
function bc!(res, u, p, t)
res[1] = u[1]
res[2] = u[3] - 1
res[3] = u[2] - sin(1.0)
return
end
u0 = [0.0, 0.0, 0.0, 0.0]
tspan = (0.0, 1.0)
mass_matrix = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 0]
fun = BVPFunction(f!, bc!; mass_matrix)
prob = BVProblem(fun, u0, tspan)
sol = solve(prob, Ascher4(zeta = [0.0, 0.0, 1.0]), dt = 0.01)retcode: Success
Interpolation: 1st order linear
t: 101-element Vector{Float64}:
0.0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
⋮
0.92
0.93
0.94
0.95
0.96
0.97
0.98
0.99
1.0
u: 101-element Vector{Vector{Float64}}:
[0.0, -1.0259220371532954e-15, 1.0, 4.76207231483357e-12]
[0.009999833334154757, 0.009999833334165648, 0.999999999999988, 2.857980878195739e-11]
[0.019998666693309266, 0.01999866669333207, 0.999999999999976, 5.2383325742515844e-11]
[0.029995500202459936, 0.029995500202494665, 0.9999999999999641, 7.622000204888205e-11]
[0.03998933418658653, 0.039989334186633176, 0.9999999999999521, 1.000102471914895e-10]
[0.049979169270618795, 0.04997916927067736, 0.9999999999999404, 1.238344667101833e-10]
[0.05996400647937318, 0.05996400647944364, 0.9999999999999286, 1.4761642450018108e-10]
[0.06994284733744946, 0.06994284733753182, 0.9999999999999167, 1.7139787068603356e-10]
[0.07991469396907751, 0.07991469396917175, 0.9999999999999049, 1.9509109982453815e-10]
[0.08987854919790403, 0.08987854919801011, 0.999999999999893, 2.187532913646442e-10]
⋮
[0.7956016200354142, 0.7956016200363661, 0.9999999999990486, 1.9064842718388763e-9]
[0.8016199408828182, 0.8016199408837772, 0.9999999999990413, 1.920477040586015e-9]
[0.8075581004041482, 0.8075581004051142, 0.9999999999990342, 1.93519104559728e-9]
[0.8134155047884001, 0.8134155047893737, 0.9999999999990268, 1.9506648856386137e-9]
[0.8191915683000172, 0.8191915683009983, 0.9999999999990198, 1.9651435662299495e-9]
[0.8248857133374615, 0.8248857133384501, 0.999999999999013, 1.9803780356147215e-9]
[0.830497370490975, 0.8304973704919705, 0.9999999999990064, 1.993021260164545e-9]
[0.8360259785995183, 0.8360259786005205, 0.9999999999989995, 2.0074077747355337e-9]
[0.841470984806888, 0.8414709848078965, 0.9999999999989934, -2.014617537411085e-9]