# Ordinary Differential Equations, Applications and Discretizations

##### Chris Rackauckas

##### October 1st, 2020

## Youtube Video Link Part 1

## Youtube Video Link Part 2

Now that we have a sense of parallelism, let's return back to our thread on scientific machine learning to start constructing parallel algorithms for integration of scientific models. We previously introduced discrete dynamical systems and their asymptotic behavior. However, many physical systems are not discrete and are in fact continuous. In this discussion we will understand how to numerically compute ordinary differential equations by transforming them into discrete dynamical systems, and use this to come up with simulation techniques for physical systems.

## What is an Ordinary Differential Equation?

An ordinary differential equation is an equation defined by a relationship on the derivative. In its general form we have that

\[ u' = f(u,p,t) \]

describes the evolution of some variable $u(t)$ which we would like to solve for. In its simplest sense, the solution to the ordinary differential equation is just the integral, since by taking the integral of both sides and applying the Fundamental Theorem of Calculus we have that

\[ u = \int_{t_0}^{t_f} f(u,p,t)dt \]

The difficulty of this equation is that the variable $u(t)$ is unknown and dependent on $t$, meaning that the integral cannot readily be solved by simple calculus. In fact, in almost all cases there exists no analytical solution for $u$ which is readily available. However, we can understand the behavior by looking at some simple cases.

## Solving Ordinary Differential Equations in Julia

To solve an ordinary differential equation in Julia, one can use the DifferentialEquations.jl package to define the differential equation you'd like to solve. Let's say we want to solve the Lorenz equations:

\[ \begin{align} \frac{dx}{dt} &= σ(y-x) \\ \frac{dy}{dt} &= x(ρ-z) - y \\ \frac{dz}{dt} &= xy - βz \\ \end{align} \]

which was the system used in our investigation of discrete dynamics. The first thing we need to do is give it this differential equation. We can either write it in an in-place form `f(du,u,p,t)`

or an out-of-place form `f(u,p,t)`

. Let's write it in the in-place form:

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

lorenz (generic function with 3 methods)

**Question: How could I maybe speed this up a little?**

Next we give an *initial condition*. Here, this is a vector of equations, so our initial condition has to be a vector. Let's choose the following initial condition:

u0 = [1.0,0.0,0.0]

3-element Vector{Float64}: 1.0 0.0 0.0

Notice that I made sure to use `Float64`

values in the initial condition. The Julia library's functions are generic and internally use the corresponding types that you give it. Integer types do not bode well for continuous problems.

Next, we have to tell it the timespan to solve on. Here, let's some from time 0 to 100. This means that we would use:

tspan = (0.0,100.0)

(0.0, 100.0)

Now we need to define our parameters. We will use the same ones as from our discrete dynamical system investigation.

p = (10.0,28.0,8/3)

(10.0, 28.0, 2.6666666666666665)

These describe an `ODEProblem`

. Let's bring in DifferentialEquations.jl and define the ODE:

using DifferentialEquations prob = ODEProblem(lorenz,u0,tspan,p)

ODEProblem with uType Vector{Float64} and tType Float64. In-place: true timespan: (0.0, 100.0) u0: 3-element Vector{Float64}: 1.0 0.0 0.0

Now we can solve it by calling `solve`

:

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]

To see what the solution looks like, we can call `plot`

:

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))

Note that the sentinal to time is `0`

, so we can also do `(t,y,z)`

with:

plot(sol,vars=(0,2,3))

The equation is continuous and therefore the solution is continuous. We can see this by checking how it is at any random time value:

sol(0.5)

3-element Vector{Float64}: 6.503654868503322 -8.508354689912013 38.09199724760152

which gives the current evolution at that time point.

## Differential Equations from Scientific Contexts

### N-Body Problems and Astronomy

There are many different contexts in which differential equations show up. In fact, it's not a stretch to say that the laws in all fields of science are encoded in differential equations. The starting point for physics is Newton's laws of gravity, which define an N-body ordinary differential equation system by describing the force between two particles as:

\[ F = G \frac{m_1m_2}{r^2} \]

