04.01.2015 Views

Introduction to Scientific Computing - Tutorial 13: Recapitulation

Introduction to Scientific Computing - Tutorial 13: Recapitulation

Introduction to Scientific Computing - Tutorial 13: Recapitulation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong><br />

Tu<strong>to</strong>rial <strong>13</strong>: <strong>Recapitulation</strong><br />

Bojana Rosic, 1. Februar 20<strong>13</strong>


Relative condition number<br />

Connection between the error in input and error in output:<br />

k(x) := ∂f<br />

∂x (x) x<br />

f (x)<br />

ɛ y = ∆y<br />

y<br />

= k(x)ɛ x = k(x) ∆x<br />

x<br />

This number is important as it shows how good some system is<br />

conditioned. If the relative error in input is ɛ x then the relative error<br />

in output is k(x)ɛ x .<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 2


Difference vs. Differential equations<br />

Difference<br />

x n+1 − x n = 2h 2 n<br />

Differential<br />

dx<br />

dt<br />

= 2t<br />

Apply Euler method<br />

x n+1 − x n = 2h 2 n<br />

Differential equations become difference when we apply numerical<br />

intergation!<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 3


Homogeneous<br />

Difference<br />

x n+1 − x n = 0<br />

Solution form<br />

x n = Cρ n<br />

Characteristic<br />

Differential<br />

dx<br />

dt<br />

= 0<br />

Solution form<br />

x(t) = Ce ρt<br />

Characteristic<br />

ρ − 1 = 0 ρ = 0<br />

x n = C(1) n = C<br />

x = Ce 0t = C<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 4


Homogeneous — roots<br />

Difference<br />

Differential<br />

Single roots ρ i<br />

x n = ∑ i C iρ n i<br />

Single roots ρ i<br />

x(t) = ∑ i C ie ρ it<br />

Double roots ρ 1 Double roots ρ 1<br />

x n = C 1 ρ n 1 + nC 2ρ n 1<br />

x(t) = C 1 e ρ 1t + tC 2 e ρ 1t<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 5


Homogeneous — roots<br />

Difference<br />

Complex ρ = α ± βi<br />

Differential<br />

Complex ρ = α ± βi<br />

µ := √ α 2 + β 2<br />

tan φ = β/α<br />

x n = µ n (Acos(nφ) + iBsin(nφ))<br />

x = e αt (Acos(βt) + iBsin(βt))<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 6


Non-homogeneous- Particular solution<br />

Difference<br />

Differential<br />

x n = x nh + x np<br />

x = x h + x p<br />

x h = Ce pt , x p = c(t)e pt<br />

variation of constant<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 7


Stability<br />

Difference<br />

x n+1 = F(x n )<br />

Differential<br />

dx<br />

dt<br />

= f (x)<br />

x ∗ = F(x ∗ ) f (x ∗ ) = 0<br />

[λ, v] = eig DF(x ∗ ) [λ, v] = eig Df (x ∗ )<br />

(λ) n ⇒ abs (λ) 1 (real)<br />

e λt ⇒ abs (λ) 0 (real)<br />

abs (µ) 1 (complex)<br />

abs (α) 0 (complex)<br />

Remember that eigenvalues are equal <strong>to</strong> roots of charact. eq.!!<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 8


Stability<br />

If there is just one root equal <strong>to</strong> 1 (i.e. 0) ⇒ stable but not asympt.<br />

(1) n = 1, e 0t = 1, n → ∞ & t →→ ∞<br />

If there are more than one root equal <strong>to</strong> 1( i.e. 0) ⇒ not stable<br />

c 1 (1) n +c 2 n(1) n → ∞, c 1 e 0t +c 2 te 0t → ∞, n → ∞ & t →→ ∞<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 9


Zero-stability<br />

Test numerical method on ODE:<br />

for example Euler:<br />

write characteristic equation<br />

ẋ = 0<br />

x n+1 − x n = 0<br />

p − 1 = 0 ⇒ p = 1<br />

and find roots. Check if they are by absolute value less or equal <strong>to</strong><br />

