Pysics-based simulation xi Differential Equations xi+1 xi xi+1 xi + x x Pysics-based simulation xi Wat is a differential equation? Differential equations describe te relation between an unknown function and its derivatives Newtonian laws gravity wind gust Differential equations Classes of DE xi+1 elastic force... x xi Ordinary differential equation (ODE) xi+1 xi + x Partial differential equation (PDE)
Ordinary differential equations Symbolic solutions An ODE is an equality involving a function and its derivatives known function ẋ(t) f(x(t)) time derivative state of te system Standard introductory differential equation courses focus on finding solutions analytically Linear ODEs can be solved by integral transforms Use DSolveeqn,x,t in Matematica Wat does it mean to solve an ODE? Differential equation: ẋ kx Solution: x e kt Numerical solutions Initial value problems In tis class, we will be concerned wit numerical solutions For eac iteration: 1. use f to calculate te derivative as te direction to next state. use tis direction to approximate cange x over a time interval t 3. increment x by x to obtain new value Derivative function f is regarded as a black box: given numerical values for x and t, returns a numerical value for ẋ In a canonical initial value problem, te beavior of te system is described by an ODE and its initial condition: ẋ f(x, t) x(t 0 ) x 0 To solve x(t) numerically, we start out from x 0 and follow te canges defined by f tereafter
Vector field x Integral curves Te differential equation can be visualized as a vector field Any point on te curve indicates te integral of f from t0 x f (x, t) f (x, t) t0 p x1 How does te vector field look like if f depends directly on time? Numerical metods Get confused yet? x! f (x, t)dt t0 Numerical metods Explicit metods Wat do we know about te system? Explicit Euler s metod Wat are we trying to solve? Te midpoint metod Runge-Kutta metod Implicit metods Implicit Euler s metod
Explicit Euler s metod How do we get to te next state from te current state? x(t0 + ) Problems of Euler s metod Inaccuracy x(t0 + ) x0 + x (t0 ) Te circle turns into a spiral no matter ow small te step size is Instead of following real integral curve, p follows a polygonal pat x(t0 ) p Discrete time step determines te errors Problems of Euler s metod Performance of Euler s metod Cost per step is determined by te number of evaluations per step Instability f kx Oscillation: At eac step, x(t) can be written in te form of Taylor series: Divergence: x(t0 + ) x(t0 ) + x (t0 ) + How small te step size as to be? n n x 3 x (t0 ) + x(3) (t0 ) +...! 3! n! tn Wat is te order of te error term in Euler s metod?
Te midpoint metod Performance of midpoint metod 1. Compute an Euler s step Prove tat te midpoint metod is correct witin O(3) x f (x(t0 )). Evaluate f at te midpoint fmid f (x(t0 ) + x ) 3. Take a step using fmid x(t0 + ) x(t0 ) + fmid x(t + ) x0 + f (x0 + f (x0 )) Runge-Kutta metod 4-stage 4t order Runge-Kutta Runge-Kutta is a numeric metod of integrating ODEs by using a trial step at te midpoint of an interval to cancel out lower-order error terms k1 k k3 k4 x(t0 + ) Te second order Runge-Kutta is known as te midpoint metod 1. f (x0, t0 ) q-stage p-order Runge-Kutta evaluates te derivative function q times in eac iteration and its approximation of te next state is correct witin O(p+1) f (x0, t0 ) f (x0 + k1, t0 + ) f (x0 + k, t0 + ) f (x0 + k3, t0 + ) x0 + 16 k1 + 13 k + 13 k3 + 16 k4 x0. f (x0 + x 3. f (x0 + t k, t0 + ) k1, t0 + ) x(t0 + ) 4. f (x0 + k3, t0 + )
Stage vs. order Adaptive step size p 1 3 4 5 6 7 8 9 10 q min(p) 1 3 4 6 7 9 11 1-17 13-17 Te minimum number of stages necessary for an explicit metod to attain order p is still an open problem Wy is fourt order te most popular Runge Kutta metod? Ideally, we want to coose as large as possible, but not so large as to give us big error or instability We can vary as we marc forward in time Step doubling Embedding estimate Variable step, variable order Step doubling Embedding estimate e Estimate Estimate x a by taking a full Euler step x a x 0 + f(x 0, t 0 ) x b by taking two alf Euler steps x temp x 0 + f(x 0, t 0 ) x b x temp + f(x temp, t 0 + ) e x a x b is bound by O( ) Also called Runge-Kutta-Felberg Compare two estimates of x(t 0 + ) Fift order Runge-Kutta wit 6 stages Fort order Runge-Kutta wit 6 stages Given error tolerance ɛ, wat is te optimal step size? ( ɛ e)1
Variable step, variable order Problems of explicit metods Cange between metods of different order as well as step based on obtained error estimates Tese metods are currently te last work in numerical integration Do not work well wit stiff ODEs Simulation explodes if te step size is too big Simulation progresses slowly if te step size is too small Example: a bead on te wire Stiff equations y(t) x(t) Y(t) (x(t), y(t)) Ẏ d dt ( x(t) y(t) ) ( x(t) ky(t) ) Stiffness constant: k Explicit Euler s metod: Y new Y 0 + Ẏ(t 0) ( x(t) y(t) ) ( x(t) + ky(t) ) Step size is limited by te largest k Systems tat as some big k s mixed in are called stiff system Y new ( (1 )x(t) (1 k)y(t) )
Implicit metods Implicit metod Explicit Euler: Y new Y 0 + f(y 0 ) Implicit Euler: Y new Y 0 + f(y new ) Our goal is to solve for Y new suc tat Y new Y 0 + f(y new ) Approximating f(y new ) by linearizing f(y) f(y new ) f(y 0 ) + Yf (Y 0 ), were Y Y new Y 0 Solving for Y new suc tat! f, at time t 0 +, points directly back at!!! Y 0 Y new Y 0 + f(y 0 ) + Yf (Y 0 ) ( ) 1 1 Y I f (Y 0 ) f(y 0 ) f(y, t) Ẏ(t) f (Y, t) Y(t) Y Example: A bead on te wire Example: A bead on te wire Apply te implicit Euler s metod to te bead-on-wire example ( ) 1 1 Y I f (Y 0 ) f(y 0 ) x(t) f(y(t)) ky(t) f (Y(t)) f(y(t)) Y 1+ Y 0 1+k 0 +1 0 0 1+k +1 x 0 1+k ky 0 1 0 0 k 1 x0 ky 0 x0 ky 0 Wat is te largest step size te implicit Euler s metod can take? lim Y lim +1 x 0 1+k ky 0 x 0 1 k ky 0 Y new Y 0 + ( Y 0 ) 0 x0 y 0
Implicit vs. explicit Second-order implicit Euler ẍ f(x(t), ẋ(t)) correct solution: x() e k x ẋ() kx() x(0) 1 d x(t) dt v(t) v(t) f(x, ẋ) explicit Euler: x() 1 k implicit Euler: x() 1 1 + k. I f f v f(x 0, ẋ 0 ) + f v x x v 0 Helpful ints Modular implementation Write solvers in terms of Reusable solver code Don t use explicit Euler s metod Do use adaptive step size Simple model implementation Generic operations: Get dim(x) Get/Set x and t Derivative evaluation at current (x, t)
Solver interface Summary GetDim How do we solve an ODE numerically? Wat are te drawbacks of Explicit Euler s metod? System Get/Set State Solver Wat is a stiff system? Deriv Eval Wy does implicit Euler allow us to take bigger steps tan explicit metods? Wat s next? How do we use Euler s metod to simulate te motion of a mass point? Wat are te equations of motion wen forces are applied to te mass point?