where $r^2$ is the Euclidean distance between the two particles. From here, we use the fact that

\[ F = ma \]

to receive differential equations in terms of the accelerations of each particle. The differential equation is a system, where we know the change in position is due to the current velocity:

\[ x' = v \]

and the change in velocity is the acceleration:

\[ v' = F/m = G \frac{m_i}{r_i^2} \]

where $i$ runs over the other particles. Thus we have a vector of position derivatives and a vector of velocity derivatives that evolve over time to give the evolving positions and velocity.

An example of this is the Pleiades problem, which is an approximation to a 7-star chaotic system. It can be written as:

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′ 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,2.0,3.0,-3.0,2.0,0,0,-4.0,4.0,0,0,0,0,0,1.75,-1.5,0,0,0,-1.25,1,0,0],tspan)

ODEProblem with uType Vector{Float64} and tType Float64. In-place: true timespan: (0.0, 3.0) u0: 28-element Vector{Float64}: 3.0 3.0 -1.0 -3.0 2.0 -2.0 2.0 3.0 -3.0 2.0 ⋮ 1.75 -1.5 0.0 0.0 0.0 -1.25 1.0 0.0 0.0

where we assume $m_i = i$. When we solve this equation we receive the following:

sol = solve(prob,Vern8(),abstol=1e-10,reltol=1e-10) plot(sol)

tspan = (0.0,200.0) prob = ODEProblem(pleiades,[3.0,3.0,-1.0,-3.0,2.0,-2.0,2.0,3.0,-3.0,2.0,0,0,-4.0,4.0,0,0,0,0,0,1.75,-1.5,0,0,0,-1.25,1,0,0],tspan) sol = solve(prob,Vern8(),abstol=1e-10,reltol=1e-10) plot(sol,vars=((1:7),(8:14)))

### Population Ecology: Lotka-Volterra

Population ecology's starting point is the Lotka-Volterra equations which describes the interactions between a predator and a prey. In this case, the prey grows at an exponential rate but has a term that reduces its population by being eaten by the predator. The predator's growth is dependent on the available food (the amount of prey) and has a decay rate due to old age. This model is then written as follows:

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)

### Biochemistry: Robertson Equations

Biochemical equations commonly display large separation of timescales which lead to a stiffness phenomena that will be investigated later. The classic "hard" equations for ODE integration thus tend to come from biology (not physics!) due to this property. One of the standard models is the Robertson model, which can be described as:

using Sundials, ParameterizedFunctions function rober(du,u,p,t) y₁,y₂,y₃ = u k₁,k₂,k₃ = p du[1] = -k₁*y₁+k₃*y₂*y₃ du[2] = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃ du[3] = k₂*y₂^2 end prob = ODEProblem(rober,[1.0,0.0,0.0],(0.0,1e5),(0.04,3e7,1e4)) sol = solve(prob,Rosenbrock23()) plot(sol)

plot(sol, xscale=:log10, tspan=(1e-6, 1e5), layout=(3,1))

### Chemical Physics: Pollution Models

Chemical reactions in physical models are also described as differential equation systems. The following is a classic model of dynamics between different species of pollutants:

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 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,k16,k17,k18,k19,k20,k21,k22,k23,k24,k25) function f(dy,y,p,t) k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,k21,k22,k23,k24,k25 = p 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 end

