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.3130352855093994]
[0.9869194287214476, -1.3031007711534117]
[0.9739375502081996, -1.2932965679604564]
[0.961053066261587, -1.2836216955020445]
[0.9482646884224784, -1.2740751862828676]
[0.9355711378424326, -1.264656085644049]
[0.9229711451558134, -1.255363451667674]
[0.9104634503528526, -1.246196355082602]
[0.8980468026536467, -1.2371538791715353]
[0.8857199603830783, -1.2282351196793475]
⋮
[0.06814608517899787, -0.8536425188086404]
[0.059612925049218654, -0.8530037290807472]
[0.05108572626162192, -0.8524502404365982]
[0.04256363608922266, -0.8519819975268682]
[0.034045802315902034, -0.8515989535268759]
[0.025531373151184485, -0.8513010701319021]
[0.01701949714505822, -0.851088317553359]
[0.008509323102829352, -0.8509606745158118]
[1.888963463549682e-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.3130352855093863]
[0.9869194287214468, -1.3031007711533988]
[0.9739375502081986, -1.2932965679604438]
[0.9610530662615859, -1.2836216955020319]
[0.9482646884224767, -1.2740751862828552]
[0.9355711378424306, -1.264656085644036]
[0.9229711451558111, -1.2553634516676615]
[0.9104634503528498, -1.2461963550825896]
[0.8980468026536435, -1.237153879171523]
[0.8857199603830749, -1.2282351196793353]
⋮
[0.06814608517899479, -0.8536425188086307]
[0.0596129250492158, -0.8530037290807374]
[0.051085726261619085, -0.8524502404365886]
[0.04256363608921999, -0.8519819975268585]
[0.03404580231589969, -0.8515989535268664]
[0.02553137315118217, -0.8513010701318926]
[0.017019497145056007, -0.8510883175533496]
[0.008509323102827404, -0.850960674515802]
[0.0, -0.85091812825484]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.33289348903294913, -1.0], [0.0, -4.010479674083407, 2.0236679146909697])
([1.5984664668543462e-5, 0.2937936928225239, -0.9797638564104205], [0.0031317666066913168, -3.8101636657992173, 2.0235091242732737])
([6.136522044712025e-5, 0.2566765050243833, -0.9595307774915736], [0.005882482569360635, -3.6139607231638085, 2.023062195619459])
([0.00013242960514588928, 0.22150071858498985, -0.939303433911345], [0.008271768059633571, -3.421885583981421, 2.0223687045265892])
([0.00022565989561508455, 0.1882249947974434, -0.919084097326964], [0.010318830494558477, -3.2339498926251906, 2.0214669037370876])
([0.0003377281741202039, 0.15680789272407658, -0.8988746728294515], [0.012042463369676237, -3.050162499071444, 2.020391879546658])
([0.0004654923844687013, 0.12720789572660743, -0.8786767298402238], [0.013461045371694092, -2.8705297384902435, 2.019175704776543])
([0.000605992180544744, 0.09938343529420685, -0.8584915314958932], [0.014592539742794158, -2.6950556922072533, 2.0178475881707127])
([0.0007564447696356123, 0.07329291235171717, -0.8383200625567625], [0.015454493870475117, -2.523742430845816, 2.0164340202676727])
([0.0009142407526820962, 0.048894716222211115, -0.8181630558740564], [0.016064039078606006, -2.356590240449211, 2.0149589157871106])
⋮
([-0.0007564445318050713, -0.07329285055714245, 0.8383200167121017], [0.015454488132544268, -2.5237420874163816, 2.0164345997483424])
([-0.000605991996947757, -0.09938337021368399, 0.8584914914331959], [0.01459253463949098, -2.695055379041445, 2.017848165153857])
([-0.00046549224859988576, -0.12720782768071215, 0.8786766955366693], [0.013461040934305194, -2.8705294591857258, 2.0191762796945234])
([-0.00033772807917794375, -0.15680782206960517, 0.8988746442665584], [0.012042459626101693, -3.050162257269704, 2.0203924528321595])
([-0.00022565983453721116, -0.18822492192770254, 0.9190840744905099], [0.010318827468947979, -3.233949691981861, 2.021467475806508])
([-0.00013242957064901495, -0.221500643929789, 0.9393034167911198], [0.008271765772021104, -3.421885428136574, 2.022369275763086])
([-6.136520506974083e-5, -0.25667642904973753, 0.9595307660809608], [0.005882481035300479, -3.613960615714086, 2.023062766355699])
([-1.5984660817239156e-5, -0.29379361603009246, 0.979763850705805], [0.0031317658368992516, -3.8101636102728773, 2.023509694773947])
([0.0, -0.33289341195923844, 1.0], [0.0, -4.010479673918109, 2.0236684851351194])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.761875388583138e-12]
[0.009999833334154759, 0.009999833334165648, 0.999999999999988, 2.8581404981736773e-11]
[0.01999866669330926, 0.01999866669333207, 0.999999999999976, 5.240391631629243e-11]
[0.02999550020245992, 0.029995500202494665, 0.9999999999999641, 7.624731992482685e-11]
[0.039989334186586525, 0.039989334186633176, 0.9999999999999521, 1.0002005946249715e-10]
[0.049979169270618774, 0.04997916927067736, 0.9999999999999404, 1.2386956351208683e-10]
[0.059964006479373146, 0.05996400647944364, 0.9999999999999286, 1.4763528451690717e-10]
[0.06994284733744946, 0.06994284733753182, 0.9999999999999168, 1.713521544583937e-10]
[0.07991469396907754, 0.07991469396917175, 0.9999999999999049, 1.9501141335998638e-10]
[0.08987854919790408, 0.08987854919801011, 0.999999999999893, 2.186289030674904e-10]
⋮
[0.7956016200354188, 0.7956016200363661, 0.9999999999990544, 1.8972711315038634e-9]
[0.8016199408828228, 0.8016199408837772, 0.9999999999990473, 1.91148019785049e-9]
[0.807558100404153, 0.8075581004051142, 0.9999999999990407, 1.925374095568946e-9]
[0.8134155047884055, 0.8134155047893737, 0.9999999999990342, 1.9389835864792843e-9]
[0.8191915683000235, 0.8191915683009983, 0.9999999999990274, 1.9527496472919205e-9]
[0.8248857133374683, 0.8248857133384501, 0.9999999999990207, 1.9665913252293982e-9]
[0.8304973704909823, 0.8304973704919705, 0.9999999999990143, 1.979204957292162e-9]
[0.8360259785995253, 0.8360259786005205, 0.9999999999990078, 1.9931070784630663e-9]
[0.8414709848068948, 0.8414709848078965, 0.9999999999990014, -2.000803364251573e-9]