0% found this document useful (0 votes)
18 views

05 Ex Numerical Methods

The document contains 9 problems related to numerical methods for solving ordinary differential equations (ODEs). The problems involve applying Euler's method, the midpoint rule, Heun's method, and Runge-Kutta methods to solve example initial value problems (IVPs) and estimating errors. Approximations are compared to exact solutions where available. Steps are shown to verify the order of accuracy of the numerical methods.

Uploaded by

raafet slimen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

05 Ex Numerical Methods

The document contains 9 problems related to numerical methods for solving ordinary differential equations (ODEs). The problems involve applying Euler's method, the midpoint rule, Heun's method, and Runge-Kutta methods to solve example initial value problems (IVPs) and estimating errors. Approximations are compared to exact solutions where available. Steps are shown to verify the order of accuracy of the numerical methods.

Uploaded by

raafet slimen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MSE

FTP OrdDiff Problem sheet 5: Numerical methods

1. Consider the ivp 


ẋ(t) = cos(t + x) + sin(t − x)
x(0) = 0
Find x(1)
a) Sketch the vector field
b) Manually calculate two steps of Eulers method with h = 0.5 to find an approximation
to x(1)
c) Calculate an approximation to x(1) with Eulers method (using Matlab) for h = 0.1,
h = 0.01 and h = 0.001
d) Use ode45 from MATLAB to find an approximation for x(1).

2. Consider the IVP 


ẋ(t) = t · x(t)
x(0) = 0.1
a) Solve it!
b) Manually calculate an approximation at t = 2 with step size h = 0.4.
c) sketch the exact solution together with approximations for step-sizes h = 0.4, h = 0.2
und h = 0.1.

3. Consider the IVP y 0 = y, y(0) = 1


a) Compare the Taylor-series y(h) = . . . of the exact solution around x = 0 with the
approximation ŷ1 of y(h) by the explicit Euler method, the implicit Euler method and
Heuns method (with stepsize h).

4. Consider any autonomous IVP y 0 = f (y), y(0) = y0 . Can you show, that one step of Heuns
method will always give the correct first-order approximation to the solution y(h)? (We
would be very impressed!)
5. Consider the IVP

ẋ = 100 · (x2 − x3 ), x0 = x(0) = 1 − ∆ < 1

For a small ∆ > 0. The exact solution looks as follows (the circle marks x(0))
1

0.8

0.6
x

0.4

0.2

0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
t

The dashed part is not relevant for us. For t > 0, the solution approaches monotonically
the value x = 1. Despite this ”‘boring”’ behaviour, the ODE is not easy for an explicit
solver.
a) Find the maximum step size (depending on ∆) that guarantees, x̂1 < 1
b) Find the equation for x̂1 for the implicit Euler method. (You don’t need to solve it!)
c) Show that the equation for x̂1 for the implicit Euler method has a solution in the interval
]x0 , 1[ for any stepsize h > 0

6. The value of a company grows with a rate proportional to the square root of its actual
value. Two years ago, it was 1 Mio CHF, now it is 1.44 Mio CHF. When will the value be
2 Mio CHF?
7. Approximate Euler’s number e, using numerical solutions of the IVP

ẋ(t) = x(t)
x(0) = 1
for t = 1 with step width h = 1 (1.e. one step only), with the following methods
a) Explicit Euler
b) Midpoint Rule
c) Heun’s method
d) 4th order Runge-Kutta

8. Consider the IVP 


ẋ(t) = t − x
x(0) = 1
We are intersted in x(2).
a) Find x(2) exactly
b) Use two steps of the midpoint rule with h = 1 (by hand)
c) Use four steps of Heun’s method with h = 0.5 (by hand)
d) Use Heun’s method with step-width h = 0.1, h = 0.01 and h = 0.001 (with a computer)
e) What is the maximum step-width for Heun’s method, if the first 6 decimals of the
numerical approximation x̂(2) must be correct?
9. Consider the IVP 
ẋ(t) = sin(tx)
x(0) = 6
We are interested in x(5).
a) Use the classical Runge-Kutta method with step widths h = 0.5 h = 0.2, h = 0.1,
h = 0.05 und h = 0.01 to find approximations x̂(5).
b) Sketch the exact solution together with approximations for h = 0.5 h = 0.2, h = 0.1,
h = 0.05 and h = 0.01 in the same figure
c) Sketch exact solutions and approximations with step-widths h = 0.5 h = 0.2, h = 0.1,
h = 0.05 and h = 0.01 for the initial conditions x(0) = 1, 2, 3, 4, 5, 6 in the same figure.
Answers for problem sheet 5
1. a) Direction field