f (generic function with 2 methods)

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.1424841091201048e-7, 1.0937647553427756e-7, 0.0999996667 6440009, 0.3000003350396963, 0.00999998209858389, 5.6037724774041954e-9, 6. 04793833740152e-9, 1.0047570803311952e-8, 5.9812281205517996e-12, 6.2395533 9467492e-9, 2.3022538431539762e-10, 3.1293305822799984e-17, 0.0069999994176 62247, 5.823377531818462e-10, 3.824053893000698e-10, 6.930819262664891e-14] [0.0006836584262738197, 0.19931633629695783, 1.9606016564783863e-10, 0.039 317452226523025, 2.0821314359971558e-7, 2.524263769819259e-7, 0.09999884087 661927, 0.30000116344748634, 0.009999897466494299, 2.0605145128366283e-8, 1 .6844788957556796e-8, 8.136618249763396e-8, 1.072453888584235e-10, 6.486750 944005248e-8, 3.1010416395449594e-9, 3.0986508172589845e-17, 0.006999996444 140231, 3.5558597681092845e-9, 2.0643860873886213e-9, 2.0476275988812484e-1 2] [0.0016428179821615404, 0.19835713123489696, 2.624592441462544e-10, 0.0383 6231097512178, 3.51527310994635e-7, 4.6645725233828817e-7, 0.09999544921365 162, 0.3000045632051474, 0.00999947365101553, 4.4964384052574906e-8, 3.3350 28459989385e-8, 4.812858451973162e-7, 1.4409606354799329e-9, 4.444464501496 5836e-7, 3.745751745418746e-8, 3.0233751050330316e-17, 0.006999981334742691 , 1.8665257309560848e-8, 1.1746742713316081e-8, 6.88603927804064e-11] [0.003246284952583631, 0.19675344580878915, 3.734915472124081e-10, 0.03676 859821471882, 4.3203353758199984e-7, 5.784969040588641e-7, 0.09998753646949 957, 0.30001250073250896, 0.00999841279050207, 5.8477926969890516e-8, 4.228 3730165807424e-8, 1.5155870313913997e-6, 8.524980254717532e-9, 1.4615346080 316135e-6, 2.1422146419315944e-7, 2.897772883427686e-17, 0.0069999433444078 36, 5.6655592163596007e-8, 4.43684966594383e-8, 1.061843105430169e-9] [0.005361393004057219, 0.19463777403944538, 5.199898853042825e-10, 0.03466 7923081446166, 4.301350110500246e-7, 5.629734298792616e-7, 0.09997580962893 295, 0.30002429018799176, 0.00999682035482297, 5.7776445418171186e-8, 4.151 14579790134e-8, 3.075243316490966e-6, 2.7267223985258375e-8, 2.988896303429 074e-6, 6.762049531575716e-7, 2.7322164104134524e-17, 0.006999886274019657, 1.1372598034289984e-7, 1.1359725262068185e-7, 7.943533863137097e-9] [0.008274964935351167, 0.19172294848166438, 7.218269559835728e-10, 0.03177 4179928789587, 3.9826084453929985e-7, 5.005231467491457e-7, 0.0999593539440 8918, 0.30004089901876024, 0.00999460673549988, 5.178145386630543e-8, 3.714 992485079163e-8, 5.229419020007254e-6, 6.87142967579641e-8, 5.0406372336119 75e-6, 1.690961472498024e-6, 2.504157391398737e-17, 0.006999806991160331, 1 .930088396685224e-7, 2.3808903519156548e-7, 4.4409090022701025e-8] [0.013002670675854904, 0.1869919454695578, 1.0493235173420224e-9, 0.027078 334676230022, 3.5965739821341415e-7, 4.23062097241978e-7, 0.099931917193328 49, 0.3000687867927592, 0.009990985412449237, 4.426632357885544e-8, 3.17174 7018445783e-8, 8.707148275200579e-6, 1.7539882287368114e-7, 8.1595418288201 17e-6, 4.282354997367437e-6, 2.134072762166872e-17, 0.006999677462783783, 3 .2253721621608243e-7, 4.292531303120023e-7, 2.4842381839580376e-7] [0.01754950653069441, 0.18244010960656096, 1.3641806124928409e-9, 0.022563 570556883707, 3.361162798275419e-7, 3.7150093428726176e-7, 0.09990309462743 717, 0.30009836544883595, 0.009987255299681627, 3.931687421453348e-8, 2.813 0812802630016e-8, 1.2231150049080628e-5, 3.3462364587005054e-7, 1.103334852 290572e-5, 8.113769678679284e-6, 1.778259332355498e-17, 0.00699954424493387 95, 4.557550661206875e-7, 5.171121404557453e-7, 7.091786398551634e-7] [0.022860660993157974, 0.17711993361265665, 1.7318456679719292e-9, 0.01729 5319170346234, 3.187907551767236e-7, 3.280175981175671e-7, 0.09986309177576 282, 0.30013989594565554, 0.009982163705371506, 3.5196527115901926e-8, 2.51 3676815405124e-8, 1.695687253352232e-5, 6.253075543101416e-7, 1.43919101330 82673e-5, 1.5036820413025796e-5, 1.3630627583154938e-17, 0.0069993626630239 51, 6.373369760492075e-7, 5.039634032907154e-7, 1.6196514074344902e-6] ⋮ [0.03892999723063901, 0.1604402370546666, 2.8511492881988117e-9, 0.0031795 600755990298, 3.0972897906069453e-7, 2.602522829601715e-7, 0.09808114604225 782, 0.30211293753489554, 0.00975642787693632, 2.8857054777614546e-8, 2.050 7650745238614e-8, 0.00021451971258084598, 2.431311049377876e-5, 2.984486304 939218e-5, 0.000580690660980703, 2.505845589866287e-18, 0.00699126005906682 35, 8.739940933174335e-6, 5.644795502477364e-7, 1.2098731834913284e-5] [0.03973179628746286, 0.15934875829742126, 2.9100311071466143e-9, 0.003267 839724450694, 3.0340454593255845e-7, 2.533682719229084e-7, 0.09727159520683 85, 0.3030166682186242, 0.009654512408596435, 2.8039000372424042e-8, 1.9914 463596397295e-8, 0.00030348669691018044, 3.515693408225817e-5, 2.8843324342 22357e-5, 0.0008554037161571975, 2.575419733298135e-18, 0.00698754630872015 4, 1.245369127984427e-5, 6.384025603676961e-7, 1.4123181158100535e-5] [0.04094318678623545, 0.1576850918783431, 2.998971487551486e-9, 0.00340381 57823948058, 2.9398821287552674e-7, 2.4320600172975527e-7, 0.09603188713491 922, 0.304398991005756, 0.009500566541513962, 2.684636948144903e-8, 1.90497 69914690373e-8, 0.00043799308881169863, 5.130948406305046e-5, 2.73117734497 9711e-5, 0.00128637594351225, 2.6825839311824863e-18, 0.006981869477596392, 1.8130522403605977e-5, 7.249055971399556e-7, 1.665550112460086e-5] [0.04265456099862557, 0.15530170721543912, 3.124615506027167e-9, 0.0036015 75222497329, 2.8144779831986016e-7, 2.2972054943080043e-7, 0.09424347009700 187, 0.30638970302322815, 0.009282897844674818, 2.528730342602191e-8, 1.791 9684960259262e-8, 0.0006285309299580904, 7.356402881817139e-5, 2.5311076152 674873e-5, 0.0019296590487066817, 2.838440278932734e-18, 0.0069737007495245 48, 2.6299250475450155e-5, 8.253072229358639e-7, 1.9841700593870297e-5] [0.044793931660118226, 0.15226289579317082, 3.2817111347737033e-9, 0.00385 89221832575414, 2.6689443964468253e-7, 2.14146758626753e-7, 0.0919425173634 1383, 0.30894550076933164, 0.00901015441479903, 2.3520931242225662e-8, 1.66 39788698595913e-8, 0.0008681120853301314, 0.00010022358782918614, 2.3056976 159432398e-5, 0.0027942303053253743, 3.041258194415905e-18, 0.0069632199649 393, 3.678003506069805e-5, 9.439336216189197e-7, 2.3887359967470643e-5] [0.047387957127462486, 0.1484819681937997, 3.4722646550358305e-9, 0.004187 711120538974, 2.506414859123144e-7, 1.9688141701254022e-7, 0.08904810542258 146, 0.31215285289507333, 0.008678047248147584, 2.1605580609099225e-8, 1.52 52579424714224e-8, 0.0011615458041670692, 0.0001303593939193295, 2.06304833 04980964e-5, 0.00393983631162137, 3.3003803021585395e-18, 0.006950068615422 139, 4.993138457785821e-5, 1.0960311780641911e-6, 2.9391471009553192e-5] [0.05036882066239134, 0.14399340437115488, 3.6913545076957686e-9, 0.004591 322479293647, 2.3347510067678807e-7, 1.7884243621649021e-7, 0.0855674941007 5945, 0.3160000520160918, 0.008293774589266533, 1.9649652481043926e-8, 1.38 36777792991087e-8, 0.0015041058995731505, 0.0001612874114336734, 1.81766589 67239272e-5, 0.005401518209025104, 3.6184707672883255e-18, 0.00693427969957 0876, 6.572030042912137e-5, 1.2906023386937793e-6, 3.683937182822643e-5] [0.053649128003848016, 0.13885256634333903, 3.9326277403452165e-9, 0.00507 28523923857325, 2.16010589688742e-7, 1.6077416721927857e-7, 0.0815216569184 575, 0.32046057020734275, 0.007866251982276726, 1.772858450647802e-8, 1.244 7110292692568e-8, 0.001889928666981539, 0.0001897490305316392, 1.5799558796 59144e-5, 0.007213605828677909, 3.9979696855099696e-18, 0.00691593051655176 8, 8.406948344822909e-5, 1.5343173858417756e-6, 4.67082381089522e-5] [0.05646254720110729, 0.13424842010171073, 4.139733781936825e-9, 0.0055231 39725450951, 2.0189808613219891e-7, 1.4645450805851662e-7, 0.07784249512128 365, 0.324507530466733, 0.007494014171885844, 1.6222966604666687e-8, 1.1358 663790538066e-8, 0.0022305051646553066, 0.0002087163072622967, 1.3969348737 814034e-5, 0.008964884997558698, 4.352845989434375e-18, 0.00689921972266994 4, 0.00010078027733005335, 1.7721521050343048e-6, 5.6829620128180964e-5]

