Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential...

63
Ordinary Differential Equations COS 323

Transcript of Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential...

Page 1: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Ordinary Differential Equations

COS 323

Page 2: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Ordinary Differential Equations (ODEs)

• Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are linked by having similar differential equations – electrical circuits

– Newtonian mechanics

– chemical reactions

– population dynamics

– economics… and so on, ad infinitum

• ODEs: 1 independent variable (PDEs have more)

Page 3: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

ODE Example: RLC circuit

∫++= dtICdt

dILRIV 1

LVq

LCdtdq

LR

dtqd

=++1

2

2

Page 4: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

ODE Example: Population Dynamics

• 1798 Malthusian catastrophe

• 1838 Verhulst, logistic growth

• Predator-prey systems, Volterra-Lotka

Page 5: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Malthusian Population Dynamics

rNdtdN

= rteNN 0=

Yikes! Population explosion!

Page 6: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Verhulst: Logistic growth

( )11 0

0

−+= rt

KN

rt

eeNN( )K

NrNdtdN

−= 1

Self-limiting

Page 7: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Predator-Prey Population Dynamics

Hudson Bay Company

Page 8: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Predator-Prey Population Dymanics

V .Volterra, commercial fishing in the Adriatic

x1= biomass of predators (sharks)

x2 = biomass of prey (fish)

12122

21212

1

1 xbaxxaxb

xx

−=−=

Page 9: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

As Functions of Time

Page 10: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

State-Space Diagram: The x1-x2 Plane

Page 11: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

More Behaviors

22212122

21212

1

1 xcxbaxxaxb

xx

−−=−=

Self-limiting term → stable focus

Delay → limit cycle

Page 12: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Varieties of Behavior

• Stable focus

• Periodic

• Limit cycle

Page 13: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Varieties of Behavior

• Stable focus

• Periodic

• Limit cycle

• Chaos

Page 14: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Terminology

• Order: highest order of derivative determines order of ODE

• Explicit: Can express k-th derivative in terms of lower orders

• Implicit: More general

mFydt

tydmdt

tdytytF

