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.33289348903294563, -1.0], [0.0, -4.010479674083402, 2.0236679146909706])
([1.598466466854329e-5, 0.2937936928225205, -0.9797638564104205], [0.003131766606691282, -3.810163665799212, 2.0235091242732746])
([6.13652204471097e-5, 0.2566765050243799, -0.9595307774915736], [0.005882482569360576, -3.6139607231638036, 2.02306219561946])
([0.00013242960514456463, 0.22150071858498652, -0.939303433911345], [0.008271768059636461, -3.4218855839814166, 2.022368704526588])
([0.00022565989561378028, 0.18822499479744012, -0.919084097326964], [0.01031883049456131, -3.2339498926251866, 2.0214669037370863])
([0.0003377281741189259, 0.15680789272407333, -0.8988746728294515], [0.012042463369679042, -3.0501624990714404, 2.020391879546657])
([0.00046549238446745247, 0.12720789572660424, -0.8786767298402239], [0.013461045371696866, -2.870529738490241, 2.0191757047765413])
([0.0006059921805435236, 0.09938343529420367, -0.8584915314958933], [0.014592539742796902, -2.6950556922072506, 2.0178475881707114])
([0.000756444769634418, 0.07329291235171402, -0.8383200625567625], [0.015454493870477834, -2.5237424308458136, 2.016434020267671])
([0.0009142407526809312, 0.04889471622220798, -0.8181630558740564], [0.01606403907860869, -2.356590240449209, 2.0149589157871093])
⋮
([-0.0007564445318050787, -0.07329285055714484, 0.8383200167121017], [0.015454488132544455, -2.52374208741638, 2.0164345997483433])
([-0.0006059919969477628, -0.09938337021368636, 0.8584914914331959], [0.014592534639491143, -2.695055379041443, 2.017848165153858])
([-0.00046549224859988994, -0.1272078276807145, 0.8786766955366693], [0.013461040934305333, -2.870529459185724, 2.0191762796945247])
([-0.00033772807917794667, -0.1568078220696075, 0.8988746442665583], [0.01204245962610181, -3.0501622572697022, 2.0203924528321604])
([-0.00022565983453721298, -0.18822492192770485, 0.9190840744905098], [0.010318827468948074, -3.2339496919818593, 2.021467475806509])
([-0.00013242957064901596, -0.22150064392979127, 0.9393034167911197], [0.008271765772021173, -3.421885428136572, 2.022369275763087])
([-6.136520506974121e-5, -0.2566764290497398, 0.9595307660809608], [0.005882481035300517, -3.6139606157140847, 2.0230627663557])
([-1.5984660817239265e-5, -0.29379361603009474, 0.979763850705805], [0.0031317658368992737, -3.810163610272876, 2.023509694773948])
([0.0, -0.33289341195924066, 1.0], [0.0, -4.010479673918108, 2.0236684851351203])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.761864869421424e-12]
[0.009999833334154757, 0.009999833334165648, 0.999999999999988, 2.8571821472923586e-11]
[0.019998666693309266, 0.01999866669333207, 0.9999999999999761, 5.23857697213451e-11]
[0.029995500202459953, 0.029995500202494665, 0.9999999999999643, 7.617767866267828e-11]
[0.03998933418658654, 0.039989334186633176, 0.9999999999999523, 9.999101043272868e-11]
[0.0499791692706188, 0.04997916927067736, 0.9999999999999405, 1.2381935111692835e-10]
[0.05996400647937319, 0.05996400647944364, 0.9999999999999287, 1.4758012870294273e-10]
[0.06994284733744953, 0.06994284733753182, 0.999999999999917, 1.7116668630348549e-10]
[0.07991469396907756, 0.07991469396917175, 0.999999999999905, 1.9504130414234239e-10]
[0.08987854919790413, 0.08987854919801011, 0.9999999999998931, 2.1858143035588727e-10]
⋮
[0.7956016200354166, 0.7956016200363661, 0.9999999999990504, 1.9021957195557365e-9]
[0.8016199408828202, 0.8016199408837772, 0.9999999999990428, 1.9163687781014216e-9]
[0.8075581004041497, 0.8075581004051142, 0.9999999999990354, 1.9319996596443984e-9]
[0.8134155047884019, 0.8134155047893737, 0.9999999999990284, 1.9459137593011235e-9]
[0.8191915683000197, 0.8191915683009983, 0.9999999999990219, 1.9596545138253377e-9]
[0.8248857133374642, 0.8248857133384501, 0.999999999999015, 1.9739201166429597e-9]
[0.8304973704909784, 0.8304973704919705, 0.9999999999990088, 1.9873835428533213e-9]
[0.8360259785995217, 0.8360259786005205, 0.9999999999990021, 2.00065862908497e-9]
[0.841470984806891, 0.8414709848078965, 0.9999999999989952, -2.0082752254591135e-9]