one (THIS IS DIFFERENCE EQ.). If yes then the method is<br />

zero-stable.<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 10


Consistency<br />

Write numerical method for ODE<br />

in a form:<br />

1<br />

h<br />

ẋ = f (t, x)<br />

k∑<br />

x n+k−i = ϕ f (t, x n+k , ..., x n , h)<br />

i=0<br />

Then find characteristic polynomial ρ(ξ) when ϕ f = 0 and its roots<br />

ξ. Then the method is consistent if<br />

and<br />

is equal <strong>to</strong> function f .<br />

ρ(1) = 0<br />

ϕ f (t, x, x, .., x, o)<br />

ρ ′ (1)<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 11


Convergence<br />

Method is convergent if it is consistent and zero-stable.<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 12


Numerical Integration of ODEs<br />

ẋ = f (t, x)<br />

Mid-point rule:<br />

x n+1 = x n + f (t n + h 2 , x n + h 2 f (t n, x n ))<br />

Trapezoidal rule:<br />

)<br />

x n+1 = x n + 1 2<br />

(f h (t n , x n ) + f (t n+1 , x n+1 )<br />

Read also Simpson’s rule, Lagrange interpolation<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite <strong>13</strong>


Linear multi-step methods<br />

k∑<br />

k∑<br />

α j x n+j = h β j f n+j , α k = 1, |α 0 | + |β 0 | ≠ 0<br />

j=0<br />

j=0<br />

where f n+j = f (t n+j , x n+j ).<br />

Euler’s explicit method:<br />

x n+1 = x n + hf (t n , x n )<br />

Euler’s implicit method:<br />

x n+1 = x n + hf (t n+1 , x n+1 )<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 14


Stability of linear multistep method<br />

Dalquist problem:<br />

i.e.<br />

k∑<br />

α j x n+j = h<br />

j=0<br />

Let z := hλ and :<br />

ẋ = λx<br />

k∑<br />

β j f n+j , α k = 1, |α 0 | + |β 0 | ≠ 0<br />

j=0<br />

k∑<br />

α j x n+j = hλ<br />

j=0<br />

ρ(ξ) :=<br />

k∑<br />

β j x n+j<br />

j=0<br />

k∑<br />

α j ξ j<br />

j=0<br />

be characteristic equation for LHS ∑ k<br />

j=0 α jx n+j = 0<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 15


Stability of linear multistep method<br />

and<br />

σ(ξ) := hλ<br />

k∑<br />

β j x j<br />

characteristic polynomial for RHS ∑ k<br />

j=0 β jf n+j = 0. Then stability is<br />

determined by roots<br />

ρ(ξ) − zσ(ξ) = 0<br />

i.e. region:<br />

G s := {z ∈ C : |ξ l | 1and if ξ l is multiple root then |ξ l | < 1}<br />

j=0<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 16


Runge-Kutta methods<br />

The general form of explicit Runge-Kutta method is:<br />

where<br />

s∑<br />

y n+1 = y n + h b i k i<br />

i=1<br />

k 1 = f (t n , y n )<br />

k 2 = f (t n + c 2 h, y n + a 21 k 1 h),<br />

k 3 = f (t n + c 3 h, y n + (a 31 k 1 + a 32 k 2 )h),<br />

.<br />

k s = f (t n + c s h, y n + h(a s1 k 1 + a s2 k 2 + · · · + a s,s−1 k s−1 ))<br />

and b i are coefficients.<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 17


Solving linear system of equations<br />

System:<br />

Gauss-Seidel method:<br />

Ax = b<br />

Lx (k+1) = b − Ux (k)<br />

Jacobi method:<br />

Dx (k+1) = b − Rx (k)<br />

Successive over-relaxation (SOR) method:<br />

x (k+1) = (D + ωL) −1( ωb − [ωU + (ω − 1)D]x (k)) .<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 18


Fixed-point iterations<br />

We want <strong>to</strong> solve<br />

Φ(x) = x<br />

such that<br />

Φ has fixed point (solution)<br />

the fixed point is unique<br />

can be obtained by iterative process<br />

Φ is also called mapping.<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 19


Fixed-point iterations<br />

Banach fixed point<br />

Lipshitz continuity (for q 0):<br />

If this holds when 0 q < 1 then<br />

‖Φ(x) − Φ(y)‖ q‖x − y‖<br />

Φ(x ∗ ) = x ∗ has unique solution<br />

for any initial value sequence x n+1 = Φ(x n ) converges <strong>to</strong><br />

solution x ∗ (this means that iterative method is convergent and<br />

gives the solution)<br />

speed of convergence are given by apriori and aposteriori<br />

estimates<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 20


New<strong>to</strong>n method<br />

F(x) = 0<br />

F(x) = F(x 0 ) + F ′ (x 0 )(x − x 0 ) + h.o.t. = 0<br />

Jacobian:<br />

with elements:<br />

New<strong>to</strong>n iteration:<br />

J(x) = F ′ (x)<br />

J ij = ∂f i<br />

∂x j<br />

x k = x k−1 − F(x k−1)<br />

J(x k−1 )<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 21


Previous homework!<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 22


Previous HW- Runge-Kutta-Fehlberg method<br />

Let us observe the ODE of form:<br />

˙u = f (t, u(t)), u(t 0 ) = u 0<br />

and find the Taylor expansion of solution:<br />

p∑ u k<br />

U := u m+1 = u m +<br />

k! hk + O(h p+1 ).<br />

k=1<br />

The 2(3)-stage Runge–Kutta–Fehlberg-method is given as<br />

k 1 = f (t, u)<br />

k 2 = f (t + c 2 h, u + ha 21 k 1 )<br />

k 3 = f (t + c 3 h, u + ha 31 k 1 + ha 32 k 2 )<br />

2∑<br />

u m+1 = u m + h b i k i , û m+1 = u m + h<br />

i=1<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 23<br />

3∑<br />

ˆb i k i .<br />

i=1


Previous HW- Runge-Kutta-Fehlberg method<br />

As k’s do not depend directly on u and t let us expand it in Taylor<br />

serie:<br />

k 1 = f<br />

k 2 = f + c 2 hf t + ha 21 ff u + h2<br />

2 [c2 2 f tt + 2c 2 a 21 ff tu + a21 2 f 2 f uu ]<br />

k 3 = f + c 3 hf t + h(a 31 k 1 + a 32 k 2 )f u<br />

+ h2<br />

2 [c2 3 f tt + 2c 3 (a 31 k 1 + a 32 k 2 )f tu + (a 31 k 1 + a 32 k 2 ) 2 f uu ]<br />

= f + h[c 3 hf t + (a 31 + a 32 )ff u ]<br />

+ h2<br />

2 [c2 3 f tt + 2c 3 (a 31 + a 32 )ff tu + (a 31 + a 32 ) 2 f 2 f uu ]<br />

+h 2 a 32 (c 2 f t + a 21 ff u )f u .<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 24


Previous HW- Runge-Kutta-Fehlberg method<br />

Then, having that:<br />

2∑<br />

W := u m+1 = u m + h b i k i ,<br />

i=1<br />

3∑<br />

Ŵ := û m+1 = u m + h ˆb i k i .<br />

i=1<br />

we may find the local truncation error:<br />

T h = U − W ,<br />

ˆTh = U − Ŵ<br />

in which we will explit the fact a 31 + a 32 = c 3<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 25


Previous HW- Runge-Kutta-Fehlberg method<br />

T h = U − W<br />

= hf [1 − b 1 − b 2 ] + h 2 [( 1<br />

2 − b 2c 2<br />

)<br />

f t +<br />

+O(h 3 )<br />

ˆT h = U − Ŵ<br />

( 1<br />

2 − b 2a 21<br />

)<br />

ff u<br />

]<br />

( )<br />

= hf [1 − ˆb 1 − ˆb 2 − ˆb 3 ] + h 2[ 1<br />

2 − ˆb 2 c 2 − ˆb 3 c 3 f t<br />

( ) [ 1<br />

+<br />

2 − ˆb 2 a 21 − ˆb<br />

]<br />

3 c 3 ff u + h<br />

3 1<br />

6 − 1 2 ˆb 2 c2 2 − 1 ]<br />

2 ˆb 3 c3<br />

2 f tt +<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 26


Previous HW- Runge-Kutta-Fehlberg method<br />

[ ] [ ]<br />

1 1<br />

+h 3 6 − c 2a 21ˆb2 − c3<br />

2 ff tu + h 3 6 − a2 21ˆb 2 − c3<br />

2 f uu<br />

[ ] [ ]<br />

1 1<br />

+h 3 6 − a 32c 2ˆb3 f t f u + h 3 6 − a 32a 21ˆb3 f 2 fu 2 .<br />

From b 2 a 21 = 1 2 and b 2c 2 = 1 2 follows a 21 = c 2 . Thus, the following<br />

equalities have <strong>to</strong> be fullfilled:<br />

b 1 + b 2 = 1<br />

b 2 c 2 = 1 2<br />

ˆb 1 + ˆb 2 + ˆb 3 = 1<br />

ˆb 2 c 2 + ˆb 3 c 3 = 1 2<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 27


Previous HW- Runge-Kutta-Fehlberg method<br />

ˆb 2 c 2 2 + ˆb 3 c 2 3 = 1 3<br />

a 32 c 2ˆb3 = 1 6 .<br />

Let c 2 and c 3 be free parameters. Then from<br />

b 2 = 1<br />

2c 2<br />

and with<br />

b 1 = 2c 2 − 1<br />

2c 2<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 28


we may find ˆb 2 and ˆb 3 as well as ˆb 1 (solve previous system):<br />

ˆb 1 = 6c 2c 3 + 2 − 3(c 2 + c 3 )<br />

6c 2 c 3<br />

ˆb 2 =<br />

3c 3 − 2<br />

6c 2 (c 3 − c 2 )<br />

ˆb 3 =<br />

2 − 3c 2<br />

6c 3 (c 3 − c 2 )<br />

a 32 = c 3(c 3 − c 2 )<br />

c 2 (2 − 3c 2 )<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 29


Previous HW- Skydiver mechanics<br />

Fall without parachute t = [0, 5]:<br />

m = 140 kg<br />

m dv<br />

dt<br />

= mg − 0.5v, v(0) = 0<br />

Fall with parachute t = [5, 30]:<br />

m dv<br />

dt<br />

= mg − 70v 2<br />

Initial condition in second is the last solution from first.<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 30


Previous HW- Skydiver mechanics I way<br />

First five seconds:<br />

m dv<br />

dt<br />

= mg − 0.5v, v(0) = 0<br />

Algebraic solution:<br />

2m dv<br />

dt<br />

= 2mg − v, v(0) = 0<br />

dv<br />

2mg − v = 1<br />

2m dt<br />

∫<br />

∫<br />

dv 1<br />

2mg − v = 2m dt<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 31


Previous HW- Skydiver mechanics<br />

First five seconds:<br />

∫<br />

∫<br />

dv 1<br />

2mg − v = 2m dt<br />

Take p := 2mg − v then dp = −dv:<br />

∫ dp<br />

p = − ∫ 1<br />

2m dt<br />

i.e.<br />

v = 2mg − Ce − 1<br />

2m t ,<br />

ln p = − 1<br />

2m t + ln C<br />

p = Ce − 1<br />

2m t<br />

v(0) = 0 ⇒ C = 2mg<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 32


Previous HW- Skydiver mechanics II way<br />

First five seconds:<br />

m dv<br />

dt<br />

= mg − 0.5v, v(0) = 0<br />

Algebraic solution:<br />

dv<br />

dt + 1<br />

2m v = g<br />

Thus equation is inhomogeneous. Homogeneous solution comes<br />

from characteristics equation:<br />

i.e. homogeneous solution is:<br />

p + 1<br />

2m = 0 ⇒ p = − 1<br />

2m<br />

v h = C h e − 1<br />

2m t<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 33


Previous HW- Skydiver mechanics II way<br />

Particular solution is obtained by the method of variation of<br />

constant:<br />

v p = C p (t)e − 2m 1 t<br />

Putting back in original ODE:<br />

dv p<br />

dt<br />

= C ′ pe − 1<br />

2m t − 1<br />

2m C p(t)e − t<br />

2m<br />

dv p<br />

dt<br />

+ 1<br />

2m v p = g<br />

C ′ pe − 1<br />

2m t = g ⇒ C p (t) = 2mge t<br />

2m<br />

v p = 2mg<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 34


Previous HW- Skydiver mechanics II way<br />

The final solution is then:<br />

v = v h + v p<br />

v = C h e − t<br />

2m + 2mg<br />

From initial condition v(0) = 0 one obtains:<br />

v(0) = C h + 2mg = 0 ⇒ C h = −2mg<br />

v = 2mg − 2mge − t<br />

2m<br />

= 2mg(1 − e<br />

− t<br />

2m )<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 35


Previous HW- Skydiver mechanics<br />

Let us now solve the second part for time interval after 5s:<br />

m dv<br />

dt<br />

i.e. let us divide it by m:<br />

then<br />

= mg − kv 2 , k = 70<br />

m<br />

k<br />

m<br />

k<br />

dv<br />

dt<br />

mg<br />

k<br />

= mg<br />

k<br />

− v2<br />

dv<br />

= dt<br />

− v2<br />

∫<br />

dv<br />

√ = k ∫<br />

dt<br />

( mg<br />

k )2 − v 2 m<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 36


Previous HW- Skydiver mechanics<br />

∫<br />

dv<br />

√ = k ∫<br />

dt<br />

( mg<br />

k )2 − v 2 m<br />

√<br />

mg<br />

1<br />

k<br />

√ ln | √<br />

2 mg mg<br />

k k<br />

√<br />

mg<br />

k<br />

√<br />

mg<br />

k<br />

+ v<br />

+ v<br />

− v = Ce k m 2√ mg<br />

v =<br />

√<br />

mg<br />

k<br />

− v | = k m t + ln C<br />

k t = Ce 2 √<br />

kg<br />

m t<br />

√<br />

Ce 2 kg<br />

m t − 1<br />

√<br />

Ce 2 kg<br />

m t + 1<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 37


Previous HW- Skydiver mechanics<br />

Initial condition is found from the solution of first ODE:<br />

v(5) = 2mg(1 − e − 5<br />

2m )<br />

and substituted <strong>to</strong>:<br />

Euler method:<br />

v(5) =<br />

√<br />

mg<br />

k<br />

√<br />

Ce 2 kg<br />

m 5 − 1<br />

√<br />

Ce 2 kg<br />

m 5 + 1<br />

v n+1 = v n + hf (v n , t n )<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 38


MATLAB<br />

% main script<br />

clear variables;clc<br />

v=0;<br />

h=0.001;<br />

tstart=0;<br />

tend=30;<br />

[vv,vtrue,tt,err]=jump(h,tstart,tend,v);<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 39


MATLAB<br />

function [vv,vtrue,tt,err]=jump(h,tstart,tend,v)<br />

% jumping function :)<br />

n=abs(tstart−tend)/h;<br />

t=tstart;<br />

vtrue=[];<br />

err=[];<br />

for i=1:n<br />

vv(i)=v;<br />

tt(i)=t;<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 40


MATLAB<br />

if t


MATLAB<br />

function v=funode1(v,t)<br />

v=10−0.5/140*v;<br />

function v=funode2(v,t)<br />

v=10−0.5*v*v;<br />

function vout=fun1(t)<br />

v=dsolve('Dy=10−y/(2*140)','y(0)=0');<br />

vout=subs(v,t);<br />

function vout=fun2(t)<br />

v=dsolve('Dy=10−0.5*y^2','y(5)=49.55');<br />

vout=subs(v,t);<br />

1. Februar 20<strong>13</strong> Bojana Rosic <strong>Introduction</strong> <strong>to</strong> <strong>Scientific</strong> <strong>Computing</strong> Seite 42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!