Professional Documents
Culture Documents
EM3solution3 PDF
EM3solution3 PDF
EM3solution3 PDF
y ′ = y 2 t, y(0) = 1.
Take a step size h = 0.1 and verify that the forward Euler approximation
to y(0.1) is y1 = 1.0 and that the approximations to subsequent points
are y2 = 1.01, y3 = 1.0304 and y4 = 1.0623. Compare these estimates
with the exact values y(0.1), y(0.2), y(0.3) and y(0.4) obtained by solving
the ODE algebraically.
Running the forward Euler (f e) process the estimates yj ≈ y(tj ) are
tabulated below. The results are rounded to four decimal places.
h tj yj f (yj , tj ) y(tj )
0.1 0 1.0000 0.0000 1.0000
0.1 0.1 1.0000 0.1000 1.0050
0.1 0.2 1.0100 0.2040 1.0204
0.1 0.3 1.0304 0.3185 1.0471
0.1 0.4 1.0623 0.4514 1.0870
2
Note that the exact solution is the function y(t) = .
2 − t2
2. By taking a step size of h = 0.5, compute the forward Euler approximation
of z(5), where z(t) satisfies the initial value problem:
1
z′ = z, z(0) = 1.
50(1 + t)
h tj zj f (zj , tj ) z(tj )
0.5 0 1.0000 0.0200 1.0000
0.5 0.5 1.0100 0.0135 1.0081
0.5 1 1.0167 0.0102 1.0140
0.5 1.5 1.0218 0.0082 1.0185
0.5 2 1.0259 0.0068 1.0222
0.5 2.5 1.0293 0.0059 1.0254
0.5 3 1.0323 0.0052 1.0281
0.5 3.5 1.0348 0.0046 1.0305
0.5 4 1.0371 0.0041 1.0327
0.5 4.5 1.0392 0.0038 1.0347
0.5 5 1.0411 0.0035 1.0365
1
3. Consider the initial value problem: find y(t) such that
p
y ′ = π 1 − y 2 , y(0) = 0.
Take a step size h = 0.1 and show that computing the forward Euler
approximation to y(1) is problematic.
Running the forward Euler process gives ...
h tj yj f (yj , tj )
0.1 0 0.0000 3.1416
0.1 0.1 0.3142 2.9825
0.1 0.2 0.6124 2.4835
0.1 0.3 0.8608 1.5991
0.1 0.4 1.0207
p
If y = 1.0207, y ′ = π 1 − y 2 cannot be calculated and the method stops.
4. Consider the initial value problem: find y(x) such that
1 x−y
y′ =
e , y(0) = 0.
2
Compute the forward Euler approximation of y(1) using (a) a single step
of size h = 1, (b) two steps each of size h = 0.5, (c) four steps each of size
h = 0.25, and (d) ten steps each of size h = 0.1. Compare your estimates
with the exact value.
2
(d) Running (f e) for h = 0.1 gives
h xj yj f (yj , xj )
0.1 0 0.0000 0.5000
0.1 0.1 0.0500 0.5256
0.1 0.2 0.1026 0.5512
0.1 0.3 0.1577 0.5765
0.1 0.4 0.2153 0.6014
0.1 0.5 0.2755 0.6259
0.1 0.6 0.3381 0.6497
0.1 0.7 0.4030 0.6729
0.1 0.8 0.4703 0.6953
0.1 0.9 0.5398 0.7168
0.1 1 0.6115 0.7374
so y(1) ≈ 0.6115.
x
The solution is y(x) = ln 1+e
2 so y(1) = 0.6201 to four decimal places.
The accuracy of the forward Euler approximation improves when the step
size h is reduced.
These results are clearly much more accurate than those obtained for the
forward Euler approximation scheme (cf. question 2).
Using four steps of size h = 0.25, estimate y(1) using (i) the forward
Euler method, (ii) the modified Euler method. Compare your estimates
with the exact value.
3
The exact solution is the function y(x) = 2e−3x + 7 − 4x. Running the
forward Euler process gives ...
h xj yj f (yj , xj )
0.25 0 9.0000 −10.0000
0.25 0.25 6.5000 −5.5000
0.25 0.5 5.1250 −4.3750
0.25 0.75 4.0313 −4.0938
0.25 1 3.0078
y ′ = y − x, y(0) = 20.
h xn yn K1 x∗ y∗ K2 y(tn )
0.5 0.0 20.0000 20.0000 0.5 30.0000 29.5000 20.0000
0.5 0.5 32.3750 31.8750 1.0 48.3125 47.3125 32.3257
0.5 1.0 52.1719 51.1719 1.5 77.7578 76.2578 52.6474
y ′ = 1 + ln y, y(0) = 1.
Carry out a step of the 4th-order Runge–Kutta method (with step size
h = 0.1) and verify that K1 = 1, K2 = 1.048790, K3 = 1.051111 and
K4 = 1.099946 so that the approximation to y(0.1) is given by y1 =
1.104996.
4
For the next step of the method, show that K1 = 1.099842, K2 =
1.148410, K3 = 1.150501 and K4 = 1.198888 so that y2 = 1.219938.
Continue for one more step and verify that y3 = 1.344702.
Running the Runge–Kutta (rk4) process gives ...
first step
h = 10 t = 0 y0 = 20.0000 K1 = −0.4472
first temporary point t = 5 ym = 17.7639 K2 = −0.4215
second temporary point t = 5 ym = 17.8926 K3 = −0.4230
third temporary point t = 10 y∗ = 15.7700 K4 = −0.3971
new point t = 10 y1 = 15.7779
h = 10 t = 10 y1 = 15.7779 K1 = −0.3972
first temporary point t = 15 ym = 13.7918 K2 = −0.3714
second temporary point t = 15 ym = 13.9210 K3 = −0.3731
third temporary point t = 20 y∗ = 12.0468 K4 = −0.3471
new point t = 20 y2 = 12.0558
h = 10 t = 20 y2 = 12.0558 K1 = −0.3472
first temporary point t = 25 ym = 10.3197 K2 = −0.3212
second temporary point t = 25 ym = 10.4496 K3 = −0.3233
third temporary point t = 30 y∗ = 8.8232 K4 = −0.2970
new point t = 30 y3 = 8.8337
5
10. Consider the initial value problem: find y(x) such that
y ′ = −2xy 2 y(0) = 1.
Carry out two steps of each of the following methods for solving the
equation. (a) forward Euler with h = 1 (b) forward Euler with h = 0.5
(c) modified Euler with h = 1 (d) modified Euler with h = 0.5 (e) Runge–
Kutta with h = 1 (f) Runge–Kutta with h = 0.5. Compare your results
with the exact solution.
h xj yj f (yj , xj ) y(xj )
1 0.0 1.0000 0.0000 1.0000
1 1.0 1.0000 −2.0000 0.5000
1 2.0 −1.0000 0.2000
Unrealistic results.
(b) Running (f e) with h = 0.5 ...
h xj yj f (yj , xj ) y(xj )
0.5 0.0 1.0000 0.0000 1.0000
0.5 0.5 1.0000 −1.0000 0.8000
0.5 1.0 0.5000 0.5000
These values are not particularly accurate but are pointing in the
right direction.
6
(e) Running (rk4) with h = 1 ...
These values are not particularly accurate but are again pointing in
the correct direction.
(f) Running (rk4) with h = 0.5 ...
d2 y dz dy
2
−2 = y + z; y(t0 ) = y0 , (t0 ) = vA ,
dt dt dt
d2 z dy dz
2
+2 = y − z; z(t0 ) = z0 , (t0 ) = vB .
dt dt dt
7
dy dz d2 y
(a) Introducing z = so = 2 gives the following coupled system.
dx dx dx
dy
• = z, y(0) = 1,
dx
dz
• = −12y, z(0) = 0.
dx
dy dz d2 y
(b) Introducing z = so = 2 gives the following coupled system.
dx dx dx
dy
• = z, y(0) = 0,
dx
dz
• = x + 4y − 2z, z(0) = 0.
dx
dy dz d2 y
(c) Introducing z = and w = = 2 gives the following system.
dx dx dx
dy
• = z, y(0) = 1,
dx
dz
• = w, z(0) = 0,
dx
dw
• = x3 + z − y, w(0) = 0.
dx
dp dq d2 p
(d) Introducing q = so = 2 gives the following system.
dt dt dt
dp
• = q, p(0) = 1,
dt
dq q
• = , q(0) = 2.
dt p+t
dy dz
(e) Finally, introducing x = and w = gives the following system
dt dt
of four equations with associated initial conditions.
dy
• = x, y(t0 ) = y0 ,
dt
dx
• = y + z + 2w, x(t0 ) = vA ,
dt
dz
• = w, z(t0 ) = z0 ,
dt
dw
• = y − z − 2x, w(t0 ) = vB .
dt
8
12. Consider the coupled initial value problem
dx dy
= −3y, = 3x, x(0) = 1, y(0) = 0.
dt dt
Approximate the solution of this system using the Runge–Kutta method
with step size h = 0.1. Show that at the first step, the K values are (0, 3),
(−0.45, 3), (−0.45, 2.9325), (−0.87975, 2.865) giving new values x1 and y1
of 0.955338 and 0.2955. Show that the second step gives values x2 and y2
of 0.825349 and 0.564604. Compare these results with the exact values
given by x = cos 3t, y = sin 3t.
Running (rk4) with h = 0.1 ...
h = 0.1 t = 0.00 x0 = 1.000000 y0 = 0.000000 Kf 1 = 0.000000 Kg1 = 3.000000
first temporary point t = 0.05 xm = 1.000000 ym = 0.150000 Kf 2 = -0.450000 Kg2 = 3.000000
second temporary point t = 0.05 xm = 0.977500 ym = 0.150000 Kf 3 = -0.450000 Kg3 = 2.932500
third temporary point t = 0.10 x∗ = 0.955000 y∗ = 0.293250 Kf 4 = -0.879750 Kg4 = 2.865000
new point t = 0.10 x1 = 0.955338 y1 = 0.295500
9
>> [time, zzrk4, nfeval] = rk4([1;-3],0.5,10);
RK4 iteration in 2 dimensions ...
t x y
0.00 1.000000 -3.000000
0.00 1.000000 -3.000000
0.05 0.952381 -2.721088
0.10 0.909091 -2.479338
0.15 0.869566 -2.268430
0.20 0.833334 -2.083332
0.25 0.800000 -1.919998
0.30 0.769231 -1.775146
0.35 0.740741 -1.646088
0.40 0.714286 -1.530609
0.45 0.689656 -1.426869
0.50 0.666667 -1.333330
all done
d2 y dy
+ y + ǫy 3 = 0; y(0) = 1, (0) = 0
dt2 dt
with the value ǫ = 0.1. (This is an example of a Duffing oscillator.)
Compute three steps of Runge–Kutta with a step size h = 0.1.
The first step is to express the second-order problem as a coupled first-
order system.
dy
• = v; y(0) = 1,
dt
dv
• = −y − 0.1y 3 ; v(0) = 0.
dt
Editing rhs and running rk4 gives the following output:
10
15. A sphere floating in water (in the absence of friction and with its centre y
above the water level) experiences a force due to gravity of − 34 πr 3 ρg and
a force due to buoyancy of 13 ρwater π 2r 3 + y(y 2 − 3r 2 ) g. It’s motion is
determined by the ODE
d2 y y3
ρwater y
= −g + g 2+ 3 −3
dt2 4ρ r r
A 0.4 metre radius sphere with density 0.7 of that of water is initially at
rest with its centre 25 cm below the level of the water. You may assume
that g = 10 ms−2 .
Finally, editing rhs and running rk4 gives the following output:
The sphere is quickly floating back to the surface! Running rk4 for longer
times suggests that the sphere tends to bob up and down in the water.
This is illustrated by the following output.
11
>> [time, zzrk4, nfeval] = rk4([-0.25;0],10,100);
RK4 iteration in 2 dimensions ...
t x y
0.00 -0.250000 0.000000
0.00 -0.250000 0.000000
0.10 -0.235369 0.288436
0.20 -0.194009 0.525712
0.30 -0.133823 0.656006
0.40 -0.067990 0.633737
0.50 -0.012547 0.451335
0.60 0.018247 0.151392
0.70 0.016357 -0.188037
0.80 -0.017715 -0.478234
0.90 -0.075068 -0.643951
1.00 -0.140999 -0.647951
1.10 -0.199604 -0.503193
1.20 -0.238239 -0.257729
1.30 -0.249634 0.032871
1.40 -0.231837 0.317951
1.50 -0.187943 0.545862
1.60 -0.126467 0.660830
1.70 -0.061066 0.620233
1.80 -0.007808 0.422030
1.90 0.019567 0.113801
2.00 0.013912 -0.224085
2.10 -0.023284 -0.503314
.
.
.
9.80 -0.009216 -0.422804
9.90 -0.062376 -0.617180
10.00 -0.127317 -0.654832
all done
>> plot(time,zzrk4(1,:),’-kx’), axis square
12
We also define the two finite difference approximations
1 1
• y ′′ (xj ) =
2
(yj−1 − 2yj + yj+1 ), y ′ (xj ) = (yj+1 − yj−1 ).
h 2h
To simplify notation define y1 = A, y2 = B and y3 = C.
The difference approximations at the point x1 = 1.25 are y ′ (xj ) ≈ B−1
0.5 =
2B − 2 and y ′′ (xj ) ≈ B−2A+1
0.252
= −32A + 16B + 16. Substituting these
expressions into the ODE gives
1.252 (−32A + 16B + 16) + 4 × 1.25 (2B − 2) + 4A = 1.253
that is, −46A + 35B = −13.046875.
The difference approximations at the point x2 = 1.5 are y ′ (xj ) ≈ C−A
0.5 =
2C − 2A and y ′′ (xj ) ≈ C−2B+A
0.252 = 16A − 32B + 16C. Substituting these
expressions into the ODE gives
1.52 (16A − 32B + 16C) + 4 × 1.5 (2C − 2A) + 4B = 1.53
that is, 24A − 68B + 48C = 3.375.
The difference approximations at the point x3 = 1.75 are y ′ (xj ) ≈ 6−B
0.5 =
12 − 2B and y ′′ (xj ) ≈ 6−2C+B
0.252
= 96 − 32C + 16B. Substituting these
expressions into the ODE gives
1.752 (96 − 32C + 16B) + 4 × 1.75 (12 − 2B) + 4C = 1.753
that is 35B − 94C = −372.640625.
Solving the 3 linear equations gives the results A = 4.90892(= y1 ), B =
6.07895(= y2 ) and C = 6.2277(= y3 ). Comparing with the exact values
we see that the errors are of order of 0.1.
17. Compute a centered difference approximation to the BVP
d2 y π2
+ y = ǫy 3 , x ∈ (0, 1); y(0) = 1, y(1) = 0,
dx2 4
for the value ǫ = 0.1, using a grid of size h = 0.5. Compare your computed
result with the exact solution at the interior grid point.
Dividing the interval into 2 pieces we introduce the unknown grid point
value y1 ≈ y(0.5). To simplify notation we let A = y1 .
0−1
The difference approximations at the point x1 = 0.5 are y ′ (xj ) ≈ 2×0.5 =
0−2A+1
−1 and y ′′ (xj ) ≈ 0.52 = 4 − 8A. Substituting these expressions into
the ODE gives
4 − 8A + (π 2 /4)A = 0.1A3 .
This is a nonlinear equation. To solve it we set up a Newton iteration
with f (A) = 4 + (π 2 /4 − 8)A − 0.1A3 so that f ′ (A) = π 2 /4 − 8 − 0.3A2 .
The iteration converges to the solution A = 0.7163 (= y1 ).
A |f (A)| f ′ (A)
0.0000 4.0000 −5.5326
0.7230 0.0377 −5.6894
0.7163 0.0000 −5.6865
13
The exact solution solution to the BVP when ǫ = 0 is the function
y(x) = cos( πx2 ) so that y(0.5) = 0.7071. The difference approximation
is surprisingly accurate.
18. An iron bar is kept at temperatures T0 and T1 at its two ends. The
radiation of heat at point x is proportional its temperature −AT (x) and
2
this effect is balanced by thermal diffusion, that is, −κ ddxT2 . Thus, when
the temperature T (x) has reached equilibrium, it satisfies the BVP
d2 T
−κ = −AT, x ∈ (0, 1); T (0) = T0 , T (1) = T1 .
dx2
Write down a centered difference approximation to T (x) for values of x of
0.25, 0.5 and 0.75 and solve the resulting linear system in the case where
A = 1, κ = 1, T0 = 1 and T1 = 2.
Multiplying by minus one and substituting parameter values gives
d2 T
−T =0 with T (0) = 1 and T (1) = 2.
dx2
Let’s define grid values so that T (0.25) ≈ P , T (0.5) ≈ Q and T (0.75) ≈ R.
The difference approximation at the point x1 = 0.25 is T ′′ (xj ) ≈ Q−2P +1
0.252
=
16Q − 32P + 16. Substituting this expression into the ODE gives
14
19. Imagine two metal bars, one with ends maintained at temperature T =
600 and the other with a temperature maintained at T = 400. As well
as thermal diffusion and general radiation, there is heat transferred from
one bar to the other. The bars thus satisfy a coupled system of ODEs
d2 T1
−0.01 + 0.01 T1 + 0.005 (T1 − T2 ) = 0,
dx2
d2 T2
−0.01 + 0.01 T2 + 0.005 (T2 − T1 ) = 0,
dx2
subject to T1 (0) = T1 (1) = 600 and T2 (0) = T2 (1) = 400.
Take a grid with points x0 = 0, x1 = 0.25, x2 = 0.5, x3 = 0.75 and x4 = 1
and compute a centered difference approximation to T1 and T2 .
First we introduce new functions y = 0.01T1 and z = 0.01T2 and substi-
tute into the ODEs. This gives the rescaled equations:
y ′′ − 1.5y + 0.5z = 0
z ′′ + 0.5y − 1.5z = 0
15