x(1) seems to be between 0.5 and 1.


b) Two steps with Eulers method h = 0.5, d.h. t0 = 0, t1 = 0.5, t2 = 1 und f (t, x) =
cos(t + x) + sin(t − x):

x0 = 0
x1 = x0 + hf (t0 , x0 ) = 0 + 0.5 · (cos(0 + 0) + sin(0 − 0)) = 0.5
x2 = x1 + hf (t1 , x1 ) = 0.5 + 0.5 · (cos(0.5 + 0.5) + sin(0.5 − 0.5)) ≈ 0.770

This gives the approximation x(1) ≈ 0.770.

Step size h approximation to x(1)


0.1 0.6718
c)
0.01 0.6558
0.001 0.6542

d) With ode45
x(1) ≈ 0.6540

2. a) Separating the variables one finds


2
x(t) = 0.1e0.5t

b) Five steps of Eulers method h = 0.4, i.e. t0 = 0, t1 = 0.4, t2 = 0.8, t3 = 1.2, t4 = 1.6,
t5 = 2 and f (t, x) = t · x:

x0 = 0.1
x1 = x0 + hf (t0 , x0 ) = 0.1
x2 = x1 + hf (t1 , x1 ) = 0.116
x3 = x2 + hf (t2 , x2 ) = 0.15312
x4 = x3 + hf (t3 , x3 ) ≈ 0.226618
x5 = x4 + hf (t4 , x4 ) ≈ 0.37165

(Exact value: x(2) = 0.7389 . . .)


c) Graphs:

3. a) The exact solution is y(h) = exp(h) = 1 + h + h2 /2 + . . .. The explicit Euler method


gives ŷ1 = 1 + h, i.e. it represents the first order term of the exact solution.

The implicit Euler method gives ŷ1 = 1 + hŷ1 which we can solve explicitely for
1
ŷ1 = = 1 + h + h2 + h3 + . . .
1−h
where we used the geometric series (or equivalently the taylor-expansion of the fraction
1/(1 − h)). The implicit Euler solution gives the correct first order term and contains
(wrong) higher order terms.

Heuns Method gives


h
k1 = 1, k2 = 1 + h, ŷ1 = 1 + (1 + 1 + h) = 1 + h + h2 /2
2
thus it represents the correct approximation to second order.
4. The solution has a taylor expansion
h2 00 h2
y(h) = y0 + hy 0 (0) + y (0) + . . . = y0 + hf (y0 ) + f 0 (y(0))f (y(0)) + . . .
2 2
Heuns method gives
h
ŷ1 = y0 +
(f (y0 ) + f (y0 + hf (y0 ))
2
Expanding the last term to first order gives

f (y0 + hf (y0 )) = f (y0 ) + hf (y0 )f 0 (y0 ) + h2 . . .


And inserting
h h2
ŷ1 = y0 + (f (y0 ) + f (y0 ) + hf (y0 )f 0 (y0 ) + h2 . . .) = y0 + hf (y0 ) + f (y0 )f 0 (y0 ) + . . .
2 2
All terms in the . . . have at least order three in h, so the approximation is indeed correct
to second order in h.
5. a) From
x̂1 = (1 − ∆) + 100h · (∆ − 2∆2 + ∆3 )
one concludes
1 1 1
h< 2

100 1 − 2∆ + ∆ 100
b)
x̂1 = x0 + 100h · (x̂21 − x̂31 )
c) The function
f (x̂) = x0 + 100 h · (x̂2 − x̂3 ) − x̂
satisfies (for 0 < x0 < 1)

f (x0 ) = 100 h · (x20 − x30 ) = 100 h x20 · (1 − x0 ) > 0


f (1) = x0 − 1 < 0