plot(sol)

plot(sol, xscale=:log10, tspan=(1e-6, 60), layout=(3,1))

## Geometric Properties

### Linear Ordinary Differential Equations

The simplest ordinary differential equation is the scalar linear ODE, which is given in the form

\[ u' = \alpha u \]

We can solve this by noticing that $(e^{\alpha t})^\prime = \alpha e^{\alpha t}$ satisfies the differential equation and thus the general solution is:

\[ u(t) = u(0)e^{\alpha t} \]

From the analytical solution we have that:

If $Re(\alpha) > 0$ then $u(t) \rightarrow \infty$ as $t \rightarrow \infty$

If $Re(\alpha) < 0$ then $u(t) \rightarrow 0$ as $t \rightarrow \infty$

If $Re(\alpha) = 0$ then $u(t)$ has a constant or periodic solution.

This theory can then be extended to multivariable systems in the same way as the discrete dynamics case. Let $u$ be a vector and have

\[ u' = Au \]

be a linear ordinary differential equation. Assuming $A$ is diagonalizable, we diagonalize $A = P^{-1}DP$ to get

\[ Pu' = DPu \]

and change coordinates $z = Pu$ so that we have

\[ z' = Dz \]

which decouples the equation into a system of linear ordinary differential equations which we solve individually. Thus we see that, similarly to the discrete dynamical system, we have that:

If all of the eigenvalues negative, then $u(t) \rightarrow 0$ as $t \rightarrow \infty$

If any eigenvalue is positive, then $u(t) \rightarrow \infty$ as $t \rightarrow \infty$

### Nonlinear Ordinary Differential Equations

As with discrete dynamical systems, the geometric properties extend locally to the linearization of the continuous dynamical system as defined by:

\[ u' = \frac{df}{du} u \]

where $\frac{df}{du}$ is the Jacobian of the system. This is a consequence of the Hartman-Grubman Theorem.

## Numerically Solving Ordinary Differential Equations

### 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 $\Delta t$. Thus we can approximate

\[ f(u,p,t) = u' = \frac{du}{dt} \approx \frac{\Delta u}{\Delta t} \]

and now since $\Delta u = u_{n+1} - u_n$ we have that

\[ \Delta t f(u,p,t) = u_{n+1} - u_n \]

We need to make a choice as to where we evaluate $f$ at. The simplest approximation is to evaluate it at $t_n$ with $u_n$ where we already have the data, and thus we re-arrange to get

\[ u_{n+1} = u_n + \Delta t f(u,p,t) \]

This is the Euler method.

We can interpret it more rigorously by looking at the Taylor series expansion. First write out the Taylor series for the ODE's solution in the near future:

\[ u(t+\Delta t) = u(t) + \Delta t u'(t) + \frac{\Delta t^2}{2} u''(t) + \ldots \]

Recall that $u' = f(u,p,t)$ by the definition of the ODE system, and thus we have that

\[ u(t+\Delta t) = u(t) + \Delta t f(u,p,t) + \mathcal{O}(\Delta t^2) \]

This is a first order approximation because the error in our step can be expressed as an error in the derivative, i.e.

\[ \frac{u(t + \Delta t) - u(t)}{\Delta t} = f(u,p,t) + \mathcal{O}(\Delta t) \]

### Higher Order Methods

We can use this analysis to extend our methods to higher order approximation by simply matching the Taylor series to a higher order. Intuitively, when we developed the Euler method we had to make a choice:

\[ u_{n+1} = u_n + \Delta t f(u,p,t) \]

where do we evaluate $f$? One may think that the best derivative approximation my come from the middle of the interval, in which case we might want to evaluate it at $t + \frac{\Delta t}{2}$. To do so, we can use the Euler method to approximate the value at $t + \frac{\Delta t}{2}$ and then use that value to approximate the derivative at $t + \frac{\Delta t}{2}$. This looks like:

\[ k_1 = f(u_n,p,t)\\ k_2 = f(u_n + \frac{\Delta t}{2} k_1,p,t + \frac{\Delta t}{2})\\ u_{n+1} = u_n + \Delta t k_2 \]

which we can also write as:

\[ u_{n+1} = u_n + \Delta t f(u_n + \frac{\Delta t}{2} f_n,p,t + \frac{\Delta t}{2}) \]

where $f_n = f(u_n,p,t)$. If we do the two-dimensional Taylor expansion we get:

\[ u_{n+1} = u_n + \Delta t f_n + \frac{\Delta t^2}{2}(f_t + f_u f)(u_n,p,t)\\ + \frac{\Delta t^3}{6} (f_{tt} + 2f_{tu}f + f_{uu}f^2)(u_n,p,t) \]

which when we compare against the true Taylor series:

\[ u(t+\Delta t) = u_n + \Delta t f(u_n,p,t) + \frac{\Delta t^2}{2}(f_t + f_u f)(u_n,p,t) + \frac{\Delta t^3}{6}(f_{tt} + 2f_{tu} + f_{uu}f^2 + f_t f_u + f_u^2 f)(u_n,p,t) \]

and thus we see that

\[ u(t + \Delta t) - u_n = \mathcal{O}(\Delta t^3) \]

### Runge-Kutta Methods

More generally, Runge-Kutta methods are of the form:

\[ k_1 = f(u_n,p,t)\\ k_2 = f(u_n + \Delta t (a_{21} k_1),p,t + \Delta t c_2)\\ k_3 = f(u_n + \Delta t (a_{31} k_1 + a_{32} k_2),p,t + \Delta t c_3)\\ \vdots \\ u_{n+1} = u_n + \Delta t (b_1 k_1 + \ldots + b_s k_s) \]

where $s$ is the number of stages. These can be expressed as a tableau:

The order of the Runge-Kutta method is simply the number of terms in the Taylor series that ends up being matched by the resulting expansion. For example, for the 4th order you can expand out and see that the following equations need to be satisfied:

The classic Runge-Kutta method is also known as RK4 and is the following 4th order method:

\[ k_1 = f(u_n,p,t)\\ k_2 = f(u_n + \frac{\Delta t}{2} k_1,p,t + \frac{\Delta t}{2})\\ k_3 = f(u_n + \frac{\Delta t}{2} k_2,p,t + \frac{\Delta t}{2})\\ k_4 = f(u_n + \Delta t k_3,p,t + \Delta t)\\ u_{n+1} = u_n + \frac{\Delta t}{6}(k_1 + 2 k_2 + 2 k_3 + k_4)\\ \]

While it's widely known and simple to remember, it's not necessarily good. The way to judge a Runge-Kutta method is by looking at the size of the coefficient of the next term in the Taylor series: if it's large then the true error can be larger, even if it matches another one asymptotically.

## What Makes a Good Method?

### Leading Truncation Coefficients

For given orders of explicit Runge-Kutta methods, lower bounds for the number of `f`

evaluations (stages) required to receive a given order are known:

While unintuitive, using the method is not necessarily the one that reduces the coefficient the most. The reason is because what is attempted in ODE solving is precisely the opposite of the analysis. In the ODE analysis, we're looking at behavior as $\Delta t \rightarrow 0$. However, when efficiently solving ODEs, we want to use the largest $\Delta t$ which satisfies error tolerances.

The most widely used method is the Dormand-Prince 5th order Runge-Kutta method, whose tableau is represented as:

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.

### Looking at the Effects of RK Method Choices and Code Optimizations

Pulling from the SciML Benchmarks, we can see the general effect of these different properties on a given set of Runge-Kutta methods:

Here, the order of the method is given in the name. We can see one immediate factor is that, as the requested error in the calculation decreases, the higher order methods become more efficient. This is because to decrease error, you decrease $\Delta t$, and thus the exponent difference with respect to $\Delta t$ has more of a chance to pay off for the extra calls to `f`

. Additionally, we can see that order is not the only determining factor for efficiency: the Vern8 method seems to have a clear approximate 2.5x performance advantage over the whole span of the benchmark compared to the DP8 method, even though both are 8th order methods. This is because of the leading truncation terms: with a small enough $\Delta t$, the more optimized method (Vern8) will generally have low error in a step for the same $\Delta t$ because the coefficients in the expansion are generally smaller.

This is a factor which is generally ignored in high level discussions of numerical differential equations, but can lead to orders of magnitude differences! This is highlighted in the following plot:

Here we see ODEInterface.jl's ODEInterfaceDiffEq.jl wrapper into the SciML common interface for the standard `dopri`

method from Fortran, and ODE.jl, the original ODE solvers in Julia, have a performance disadvantage compared to the DifferentialEquations.jl methods due in part to some of the coding performance pieces that we discussed in the first few lectures.

Specifically, a large part of this can be attributed to inlining of the higher order functions, i.e. ODEs are defined by a user function and then have to be called from the solver. If the solver code is compiled as a shared library ahead of time, like is commonly done in C++ or Fortran, then there can be a function call overhead that is eliminated by JIT compilation optimizing across the function call barriers (known as interprocedural optimization). This is one way which a JIT system can outperform an AOT (ahead of time) compiled system in real-world code (for completeness, two other ways are by doing full function specialization, which is something that is not generally possible in AOT languages given that you cannot know all types ahead of time for a fully generic function, and calling C itself, i.e. c-ffi (foreign function interface), can be optimized using the runtime information of the JIT compiler to outperform C!).

The other performance difference being shown here is due to optimization of the method. While a slightly different order, we can see a clear difference in the performance of RK4 vs the coefficient optimized methods. It's about the same order of magnitude as "highly optimized code differences", showing that both the Runge-Kutta coefficients and the code implementation can have a significant impact on performance.

Taking a look at what happens when interpreted languages get involved highlights some of the code challenges in this domain. Let's take a look at for example the results when simulating 3 ODE systems with the various RK methods:

We see that using interpreted languages introduces around a 50x-100x performance penalty. If you recall in your previous lecture, the discrete dynamical system that was being simulated was the 3-dimensional Lorenz equation discretized by Euler's method, meaning that the performance of that implementation is a good proxy for understanding the performance differences in this graph. Recall that in previous lectures we saw an approximately 5x performance advantage when specializing on the system function and size and around 10x by reducing allocations: these features account for the performance differences noticed between library implementations, which are then compounded by the use of different RK methods (note that R uses "call by copy" which even further increases the memory usages and makes standard usage of the language incompatible with mutating function calls!).

### 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' = \alpha u \]

and ask the question, does the discrete dynamical system defined by the discretized ODE end up going to zero? You would hope that the discretized dynamical system and the continuous dynamical system have the same properties in this simple case, and this is known as linear stability analysis of the method.

As an example, take a look at the Euler method. Recall that the Euler method was given by:

\[ u_{n+1} = u_n + \Delta t f(u_n,p,t) \]

When we plug in the linear test equation, we get that

\[ u_{n+1} = u_n + \Delta t \alpha u_n \]

If we let $z = \Delta t \alpha$, then we get the following:

\[ u_{n+1} = u_n + z u_n = (1+z)u_n \]

which is stable when $z$ is in the shifted unit circle. This means that, as a necessary condition, the step size $\Delta t$ needs to be small enough that $z$ satisfies this condition, placing a stepsize limit on the method.

If $\Delta t$ is ever too large, it will cause the equation to overshoot zero, which then causes oscillations that spiral out to infinity.

Thus the stability condition places a hard constraint on the allowed $\Delta t$ which will result in a realistic simulation.

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

\[ u' = \frac{df}{du}u \]

You can understand the local behavior through diagonalizing this matrix. Therefore, the scalar for the linear stability analysis is performing an analysis on the eigenvalues of the Jacobian. The method will be stable if the largest eigenvalues of df/du are all within the stability limit. This means that stability effects are different throughout the solution of a nonlinear equation and are generally understood locally (though different more comprehensive stability conditions exist!).

### Implicit Methods

If instead of the Euler method we defined $f$ to be evaluated at the future point, we would receive a method like:

\[ u_{n+1} = u_n + \Delta t f(u_{n+1},p,t+\Delta t) \]

in which case, for the stability calculation we would have that

\[ u_{n+1} = u_n + \Delta t \alpha u_n \]

or

\[ (1-z) u_{n+1} = u_n \]

which means that

\[ u_{n+1} = \frac{1}{1-z} u_n \]

which is stable for all $Re(z) < 0$ a property which is known as A-stability. It is also stable as $z \rightarrow \infty$, a property known as L-stability. This means that for equations with very ill-conditioned Jacobians, this method is still able to be use reasonably large stepsizes and can thus be efficient.

### Stiffness and Timescale Separation

From this we see that there is a maximal stepsize whenever the eigenvalues of the Jacobian are sufficiently large. It turns out that's not an issue if the phenomena we see are fast, since then the total integration time tends to be small. However, if we have some equations with both fast modes and slow modes, like the Robertson equation, then it is very difficult because in order to resolve the slow dynamics over a long timespan, one needs to ensure that the fast dynamics do not diverge. This is a property known as stiffness. Stiffness can thus be approximated in some sense by the condition number of the Jacobian. The condition number of a matrix is its maximal eigenvalue divided by its minimal eigenvalue and gives a rough measure of the local timescale separations. If this value is large and one wants to resolve the slow dynamics, then explicit integrators, like the explicit Runge-Kutta methods described before, have issues with stability. In this case implicit integrators (or other forms of stabilized stepping) are required in order to efficiently reach the end time step.

## Exploiting Continuity

So far, we have looked at ordinary differential equations as a $\Delta t \rightarrow 0$ formulation of a discrete dynamical system. However, continuous dynamics and discrete dynamics have very different characteristics which can be utilized in order to arrive at simpler models and faster computations.

### Geometric Properties: No Jumping and the Poincaré–Bendixson theorem

In terms of geometric properties, continuity places a large constraint on the possible dynamics. This is because of the physical constraint on "jumping", i.e. flows of differential equations cannot jump over each other. If you are ever at some point in phase space and $f$ is not explicitly time-dependent, then the direction of $u'$ is uniquely determined (given reasonable assumptions on $f$), meaning that flow lines (solutions to the differential equation) can never cross.

A result from this is the Poincaré–Bendixson theorem, which states that, with any arbitrary (but nice) two dimensional continuous system, you can only have 3 behaviors:

Steady state behavior

Divergence

Periodic orbits

A simple proof by picture shows this.