y

29 downloads 169 Views 2MB Size Report
Numerical Analysis, lecture 13, slide 2. Differential equations describe change ..... k4 = f(x(n)+h(n), y(n,:).'+h(n)*k3); y(n+1,:) = y(n,:) + h(n)/6*(k1+2*k2+2*k3+k4).'; .
Numerical Analysis, lecture 13:

Initial Value Problems (textbook sections 10.1-4, 10.7)

• differential equations ‣ standard form ‣ existence & uniqueness

• solution methods ‣ Euler, Heun, and Runge-Kutta ‣ adaptive step size

y2

y0 y1 x0

h

x1

h

x2

Differential equations describe change over time or space

∂ T = ∇i(κ ∇T ) ∂t

Mz′′ + Qz′ + Kz = 0

Ub

R1

R5

C

R2

(p. 309-310)

4

5

2 1

R4

Ue R3 U0

3

ΔUi 0=∑ + ∑ Ci ΔUi′ + ∑ I i (U ) i Ri i i

y1′ = α1y1 − β1y1y2 y2′ = −α 2 y2 + β2 y1y2

Numerical Analysis, lecture 13, slide  2

A high-order ODE can be written as a system of 1st-order ODEs z ( p) = F(x, z, z′, z′′,…, z ( p−1) ) a p-th order ODE



y =z 1 y = z′ 2 

y′ = f (x, y) p-vectors

y p = z( p−1)

example (p. 311) Mz′′ + Qz′ + Kz = 0 y1 = z, y2 = z′ ⇒ y1′ = y2 , y2′ = −M −1 (Qy2 + Ky1 ) ⎤ ⎡ y1′ ⎤ ⎡ y2 ⎥ ⎢ y′ ⎥ = ⎢ −1 ⎣ 2 ⎦ ⎣ −M (Qy2 + Ky1 ) ⎦  f (x,y)

exercise (p. 355) 3z′′′ + 4 xz′′ + sin z = q(x) Numerical Analysis, lecture 13, slide  3

Initial value problem = differential equation & initial condition

(p. 310)

a differential equation specifies a direction field y′ = f (x, y) y

integral curves

x

initial value problem Find the integral curve that passes through (x0 , y0 ). initial condition

Numerical Analysis, lecture 13, slide  4

Some initial value problems do not have unique solutions

no solution

( y′ )

2

+ y 2 = 0, y(0) = 1 1

solution not unique

y

y′ = y1 3 , y(0) = 0 ⎧⎪ ⇒ y(x) = ⎨ ⎪⎩

(

if x ≤ α

0

)

32 2 (x − α ) 3

0 0

2

x

otherwise

blowup

solution exists only locally y

y′ = 1 + y 2 , y(0) = 1 ⇒ y(x) = tan(x + π 4) if −3π 4 < x < π 4

1 0 0

x

1

Numerical Analysis, lecture 13, slide  5

Lipschitz continuity of f ensures local existence and uniqueness (p. 311-312)

Theorem (p. 312)

If f (x, y) − f (x, y ) ≤ L y − y for all x ∈[a,b] and y, y ∈Ω (closed and bounded), then for any interior point (x0 , y0 ) of [a,b] × Ω the IVP y′ = f (x, y), y(x0 ) = y0 has a unique solution in a neighbourhood of (x0 , y0 ) that can be uniquely continued to the boundary of [a,b] × Ω.

helpful fact examples ( y′ )

If f is differentiable then L = max

[a,b]×Ω

y′ = xy, y(0) = 1 :

∂f ∂y

∂f = x is bounded in nbhd of (0,1) ∂y

2

+ y 2 = 0, y(0) = 1 : Not standard form ∂f 1 −2/3 13 y′ = y , y(0) = 0 : = 3y is unbounded at y = 0 ∂y 2

y′ = 1 + y , y(0) = 1 :

∂f = 2y is bounded in nbhd of (0,1) ∂y Numerical Analysis, lecture 13, slide  6

Euler’s method can be derived in many ways y′ = f (x, y), y(x0 ) = y0

geometry (p. 315-316)

yn+1 = yn + hf (xn , yn )

finite difference f (xn , yn ) = y′(xn ) ≈

(p. 312-313)

y(xn + h) − y(xn ) h

Taylor y2

y0 y1 x0

h

x1

h

x2

y(xn + h) = y(xn ) + hy′(xn ) + 12 h 2 y′′(ξ )

numerical integration y(xn + h) = y(xn ) +

x +h n



f (t, y(t))dt

x

n ≈ hf (xn , y(xn )) Numerical Analysis, lecture 13, slide  7

IVP solved by Euler’s method yn+1 = yn + hf (xn , yn )

example (p. 314) y′ = xy, y(0) = 1 f(x,y)=xy

with h = 0.2 :

y0 = 1 y1 = 1 + 0.2 ⋅ 0 ⋅1 = 1 y2 = 1 + 0.2 ⋅ 0.2 ⋅1 = 1.04

y2 − y(0.4) = 0.0433 exact solution y(x)=exp(x2/2)

with h = 0.1 :

y0 = 1 y1 = 1 + 0.1⋅ 0 ⋅1 = 1 y2 = 1 + 0.1⋅ 0.1⋅1 = 1.01 y3 = 1.01 + 0.1⋅ 0.2 ⋅1.01 = 1.0302 y4 = 1.0302 + 0.1⋅ 0.3 ⋅1.0302 = 1.0611

y4 − y(0.4) = 0.0222

Numerical Analysis, lecture 13, slide  8

Euler’s method in Matlab

(p. 314-315)

function y = eulers(f,x,y0) N = length(x); h = x(2:end)-x(1:end-1); y = zeros(N,length(y0)); y(1,:) = y0(:).'; for n = 1:N-1 y(n+1,:) = y(n,:) + h(n)*f(x(n),y(n,:).').'; end