/

)()(),(, 2

2

=′′

=

y(k ) = f (t, y, ′ y , ′ ′ y ,..., y(k −1))′ ′ y = F /m

f (t,y, ′ y , ′ ′ y ,...,y(k )) = 0

Page 15: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Notational Conventions

• t is independent variable (scalar for ODEs)

• y is dependent variable – may be vector-valued

• focus exclusively here on explicit, first-order ODEs:

• Special case: f does not depend explicitly on t: autonomous ODE

′ y = f (t,y) where f : ℜn +1 →ℜn

′ y = f (y)

Page 16: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Transforming a higher-order ODE into a system of first-order ODEs

Page 17: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Newton’s second law as first-order system

Page 18: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Solving ODEs

Page 19: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

What does it mean to solve an ODE?

• Analytically: transform f(t, y, y’, y’’… y(k)) into equation of form y = …

• Numerically: use f(t, y, y’, y’’… y(k)) to compute approximations of y for discrete values of t – e.g., (y1, t1), (y2, t2), …(yn, tn)

e.g., transform dydx

= −2x 3 −12x 2 − 20x + 8.5

into y = −0.5x 4 + 4x 3 −10x 2 + 8.5x + C

Page 20: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Analytically-derived solution

dy/dt y

Page 21: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Numerically-derived Solution

Page 22: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

ODEs have many solutions

initial value

Page 23: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

IVP vs BVP

• Today: Initial Value Problems – Complete state known at t=t0

• As opposed to Boundary Value Problems – Parts of state known at multiple values of t

Page 24: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

ODEs and integration

• If y’ = f(t, y) and y(t0) = y0, then

• This directly useful only if f is independent of y, but helps us understand why there are so many parallels to numerical integration

y(t) = y0 + f (s,y(s))dst0

t∫

Page 25: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Numerical Methods for ODEs

Page 26: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Need for numerical methods

• Linear ODEs are nice: an(t) y(n)+…a1(t) y’+a0(t) y = f(t)

• No analytical solutions for most nonlinear ODEs

• Can sometimes locally linearize non-linear ODEs; e.g., pendulum equation

d2θdt

+gl

sinθ = 0

can be estimated as d2θdt

+gl

θ = 0

Page 27: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Numerical methods for ODEs

• Can’t solve many (most) interesting problems analytically

• Numerical methods find yk at a discrete set of tk given f(y, t) and y0

• Important considerations: – Accuracy / error analysis

– Efficiency: running time, number of steps

– Stability: will estimate of y(tk) diverge from true value?

Page 28: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

“Simplest possible” method

• Known:

• What is y1 at time t1=t0 + h?

dydt

= f (t, y)

y = y0 at t = t 0

y1 = y0 + f (t0,y0)h

t0 t1

y0 y0+f(t0,y0)h Euler’s method

Page 29: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Forward (Explicit) Euler’s method

• Can repeat for subsequent estimates:

yi+1 = yi + f (ti,yi)h

Page 30: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Example from Chapra & Canale

Solve dydt

= −2t 3 −12t 2 − 20t + 8.5

for t =1 given y =1 at t = 0, and for step size 0.5 :

Step 1:y(0.5) = y(0) + f (0,1)*0.5where y(0,1) = − 2(0)3 + 12(0)2 - 20(0) +8.5 = 8.5so y(0.5) = 5.25

Step 2 :y(1.0) = y(0.5) + f (0.5,5.25) *0.5 = 5.25 + [−2(0.5)3 +12(0.5)2 − 20(0.5) + 8.5]*0.5

Page 31: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Sequence of Euler solutions

Page 32: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Error analysis of Euler’s method

Derive yi+1 using Taylor series expansion around (ti, yi):

Euler’s method uses first two terms of this, so we have truncation error:

yi+1 = yi + f (ti, yi)h + ′ f (ti,yi)h2

2!+ ...+

f (n −1)(ti,yi)hn

n!+ O(hn +1)

Et = ′ f (ti,yi)h2

2!+ ...+ f (n −1)(ti,yi)h

n

n!+ O(hn +1)

E = O(h2)This is local error. Works perfectly if solution is linear: it’s a first-order method

Page 33: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Local and Global Error

Page 34: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Local and Global error

Page 35: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Error analysis, in general

• Local error: concerned with accuracy at each step – Euler’s method: O(h2)

• Global error: concerned with stability over multiple steps – Euler’s method: O(h)

• In general, for nth-order method: – Local error O(hn+1), global error O(hn)

• Stability is not guaranteed

Page 36: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Stability of ODE

Page 37: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Stable

Page 38: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Asymptotically Stable

Page 39: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Stability of Method

• Possible to have instability (divergence from true solution) even when solutions to ODE are stable

• Euler’s method sensitive to choice of h: – Consider dy/dt = –λy

– Analytic solution is y(t) = y0 e–λt

– Forward Euler step is yk+1 = yk – λykh = yk (1 – λh)

– Euler’s method unstable if h > 2/λ

Other methods often have better stability.

Page 40: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Higher Order: Runge-Kutta Methods

Page 41: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Taylor Series Methods

Page 42: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Why not use TS methods?

• Requires higher-level derivatives of y

• Ugly and hard to compute!

• More efficient higher-order methods exist

Page 43: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Runge-Kutta

• Family of techniques

• Achieves accuracy of Taylor Series without needing higher derivatives

• Accomplishes this by evaluating f several times between tk and tk+1

Page 44: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Runge-Kutta: General Form

)...,(

),(),(

),( and

... where),,(

11,122,111,11

22212123

11112

1

2211

1

hkqhkqhkqyhptfk

hkqhkqyhptfkhkqyhptfk

ytfk

kakakahhytyy

nnnnninin

i

ii

ii

nn

iiii

−−−−−−

+

+++++=

+++=++=

=

+++=+=

φφ

Page 45: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Euler as R-K

• Let n = 1

yi+1 = yi + φ(ti,yi,h)hwhere φ = a1k1

and k1 = f (ti,yi)a1 =1

Page 46: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Higher-Order RK

• Midpoint method • 4th-order Runge Kutta

)()2/(

)(

3)()1(

)(

)(

hObyyayfhb

yfha

kk

k

k

++=+⋅=

⋅=

+

)()22(

)()2/()2/(

)(

561)()1(

)(

)(

)(

)(

hOdcbayy

cyfhdbyfhcayfhb

yfha

kk

k

k

k

k

+++++=

+⋅=+⋅=+⋅=

⋅=

+

Page 47: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

4th order RK

From Chapra & Canale

Page 48: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Usual Bag of Tricks: Extrapolation

• Richardson: compute for several values of h, combine to cancel error: higher-order method – As with integration, yields some “classical”

algorithms: Euler + Richardson → Runge Kutta

• Burlisch-Stoer: fit function (polynomial or rational) to approximation as a function of h; extrapolate to h=0

Page 49: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Usual Bag of Tricks: Adaptive Solvers

• Change step size to get better accuracy when function is changing quickly

Page 50: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Usual Bag of Tricks: Adaptive Solvers

• Change step size to get better accuracy when function is changing quickly

• Determine appropriate step size by estimating error – Method 1: Halve the RK step size and compare

results: Error ~ y2 – y1

– Method 2: Compute RK predictions of different order

Page 51: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Better Stability: Implicit Methods

Page 52: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Need for Implicit Methods

• We saw that Euler’s method becomes unstable with sufficiently small step size – Same for RK, and all the methods we’ve seen

• Even for “nice” functions – dy/dt = –λy → y(t) = y0 e–λt

• Can we avoid this by always using step sizes on the order of “fastest-moving” component of solution (i.e., t ~ 1/ λ)? No!

Page 53: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Stiff ODE

• May involve transients, rapidly oscillating components: rates of change much smaller than interval of study

from Chapra & Canale

Page 54: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Another Stiff ODE

Page 55: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

See http://www.cse.illinois.edu/iem/ode/stiff/

Page 56: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Backward (Implicit) Euler

• Compare to Forward (Explicit) Euler:

• Local error still O(h2)

• Stable for large step size! (At least on )

• In general, requires nonlinear root finding

• Implicit and semi-implicit methods for higher orders

hytfyy iiii ),( 111 +++ +=

yi+1 = yi + f (ti, yi)h

yy λ−=

Page 57: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Predictor-Corrector Methods

Page 58: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Heun’s method

• Forward Euler: Assumes derivative at ti is a good estimate for whole interval

• Heun: want to average derivative at ti, ti+1

ti ti+1

yi yi+f(ti,yi)h

yi+1 = yi +f (ti,yi) + f (ti+1,yi+1)

2h

Page 59: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Heun’s method

• To actually do this, predict yi+1, then use slope at yi+1 to correct the prediction

• Predictor:

• Corrector:

hytfyy iiii ),()0(1 +=+

hytfytfyy iiiiii 2

),(),( )0(11

1++

++

+≈

Page 60: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Heun: An iterative method!

• Can apply corrector once (so it’s a 2nd order RK) or iteratively

• Corrector:

• Error estimate:

– guaranteed to converge to something, not necessarily 0

• Error might not decrease monotonically, but should decrease eventually for sufficiently small h

hytfytfyyk

iiiii

ki 2

),(),( )1(1)(

1

−+

++

+=

E =yi+1

j − yi+1j −1

yi+1j

Page 61: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Heun: Example

Solve dydt

= 4e0.8t − 0.5y

for t =1 given y = 2 at t = 0, and for step size 1:

3)2(5.042),(:Predict 1, Step

0000

)0(1 =−+=+= ehytfyy

701082.6)1(2402164.632

2),(),(

:Correct 2, Step)0(

11000

)1(1 =

++=

++= hytfytfyy

275811.62

),(),(:againCorrect 3, Step

)1(1100

0)2(

1 =+

+= hytfytfyy

Page 62: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Error of Heun’s method

• Local: O(h3)

• Global: O(h2) (i.e., it’s a 2nd-order method)

Page 63: Ordinary Differential Equations€¦ · Ordinary Differential Equations (ODEs) •Differential equations are ubiquitous: the lingua franca of the sciences. Many different fields are

Relationship between Heun and Trapezoid

• when dy/dt depends only on t:

)(2

)()(

)(

)(

)(/

11

1

11

11

iiii

ii

t

tii

t

t

y

y

tttftfyy

dttfyy

dttfdy

tfdtdy

i

i

i

i

i

i

−+

+≈

=−

=

=

++

+

+ ∫∫∫

+

++