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]
end
function bc!(res, u, p, t)
res[1] = u(0.0)[1] - 1
res[2] = u(1.0)[1]
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]
end
function bca!(res, ua, p)
res[1] = ua[1] - 1
end
function bcb!(res, ub, p)
res[1] = ub[1]
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.8980468026536436, -1.2371538791715229]
[0.885719960383075, -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{cases} 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{cases}\]
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]) / ε
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]
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.33289348903294824, -1.0], [0.0, -4.010479674083406, 2.0236679146909693])
([1.5984664668543418e-5, 0.29379369282252304, -0.9797638564104205], [0.003131766606691308, -3.810163665799216, 2.0235091242732732])
([6.136522044712021e-5, 0.25667650502438244, -0.9595307774915736], [0.005882482569360612, -3.6139607231638076, 2.0230621956194588])
([0.00013242960514612444, 0.22150071858498901, -0.939303433911345], [0.008271768059633372, -3.42188558398142, 2.0223687045265906])
([0.00022565989561530486, 0.1882249947974426, -0.919084097326964], [0.010318830494558283, -3.2339498926251893, 2.021466903737089])
([0.0003377281741204193, 0.15680789272407578, -0.8988746728294515], [0.012042463369676046, -3.050162499071442, 2.0203918795466596])
([0.00046549238446891295, 0.12720789572660665, -0.8786767298402238], [0.013461045371693892, -2.870529738490242, 2.019175704776544])
([0.0006059921805449546, 0.09938343529420608, -0.8584915314958932], [0.01459253974279395, -2.6950556922072515, 2.017847588170714])
([0.0007564447696358193, 0.07329291235171642, -0.8383200625567625], [0.015454493870474909, -2.523742430845814, 2.0164340202676736])
([0.0009142407526823018, 0.04889471622221039, -0.8181630558740562], [0.01606403907860579, -2.356590240449209, 2.0149589157871115])
⋮
([-0.0007564445318050683, -0.07329285055714173, 0.8383200167121018], [0.015454488132544197, -2.523742087416381, 2.016434599748342])
([-0.0006059919969477548, -0.09938337021368325, 0.858491491433196], [0.014592534639490916, -2.695055379041444, 2.0178481651538567])
([-0.00046549224859988397, -0.1272078276807114, 0.8786766955366694], [0.013461040934305137, -2.870529459185725, 2.019176279694523])
([-0.00033772807917794255, -0.15680782206960442, 0.8988746442665584], [0.012042459626101645, -3.050162257269703, 2.0203924528321586])
([-0.0002256598345372104, -0.18822492192770177, 0.9190840744905099], [0.01031882746894794, -3.23394969198186, 2.0214674758065074])
([-0.00013242957064901458, -0.22150064392978822, 0.9393034167911198], [0.008271765772021083, -3.421885428136573, 2.0223692757630856])
([-6.136520506974064e-5, -0.25667642904973675, 0.9595307660809608], [0.005882481035300461, -3.613960615714085, 2.0230627663556984])
([-1.5984660817239116e-5, -0.2937936160300917, 0.979763850705805], [0.0031317658368992434, -3.810163610272876, 2.0235096947739466])
([0.0, -0.3328934119592376, 1.0], [0.0, -4.010479673918108, 2.0236684851351185])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{cases} x_1' = (ε+ x_2 - \sin(t)) y + \cos(t) \\ x_2' = \cos(t) \\ x_3' = y \\ 0 = (x_1-p_1(t)) (y-e^t) \end{cases}\]
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))
end
function bc!(res, u, p, t)
res[1] = u[1]
res[2] = u[3] - 1
res[3] = u[2] - sin(1.0)
end
u0 = [0.0, 0.0, 0.0, 0.0]
tspan = (0.0, 1.0)
fun = BVPFunction(f!, bc!, mass_matrix = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 0])
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.76135832351527e-12]
[0.009999833334154757, 0.009999833334165648, 0.999999999999988, 2.8579238589425407e-11]
[0.019998666693309276, 0.01999866669333207, 0.9999999999999761, 5.237755722129986e-11]
[0.02999550020245993, 0.029995500202494665, 0.9999999999999643, 7.621661319539019e-11]
[0.03998933418658654, 0.039989334186633176, 0.9999999999999523, 9.997699669875379e-11]
[0.049979169270618816, 0.04997916927067736, 0.9999999999999404, 1.2378972768141135e-10]
[0.05996400647937318, 0.05996400647944364, 0.9999999999999285, 1.475773287621681e-10]
[0.0699428473374495, 0.06994284733753182, 0.9999999999999167, 1.7131385383621756e-10]
[0.07991469396907754, 0.07991469396917175, 0.9999999999999049, 1.9501766924604922e-10]
[0.08987854919790399, 0.08987854919801011, 0.9999999999998929, 2.1890735974029323e-10]
⋮
[0.795601620035417, 0.7956016200363661, 0.9999999999990511, 1.9005087130180983e-9]
[0.8016199408828205, 0.8016199408837772, 0.9999999999990434, 1.916167456445694e-9]
[0.80755810040415, 0.8075581004051142, 0.9999999999990362, 1.9314613608204314e-9]
[0.813415504788403, 0.8134155047893737, 0.9999999999990298, 1.9442051099533203e-9]
[0.8191915683000205, 0.8191915683009983, 0.9999999999990224, 1.95841120472908e-9]
[0.8248857133374655, 0.8248857133384501, 0.9999999999990156, 1.971977832070703e-9]
[0.8304973704909792, 0.8304973704919705, 0.9999999999990086, 1.985236259910425e-9]
[0.8360259785995224, 0.8360259786005205, 0.9999999999990017, 1.9986604727359073e-9]
[0.8414709848068925, 0.8414709848078965, 0.9999999999989958, -2.005743902510829e-9]