>> f = @(x,y) x*y; >> x = 0:.1:0.4; >> y = eulers(f,x,1) y = 1.0000 1.0000 1.0100 1.0302 1.0611

Numerical Analysis, lecture 13, slide  9

Euler’s global truncation error is O(h)

local truncation error

(p. 316-318)

proof

is the difference between yn+1 and the value at xn+1 of the integral curve that passes through (xn , yn ). The local truncation error of Euler's method is O(h 2 ).

global truncation error is the difference between yn+1 and the value at xn+1 of the integral curve that passes through (x0 , y0 ).

lte

y2

y0 y1

The global truncation error of Euler's method is O(h).

x0

h

x1

h

x2

Numerical Analysis, lecture 13, slide  10

nput argument x is a vector, and each row of the output array y contains the values of vector corresponding amethod single x value. Getto better accuracy by using several stages 9. Euler’s in Matlab

for each time step

319-321) My code eulers works also for first-order(p. ODE systems, where y and f are vectors. The 1. Get better accuracy by using several stages for each time step input argument x is a vector, and each row of the output array y contains the values of vector corresponding a single value. eun’symethod can be to derived byxapproximating the integral in � xn +h (p. 320) 11. Get better y(x accuracy by using several stages each time step f (t, y(t)) dt for n + h) = y(xn ) + y′ = xy, y(0) = 1

Heun’s 2-stage method example 2 hasmethod gte = ) by approximating the integral in Heun’s can O(h be derived xn

h = 0.2y(x : n + h) in this y the trapezoid rule h2 f (xn , y(xn )) + h2 f (xn + h, y(x�n x+n +h h)), thenwith replacing k1 =approximation f (xn , yn )y(x + ormula by the Euler += hfy(x (xnn). )+ f (t, y(t)) n ynh) k1 =dt0, k2 = 0.2, y1 = 1.02 xn shown to be at least O(h3 ) as follows. k2 = ferror (xn +for h, yHeun’s + hk ) The local truncation method can be n 1 k1 = 0.204, k2 = 0.42432, y2 = 1.082832 ne step of Heun’s method hgives h h , y(xh )) + f (x + h, y(x + h)), then replacing y(x + h) in this by the trapezoid rule f (x yn+1 = yn 2+

kn1 +

n

n n n 2 y2 − y(0.4) =� 0.000433 formula by the1 Euler approximation 2 1 2 � yn + hf (xn ). 3 3 yn+1 The = local yn + truncation hf (xn , ynerror ) + for h Heun’s f (xn , ymethod , y ) + hk f (x , y ) + O(h n ) + hfcan x (xn n 1 y n n be shown to be at least O(h ) as )follows.

k2

2 2 3 One step of Heun’s method 1gives� � 2 = yn + hf (xn , yn ) + h fx (xn , yn ) + f (xn , yn )fy (xn , yn ) + O(h3 ) � 1 1 � 2 yn+1 = yn + hf (xn , yn ) + h f (xn , yn ) + hfx (xn , yn ) + hk1 fy (xn , yn ) + O(h3 ) 2 ∂f /∂y. Denoting by yˆ the exact solution of the here fx denotes ∂f /∂x 2and fy denotes � 1 2� DE with initial=condition yn , h we have yn + hfy(x (xnn, )yn=) + fx (xn , yn ) + f (xn , yn )fy (xn , yn ) + O(h3 ) 2 1 2 �� ∂f /∂y. Denoting � where f denotes ∂f /∂x and f denotes by yˆ the exact solution of the yˆ(xn + h)x = yˆ(xn ) + hˆ y (xn ) y+ h yˆ (xn ) + O(h3 ) ODE with initial condition y(xn ) =2 yn , we have d 1 = yn + hf (xn , yn ) + h2 1f (xn , yn ) + O(h3 ) yˆ(xn + h) = yˆ(xn ) + hˆ y � (x2n ) +dx1h2 yˆ�� (xn ) + O(h3 ) � 1 2�2 = yn + hf (xn , yn ) + h fx (xn , yn ) + f (xn , yn )fy (xn , yn ) + O(h3 ) 2 Numerical Analysis, lecture 13, slide  11 1

proof that lte = O(h ):

3

The classic 4-stage Runge-Kutta method has been popular since 1905 (p. 319-321)

4 This method has gte O(h ) k1 = f (xn , yn ) k2 = f (xn + 12 h, yn + 12 hk1 ) k3 = f (xn + 12 h, yn + 12 hk2 ) k4 = f (xn + h, yn + hk3 )

yn+1 = yn + 16 h ( k1 + 2k2 + 2k3 + k4 )

example (p. 322) y′ = xy, y(0) = 1 with h = 0.4 : y1 − y(0.4) = 0.0000017

Numerical Analysis, lecture 13, slide  12

The Runge-Kutta method in Matlab

function y = rungekutta(f,x,y0) N = length(x); h = x(2:end)-x(1:end-1); y = zeros(N,length(y0)); y(1,:) = y0(:).'; for n = 1:N-1 k1 = f(x(n), y(n,:).'); k2 = f(x(n)+h(n)/2, y(n,:).'+h(n)*k1/2); k3 = f(x(n)+h(n)/2, y(n,:).'+h(n)*k2/2); k4 = f(x(n)+h(n), y(n,:).'+h(n)*k3); y(n+1,:) = y(n,:) + h(n)/6*(k1+2*k2+2*k3+k4).'; end

Error with various h >> >> >> >> >> >> >>

f = @(x,y) x*y; N = 2.^(0:13); for i=1:length(N) y = rungekutta(f,0:0.4/N(i):0.4,1); err(i) = abs(y(end)-exp(0.4^2/2)); end loglog(N,err,'o')

(p. 321-323)

−5

10

1 −10

10

4

−15

10

0

10

2

10 N

4

10

Numerical Analysis, lecture 13, slide  13

The Runge-Kutta method can solve the rocket problem from lecture 1 (p. 3-4)

function dy = rocket(t,y) v = y(2); m = max(180-10*t,0); M = 120+m; dy = [v ((5000+10*v)*(t> t = 0:.1:40; >> Y = rungekutta(@rocket,t,[0 0]); >> plot(t,Y(:,1)) 3000

2500

2000

h

1500

1000

500

0

0

5

10

15

20 t

25

30

35

40

Numerical Analysis, lecture 13, slide  14

Adaptive solvers change the step size according to local truncation error (p. 334)

strategy Compute yn+1 with local truncation error O(h p+1 ) and yn+1 with l.t.e. O(h p+2 ).

Estimate l.t.e. of yn+1 as d = yn+1 − yn+1.

1 ( p+1) ⎛ ⎛τ ⎞ If d > τ , redo the step with smaller step size = max ⎜ 0.8h ⎜ ⎟ , ⎜⎝ ⎝ d⎠

Otherwise, accept yn+1

h⎞ ⎟; 5 ⎟⎠

1 ( p+1) ⎛ ⎞ ⎛τ ⎞ and do next step with larger step size = min ⎜ 0.8h ⎜ ⎟ , 5h ⎟ . ⎜⎝ ⎟⎠ ⎝ d⎠

Numerical Analysis, lecture 14, slide  15

Matlab’s ODE solvers are adaptive

example (p. 336)

(p. 336-337)

y1′ = 1 + y12 y2 − 4 y1, y1 (0) = 1.5 y2′ = 3y1 − y12 y2 , y2 (0) = 3

>> f =@(x,y) [1+y(1)*(y(1)*y(2)-4); y(1)*(3-y(1)*y(2))]; >> options = odeset('AbsTol', 1e-6, 'RelTol', 1e-3, 'Stats','on'); >> [x,y] = ode45(f,[0 20],[1.5;3],options); 46 successful steps 12 failed attempts 349 function evaluations

a step is accepted if |d|≤AbsTol OR |d/y|≤RelTol

5 >> ii = 1:4:length(x); >> plot(x,y,'r-',... x(ii),y(ii,1),'bo',... x(ii),y(ii,2),'b*')

y

0 0

x

20 Numerical Analysis, lecture 14, slide  16

what happened

• ODE initial value problems > standard form y′ = f(x,y), y(x0) = y0 > existence & uniqueness if |∂f/∂y| is bounded

• step-by-step solution > Euler’s method has local truncation error O(h2), global t. e. O(h) > 2-stage Heun method has g.t.e. O(h2) > 4-stage Runge-Kutta has g.t.e. O(h4)

• modern codes use a pair of formulas to estimate local truncation error & thereby automatically adjust the step size

Numerical Analysis, lecture 13, slide  17