Book Sciml Ai Notes 07 Ordinary - Differential - Equations Applications - and - Discreti
Book Sciml Ai Notes 07 Ordinary - Differential - Equations Applications - and - Discreti
Notes
02: Serial Code 03: SciML Intro 04: How Loops Work
08: Forward AD 09: Stiff ODEs 10: Reverse AD 11: δP 12: MPI
Ordinary Differential
Equations, Applications
and Discretizations
Chris Rackauckas
u = ∫ f (u, p, t)dt
t0
dx
= σ(y − x) (1)
dt
dy
= x(ρ − z) − y (2)
dt
dz
= xy − βz (3)
dt
function lorenz(du,u,p,t)
du[1] = p[1]*(u[2]-u[1])
du[2] = u[1]*(p[2]-u[3]) - u[2]
du[3] = u[1]*u[2] - p[3]*u[3]
end
u0 = [1.0,0.0,0.0]
3-element Vector{Float64}:
1.0
0.0
0.0
tspan = (0.0,100.0)
(0.0, 100.0)
p = (10.0,28.0,8/3)
using DifferentialEquations
prob = ODEProblem(lorenz,u0,tspan,p)
sol = solve(prob)
retcode: Success
Interpolation: specialized 4th order "free" interpolation, specialized 2nd
order "free" stiffness-aware interpolation
t: 1263-element Vector{Float64}:
0.0
3.5678604836301404e-5
0.0003924646531993154
0.0032624077544510573
0.009058075635317072
0.01695646895607931
0.02768995855685593
0.04185635042021763
0.06024041165841079
0.08368541255159562
⋮
99.30760258626904
99.39665422328268
99.49536147459878
99.58822928767293
99.68983993598462
99.77864535713971
99.85744078539504
99.93773320913628
100.0
u: 1263-element Vector{Vector{Float64}}:
[1.0, 0.0, 0.0]
[0.9996434557625105, 0.0009988049817849058, 1.781434788799208e-8]
[0.9961045497425811, 0.010965399721242457, 2.146955365838907e-6]
[0.9693591634199452, 0.08977060667778931, 0.0001438018342266937]
[0.9242043615038835, 0.24228912482984957, 0.0010461623302512404]
[0.8800455868998046, 0.43873645009348244, 0.0034242593451028745]
[0.8483309847495312, 0.6915629321083602, 0.008487624590227805]
[0.8495036669651213, 1.0145426355349096, 0.01821208962127994]
[0.9139069574560097, 1.4425599806525806, 0.03669382197085303]
[1.088863826836895, 2.052326595543049, 0.0740257368585531]
⋮
[4.669609096878053, 3.061564434452441, 25.1424735017959]
[4.188801916573263, 4.617474401440693, 21.09864175382292]
[5.559603854699961, 7.905631612648314, 18.79323210016923]
[8.556629716266505, 12.533041060088328, 20.6623639692711]
[12.280585075547771, 14.505154761545633, 29.332088452699942]
[11.736883151600804, 8.279294641640229, 34.68007510231878]
[8.10973327066804, 3.2495066495235854, 31.97052076740117]
[4.958629886040755, 2.194919965065022, 26.948439650907677]
[3.8020065515435855, 2.787021797920187, 23.420567509786622]
using Plots
plot(sol)
We can also plot phase space diagrams by telling it which
vars to compare on which axis. Let's plot this in the (x,y,z)
plane:
plot(sol,vars=(1,2,3))
with:
plot(sol,vars=(0,2,3))
sol(0.5)
3-element Vector{Float64}:
6.503654868503322
-8.508354689912013
38.09199724760152
which gives the current evolution at that time point.
F = ma
′
x = v
using OrdinaryDiffEq
function pleiades(du,u,p,t)
@inbounds begin
x = view(u,1:7) # x
y = view(u,8:14) # y
v = view(u,15:21) # x′
w = view(u,22:28) # y′
w = view(u,22:28) # y′
du[1:7] .= v
du[8:14].= w
for i in 15:28
du[i] = zero(u[1])
end
for i=1:7,j=1:7
if i != j
r = ((x[i]-x[j])^2 + (y[i] - y[j])^2)^(3/2)
du[14+i] += j*(x[j] - x[i])/r
du[21+i] += j*(y[j] - y[i])/r
end
end
end
end
tspan = (0.0,3.0)
prob = ODEProblem(pleiades,[3.0,3.0,-1.0,-3.0,2.0,-2.0,
sol = solve(prob,Vern8(),abstol=1e-10,reltol=1e-10)
plot(sol)
tspan = (0.0,200.0)
tspan = (0.0,200.0)
prob = ODEProblem(pleiades,[3.0,3.0,-1.0,-3.0,2.0,-2.0,
sol = solve(prob,Vern8(),abstol=1e-10,reltol=1e-10)
plot(sol,vars=((1:7),(8:14)))
function lotka(du,u,p,t)
du[1] = p[1]*u[1] - p[2]*u[1]*u[2]
du[2] = -p[3]*u[2] + p[4]*u[1]*u[2]
end
p = [1.5,1.0,3.0,1.0]
prob = ODEProblem(lotka,[1.0,1.0],(0.0,10.0),p)
sol = solve(prob)
plot(sol)
k1=.35e0
k2=.266e2
k3=.123e5
k4=.86e-3
k5=.82e-3
k6=.15e5
k7=.13e-3
k8=.24e5
k9=.165e5
k10=.9e4
k11=.22e-1
k12=.12e5
k13=.188e1
k13=.188e1
k14=.163e5
k15=.48e7
k16=.35e-3
k17=.175e-1
k18=.1e9
k19=.444e12
k20=.124e4
k21=.21e1
k22=.578e1
k23=.474e-1
k24=.178e4
k25=.312e1
p = (k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15
function f(dy,y,p,t)
k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16
r1 = k1 *y[1]
r2 = k2 *y[2]*y[4]
r3 = k3 *y[5]*y[2]
r4 = k4 *y[7]
r5 = k5 *y[7]
r6 = k6 *y[7]*y[6]
r7 = k7 *y[9]
r8 = k8 *y[9]*y[6]
r9 = k9 *y[11]*y[2]
r10 = k10*y[11]*y[1]
r11 = k11*y[13]
r12 = k12*y[10]*y[2]
r13 = k13*y[14]
r14 = k14*y[1]*y[6]
r15 = k15*y[3]
r16 = k16*y[4]
r17 = k17*y[4]
r18 = k18*y[16]
r19 = k19*y[16]
r20 = k20*y[17]*y[6]
r21 = k21*y[19]
r22 = k22*y[19]
r23 = k23*y[1]*y[4]
r24 = k24*y[19]*y[1]
r25 = k25*y[20]
dy[1] = -r1-r10-r14-r23-r24+
r2+r3+r9+r11+r12+r22+r25
dy[2] = -r2-r3-r9-r12+r1+r21
dy[3] = -r15+r1+r17+r19+r22
dy[4] = -r2-r16-r17-r23+r15
dy[5] = -r3+r4+r4+r6+r7+r13+r20
dy[6] = -r6-r8-r14-r20+r3+r18+r18
dy[7] = -r4-r5-r6+r13
dy[8] = r4+r5+r6+r7
dy[9] = -r7-r8
dy[10] = -r12+r7+r9
dy[11] = -r9-r10+r8+r11
dy[12] = r9
dy[13] = -r11+r10
dy[14] = -r13+r12
dy[15] = r14
dy[16] = -r18-r19+r16
dy[17] = -r20
dy[18] = r20
dy[19] = -r21-r22-r24+r23+r25
dy[20] = -r25+r24
dy[20] = -r25+r24
end
u0 = zeros(20)
u0[2] = 0.2
u0[4] = 0.04
u0[7] = 0.1
u0[8] = 0.3
u0[9] = 0.01
u0[17] = 0.007
prob = ODEProblem(f,u0,(0.0,60.0),p)
sol = solve(prob,Rodas5())
retcode: Success
Interpolation: specialized 4rd order "free" stiffness-aware interpolation
t: 29-element Vector{Float64}:
0.0
0.0013845590497824308
0.003242540880561935
0.007901605227525086
0.016011572765091416
0.02740615678429701
0.044612092501151696
0.07720629370280543
0.11607398786651008
0.1763063703057767
⋮
3.676321507769747
5.344945477147836
7.985769209063678
11.981251310096694
17.452504634746386
24.882247321193052
34.66462280306778
47.27763232418448
60.0
u: 29-element Vector{Vector{Float64}}:
[0.0, 0.2, 0.0, 0.04, 0.0, 0.0, 0.1, 0.3, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0, 0
.0, 0.0, 0.007, 0.0, 0.0, 0.0]
[0.0002935083676916062, 0.19970649101355778, 1.6938359129632626e-10, 0.039
706733663924174, 1.1424841091201038e-7, 1.0937647553427759e-7, 0.0999996667
6440009, 0.3000003350396963, 0.00999998209858389, 5.6037724774041996e-9, 6.
047938337401518e-9, 1.004757080331196e-8, 5.9812281205517996e-12, 6.2395533
94674922e-9, 2.3022538431539757e-10, 3.1293305822799984e-17, 0.006999999417
662247, 5.823377531818463e-10, 3.824053893000698e-10, 6.930819262664891e-14
]
[0.0006836584262738197, 0.19931633629695783, 1.9606016564783863e-10, 0.039
317452226523025, 2.0821314359971537e-7, 2.5242637698192583e-7, 0.0999988408
7661927, 0.30000116344748634, 0.009999897466494299, 2.0605145128366293e-8,
1.6844788957556812e-8, 8.136618249763386e-8, 1.0724538885842349e-10, 6.4867
5094400525e-8, 3.1010416395449565e-9, 3.0986508172589845e-17, 0.00699999644
4140231, 3.555859768109287e-9, 2.0643860873886213e-9, 2.0476275988812484e-1
2]
[0.0016428179821615404, 0.19835713123489696, 2.624592441462544e-10, 0.0383
6231097512178, 3.5152731099463506e-7, 4.6645725233828785e-7, 0.099995449213
65162, 0.3000045632051474, 0.00999947365101553, 4.4964384052574966e-8, 3.33
5028459989385e-8, 4.812858451973162e-7, 1.4409606354799333e-9, 4.4444645014
965825e-7, 3.7457517454187436e-8, 3.0233751050330316e-17, 0.006999981334742
691, 1.8665257309560828e-8, 1.1746742713316084e-8, 6.886039278040633e-11]
[0.003246284952583631, 0.19675344580878915, 3.734915472124081e-10, 0.03676
[0.003246284952583631, 0.19675344580878915, 3.734915472124081e-10, 0.03676
859821471882, 4.320335375820004e-7, 5.784969040588645e-7, 0.099987536469499
57, 0.30001250073250896, 0.00999841279050207, 5.8477926969890516e-8, 4.2283
73016580745e-8, 1.5155870313913993e-6, 8.524980254717523e-9, 1.461534608031
6128e-6, 2.1422146419315973e-7, 2.897772883427686e-17, 0.006999943344407836
, 5.6655592163595934e-8, 4.43684966594383e-8, 1.0618431054301687e-9]
[0.005361393004057219, 0.19463777403944538, 5.199898853042825e-10, 0.03466
7923081446166, 4.3013501105002817e-7, 5.629734298792673e-7, 0.0999758096289
3295, 0.30002429018799176, 0.00999682035482297, 5.77764454181714e-8, 4.1511
45797901367e-8, 3.0752433164909642e-6, 2.7267223985258345e-8, 2.98889630342
90727e-6, 6.762049531575715e-7, 2.7322164104134524e-17, 0.00699988627401965
7, 1.1372598034289961e-7, 1.1359725262068185e-7, 7.943533863137097e-9]
[0.008274964935351174, 0.19172294848166438, 7.218269559835734e-10, 0.03177
4179928789587, 3.982608445392987e-7, 5.00523146749144e-7, 0.099959353944089
18, 0.30004089901876024, 0.00999460673549988, 5.1781453866305105e-8, 3.7149
92485079143e-8, 5.2294190200072585e-6, 6.871429675796404e-8, 5.040637233611
9715e-6, 1.6909614724980285e-6, 2.504157391398737e-17, 0.006999806991160331
, 1.9300883966852227e-7, 2.3808903519156553e-7, 4.4409090022701e-8]
[0.013002670675854915, 0.1869919454695578, 1.049323517342023e-9, 0.0270783
34676230022, 3.596573982134105e-7, 4.230620972419732e-7, 0.0999319171933284
9, 0.3000687867927592, 0.009990985412449237, 4.426632357885494e-8, 3.171747
018445745e-8, 8.707148275200576e-6, 1.7539882287368083e-7, 8.15954182882010
5e-6, 4.282354997367439e-6, 2.134072762166872e-17, 0.006999677462783783, 3.
2253721621608143e-7, 4.2925313031200287e-7, 2.4842381839580386e-7]
[0.01754950653069443, 0.18244010960656093, 1.3641806124928421e-9, 0.022563
57055688371, 3.3611627982754385e-7, 3.7150093428726456e-7, 0.09990309462743
716, 0.30009836544883595, 0.009987255299681627, 3.931687421453388e-8, 2.813
0812802630264e-8, 1.2231150049080625e-5, 3.3462364587005033e-7, 1.103334852
2905705e-5, 8.113769678679295e-6, 1.7782593323554984e-17, 0.006999544244933
8795, 4.5575506612068657e-7, 5.171121404557456e-7, 7.09178639855165e-7]
[0.02286066099315797, 0.17711993361265663, 1.7318456679719284e-9, 0.017295
319170346234, 3.1879075517672363e-7, 3.280175981175673e-7, 0.09986309177576
28, 0.30013989594565554, 0.009982163705371506, 3.519652711590193e-8, 2.5136
768154051253e-8, 1.6956872533522306e-5, 6.253075543101422e-7, 1.43919101330
8265e-5, 1.5036820413025804e-5, 1.3630627583154936e-17, 0.00699936266302395
2, 6.373369760492062e-7, 5.039634032907158e-7, 1.6196514074344935e-6]
⋮
[0.03892999723063904, 0.16044023705466662, 2.8511492881988138e-9, 0.003179
5600755990337, 3.097289790606945e-7, 2.6025228296017144e-7, 0.0980811460422
5782, 0.30211293753489554, 0.009756427876936317, 2.8857054777614526e-8, 2.0
5076507452386e-8, 0.0002145197125808461, 2.4313110493778758e-5, 2.984486304
939218e-5, 0.0005806906609807025, 2.5058455898662905e-18, 0.006991260059066
824, 8.739940933174326e-6, 5.64479550247737e-7, 1.2098731834913306e-5]
[0.039731796287462894, 0.15934875829742126, 2.910031107146617e-9, 0.003267
8397244506954, 3.0340454593255866e-7, 2.533682719229086e-7, 0.0972715952068
385, 0.3030166682186243, 0.009654512408596433, 2.8039000372424056e-8, 1.991
4463596397305e-8, 0.0003034866969101812, 3.515693408225822e-5, 2.8843324342
223694e-5, 0.0008554037161571975, 2.5754197332981358e-18, 0.006987546308720
155, 1.2453691279844257e-5, 6.384025603676959e-7, 1.4123181158100541e-5]
[0.04094318678623549, 0.15768509187834306, 2.998971487551489e-9, 0.0034038
15782394805, 2.93988212875527e-7, 2.432060017297553e-7, 0.09603188713491922
, 0.3043989910057561, 0.009500566541513962, 2.6846369481449033e-8, 1.904976
991469038e-8, 0.0004379930888116998, 5.130948406305054e-5, 2.73117734497972
47e-5, 0.0012863759435122516, 2.682583931182485e-18, 0.006981869477596393,
1.8130522403605964e-5, 7.249055971399561e-7, 1.6655501124600894e-5]
[0.0426545609986256, 0.15530170721543912, 3.1246155060271694e-9, 0.0036015
75222497331, 2.8144779831986e-7, 2.2972054943080024e-7, 0.09424347009700186
, 0.30638970302322827, 0.009282897844674818, 2.528730342602189e-8, 1.791968
496025925e-8, 0.0006285309299580917, 7.35640288181715e-5, 2.531107615267494
8e-5, 0.0019296590487066839, 2.8384402789327358e-18, 0.006973700749524549,
2.629925047545015e-5, 8.253072229358661e-7, 1.9841700593870354e-5]
[0.04479393166011826, 0.15226289579317082, 3.281711134773705e-9, 0.0038589
221832575444, 2.6689443964468237e-7, 2.141467586267528e-7, 0.09194251736341
221832575444, 2.6689443964468237e-7, 2.141467586267528e-7, 0.09194251736341
381, 0.3089455007693318, 0.00901015441479903, 2.3520931242225642e-8, 1.6639
7886985959e-8, 0.0008681120853301324, 0.0001002235878291863, 2.305697615943
2428e-5, 0.002794230305325378, 3.041258194415907e-18, 0.006963219964939303,
3.6780035060698035e-5, 9.439336216189186e-7, 2.3887359967470623e-5]
[0.047387957127462514, 0.14848196819379966, 3.472264655035832e-9, 0.004187
711120538979, 2.506414859123144e-7, 1.9688141701254014e-7, 0.08904810542258
146, 0.3121528528950735, 0.008678047248147585, 2.1605580609099232e-8, 1.525
2579424714227e-8, 0.0011615458041670701, 0.00013035939391932975, 2.06304833
0498099e-5, 0.003939836311621373, 3.3003803021585434e-18, 0.006950068615422
143, 4.993138457785821e-5, 1.0960311780641968e-6, 2.9391471009553375e-5]
[0.050368820662391366, 0.14399340437115485, 3.6913545076957706e-9, 0.00459
1322479293647, 2.334751006767882e-7, 1.7884243621649027e-7, 0.0855674941007
5945, 0.31600005201609194, 0.008293774589266535, 1.9649652481043943e-8, 1.3
836777792991099e-8, 0.0015041058995731527, 0.0001612874114336738, 1.8176658
967239337e-5, 0.005401518209025111, 3.6184707672883255e-18, 0.0069342796995
7088, 6.572030042912137e-5, 1.2906023386937759e-6, 3.6839371828226355e-5]
[0.05364912800384803, 0.13885256634333903, 3.932627740345218e-9, 0.0050728
52392385736, 2.160105896887418e-7, 1.6077416721927844e-7, 0.081521656918457
5, 0.3204605702073428, 0.007866251982276728, 1.7728584506478013e-8, 1.24471
10292692562e-8, 0.0018899286669815415, 0.0001897490305316394, 1.57995587965
91382e-5, 0.007213605828677921, 3.997969685509972e-18, 0.006915930516551773
, 8.406948344822907e-5, 1.5343173858417779e-6, 4.6708238108952274e-5]
[0.05646254720110732, 0.13424842010171067, 4.139733781936828e-9, 0.0055231
39725450954, 2.01898086132199e-7, 1.4645450805851654e-7, 0.0778424951212836
6, 0.3245075304667331, 0.007494014171885846, 1.622296660466669e-8, 1.135866
3790538066e-8, 0.0022305051646553087, 0.00020871630726229673, 1.39693487378
14003e-5, 0.008964884997558712, 4.352845989434378e-18, 0.006899219722669948
, 0.00010078027733005333, 1.7721521050343056e-6, 5.6829620128181005e-5]
plot(sol)
Geometric Properties
Linear Ordinary Differential Equations
The simplest ordinary differential equation is the scalar linear
ODE, which is given in the form
′
u = αu
αt
u(t) = u(0)e
′
u = Au
′
Pu = DP u
′
z = Dz
du
Euler's Method
To numerically solve an ordinary differential equation, one
turns the continuous equation into a discrete equation by
discretizing it. The simplest discretization is the Euler method.
The Euler method can be thought of as a simple
approximation replacing dt with a small non-infinitesimal Δt
. Thus we can approximate
′
du Δu
f (u, p, t) = u = ≈
dt Δt
2
u(t + Δt) = u(t) + Δtf (u, p, t) + O(Δt )
2
and then use that value to approximate the
derivative at t + . This looks like:
Δt
k1 = f (un , p, t)
Δt Δt
k2 = f (un + k1 , p, t + )
2 2
un+1 = un + Δtk2
Δt Δt
un+1 = un + Δtf (un + fn , p, t + )
2 2
expansion we get:
2
Δt
un+1 = un + Δtfn + (ft + fu f )(un , p, t)
2
3
Δt
2
+ (ftt + 2ftu f + fuu f )(un , p, t)
6
3
u(t + Δt) − un = O(Δt )
Runge-Kutta Methods
More generally, Runge-Kutta methods are of the form:
k1 = f (un , p, t)
un+1 = un + Δt(b1 k1 + … + bs ks )
where s is the number of stages. These can be expressed as a
tableau:
k1 = f (un , p, t)
Δt Δt
k2 = f (un + k1 , p, t + )
2 2
Δt Δt
k3 = f (un + k2 , p, t + )
2 2
Δt
un+1 = un + (k1 + 2k2 + 2k3 + k4 )
6
Notice that this method takes 7 calls to f for 5th order. The
key to this method is that it has optimized leading truncation
error coefficients, under some extra assumptions which allow
for the analysis to be simplified.
Stability of a Method
Simply having an order on the truncation error does not
imply convergence of the method. The disconnect is that the
errors at a given time point may not dissipate. What also
needs to be checked is the asymptotic behavior of a
disturbance. To see this, one can utilize the linear test
problem:
′
u = αu
un+1 = un + Δtαun
For reference, the stability regions of the 2nd and 4th order
Runge-Kutta methods that we discussed are as follows:
Interpretation of the Linear Stability Condition
To interpret the linear stability condition, recall that the
linearization of a system interprets the dynamics as locally
being due to the Jacobian of the system. Thus
′
u = f (u, p, t)
is locally equivalent to
′
df
u = u
du
Implicit Methods
If instead of the Euler method we defined f to be evaluated
at the future point, we would receive a method like:
un+1 = un + Δtαun
or
(1 − z)un+1 = un
1
un+1 = un
1−z
Exploiting Continuity
So far, we have looked at ordinary differential equations as a
Δt → 0 formulation of a discrete dynamical system.