So there is at least one zero of f in ]x0 , 1[.


6. Let x(t) the value at time t. The ODE must be

ẋ = λ x

with λ ∈ R to be found. The general solution is (separation of variables)

λt + C 2
 
x= , C ∈ R.
2
Inserting x(−2) = 106 and x(0) = 1.44 · 106 yields
C 2
= 1.44 · 106

2
−2λ+C 2
2 = 106
So C = 2400 and λ = 200. The solution of the IVP is therefore

x(t) = (100t + 1200)2 .



From the equation x(t0 ) = 2 · 106 we find t0 = 2 · 10 − 12 ≈ 2.142.
7. a) Eulers explicit method:

x1 = x0 + h · f (t0 , x0 )
= 1+1·1
= 2

b) Midpoint rule:

 
h h
x1 = x0 + h · f t0 + , x0 + f (t0 , x0 )
2 2
= 1 + 1 · f (0.5, 1.5)
= 2.5

c) Heuns method:

m1 = f (t0 , x0 ) = 1;
m2 = f (t0 + h, x0 + hm1 )
= f (2, 1 + 1) = 2;
h
x1 = x0 + (m1 + m2 )
2
= 1 + 0.5(1 + 2)
= 2.5
d) Fourth order Runge-Kutta

m1 = f (t0 , x0 ) = 1;
h h
m2 = f (t0 + , x0 + m1 )
2 2
= f (0.5, 1 + 0.5) = 1.5;
h h
m3 = f (t0 + , x0 + m2 )
2 2
= f (0.5, 1 + 1.75) = 1.75;
m4 = f (t0 + h, x0 + hm3 )
= f (1, 3.75) = 2.75
h
x1 = x0 + (m1 + 2m2 + 2m3 + m4 );
6
1
= x0 + (1 + 2 · 1.5 + 2 · 1.75 + 2.75)
6
≈ 2.70833

8. a) closed form solution:


ODE: ẋ = −x + t; homogeneous DGL: ẋ = −x with general solution

x = Ke−t , K ∈ R.

Trial function for xs : xs = at + b with ẋs = a; inserting into the ODE: a = t − (at + b),
thus
0 = 1−a
,
a = −b
a = 1, b = −1 and thus xs = t − 1 the general solution is therefore

x = Ke−t + t − 1, K ∈ R.

With the initial condition x(0) = 1 we get K = 2 and the solution of the IVP is
x = 2e−t + t − 1.
Thus
x(2) = 2e2 − 1 ≈ 1.270670566473225

b) Two steps with the midpoint-rule for h = 1: t0 = 0 , t1 = 1, t2 = 2:

x0 = 1
 
h h
x1 = x0 + h · f t0 + , x0 + f (t0 , x0 )
2 2
= 1 + 1 · f (0.5, 1 + 0.5f (0, 1))
= 1  
h h
x2 = x1 + h · f t1 + , x1 + f (t1 , x1 )
2 2
= 1 + 1 · f (1.5, 1 + 0.5f (1, 1))
= 1.5
c) Four steps with Heuns method and h = 0.5: t0 = 0 , t1 = 0.5, t2 = 1, t3 = 1.5, t4 = 2:

x0 = 1
0.5
x1 = 1 + (−1 + 0) = 0.75
2
0.5
x2 = 0.75 + (−0.25 + 0.375) = 0.78125
2
0.5
x3 = 0.78125 + (0.21875 + 0.609375) = 0.98828125
2
0.5
x4 = 0.98828125 + (0.51171875 + 0.755859375) = 1.30517578125
2

d) Results for different step sizes


ˆ h = 0.1: x(2) ≈ 1.271644915004169
ˆ h = 0.01: x(2) ≈ 1.270679656916392
ˆ h = 0.001: x(2) ≈ 1.270670656764417
e) h ≈ 0.00235
9. a) Fourth order Runge Kutta:
ˆ h = 0.5: x(5) ≈ 4.730126534759491
ˆ h = 0.2: x(5) ≈ 4.705924274348357
ˆ h = 0.1: x(5) ≈ 4.706729484540293
ˆ h = 0.01: x(5) ≈ 4.706804777341525
ˆ h = 0.001: x(5) ≈ 4.706804785185181

b)

c) Approximate and ”‘exact”’ solution curves

You might also like