APM3711 - 2024 - ASS - 1 - Solution Guide
APM3711 - 2024 - ASS - 1 - Solution Guide
Numerical Analysis II
APM3711
YEAR MODULE
This tutorial letter contains the solution guide for APM3711 Assignment 01
BARCODE
Open Rubric
This solution guide may contain more details than you may be expected to present in your typical
write up of the answer. This is aimed at SEMESTER 01
clarifying certain conceptual steps.
Note that if there is any computer code required, such is not expected to be identical to the one
given as different people may code differently. Required computer code should be an authentic
listing and should be accompanied by a computer printout.
QUESTION 1
Consider the differential equation
SOLUTION:
Given
y 0 (x) = −3x2 y 2 , y (0) = 2
Analytical solution
1
−2
dy = 3x2 dx
y
Z Z
1
∴ − dy = 3x2 dx + c
y2
1
∴ = x3 + c
y
1
∴ = 0 + c if x = 0
2
1
2x3 + 1 / 2
∴ =
y
2
∴ y = .
2x3+1
2
Numerical solution
The algorithm is
Predictor Corrector
where
f (x, y) = −3x2y2.
The program and results follow. For the sake of comparison the calculations were also done using h =
0.2 and 4 corrections at each step. This produced less accurate values than those obtained by using h =
0.1 and 2 corrections, which shows that it is more efficient to reduce the step length than to increase the
number of corrections.
uses printer;
CONST Required computer
xstart = 0.0; listing.
Beware of academic
ystart = 2.0; dishonesty.
xend = 1.0;
corrects = 2;
VAR
x, y, ynew, fold, fnew, h : real;
i, j, n : integer;
fst:text;
3
rewrite(fst);
writeln(fst); writeln(fst);
writeln(fst,’ ********** ASSIGNMENT 1, QUESTION 1 **********’);
writeln(fst);
writeln(fst,’ MODIFIED EULER METHOD’);
writeln(fst,’ Steplength = ’,h:6:4,’ Corrections = ’,corrects);
writeln(fst);
writeln(fst,’ x y Exact y Error’,
’ f(x,y) yp’);
writeln(fst,’ ’,
’ fnew ynew’);
n := round((xend - xstart)/h);
x := xstart;
y := ystart;
write(fst,x:3:1, y:11:6, yexact(x):11:6, (yexact(x) - y):11:6);
FOR i := 1 to n DO
BEGIN
fold := f(x,y);
x := x + h;
ynew := y + h*fold;
writeln(fst,fold:11:6,ynew:11:6);
FOR j := 1 to corrects DO
BEGIN
fnew := f(x,ynew);
ynew := y + h*(fold + fnew)/2;
write(fst,’ ’);
writeln(fst,fnew:11:6,ynew:11:6);
END; {for j}
y := ynew;
write(fst,x:3:1, y:11:6, yexact(x):11:6, (yexact(x) - y):11:6);
END; {for i}
writeln(fst);
close(fst);
END.
Required computer output
********** ASSIGNMENT 1, QUESTION 1 ********** listing
MODIFIED EULER METHOD
Steplength = 0.2000 Corrections = 2
4
0.4 1.762801 1.773050 0.010249 -1.491584 1.464484
-2.316290 1.382013
-2.062758 1.407367
0.6 1.407367 1.396648 -0.010718 -2.139135 0.979540
-1.842236 1.009229
-1.955605 0.997893
0.8 0.997893 0.988142 -0.009750 -1.911916 0.615509
-1.136555 0.693045
-1.440936 0.662607
1.0 0.662607 0.666667 0.004059
5
1.0 0.666788 0.666667 -0.000122
QUESTION 2
Solve the differential equation
dy
= 3x + 2y + xy, y(0) = −1
dx
by means of the Taylor-series expansion to get the value of y at x = 0.1. Use terms up to x6 .
SOLUTION:
Given
dy
= 3x + 2y + xy, y (0) = −1.
dx
6
We approximate y (0.1) by the truncated Taylor series
(0.1)2 00
0 (0.1)6 (6)
y (0.1) ' y (0) + (0.1) y (0) + y (0) + . . . + y (0) (1)
2 6!
The derivatives at x = 0 are calculated in the way illustrated below:
y 0 (x) = 3x + (2 + x) y , y 0 (0) = 2 (−1) = −2
The exact value, rounded to 10 significant numbers, can be calculated by adding more terms of the
Taylor series to (1) until it is clear that further addition of terms will not alter the tenth digit. You
may verify that this gives
y (0.1) = −1.211431726.
Note that we have here calculated y 0 (0) from y (0) , y 00 (0) from y 0 (0) , and so on. Alternatively we
could continue further to derive an expression for y 0 , y 00 etc. in terms of y and x only, as follows:
y 0 (x) = 3x + (2 + x) y,
y 00 (x) = 3 + y + (2 + x) y 0 = 3 + y + (2 + x) (3x + (2 + x) y)
and so on. However, this is unnecessary in this particular problem, and in more complicated differential
equations can lead to quite complicated expressions, hence increasing the possibility of errors creeping
into the manual calculations.
When implementing the Taylor methods in a computer program, we would of course want to find the
expressions in terms of x and y for y 0 (x) , y 00 (x) and so on, since we would need to evaluate these
functions at many different x and y points.
QUESTION 3
Consider the system of coupled second-order differential equations
7
with initial conditions
u(0) = 2, u0 (0) = 1, v(0) = 3, v 0 (0) = −1.
Use the second-order Runge-Kutta method with h = 0.2 and a = 2/3, b = 1/3, α = β = 3/2, to find
u, u0 , v and v 0 at t = 0.2.
SOLUTION:
Given
u00 − (t + 1) uv + v 0 = cos t
v 00 = u0 + uv
First, we convert the system of second–order differential equations to a system of first–order differential
equations, by introducing two new variables which equal the derivatives of the original variables. Let
u0 = w,
v 0 = x,
then we have
w0 = (t + 1) uv − x + cos t,
x0 = w + uv.
To see how the algorithms can be adapted to systems such as (1), note first that the system (1) can
be written as 0
u = U (t, u, v, w, x) u (0) = 2
0
v = V (t, u, v, w, x) v (0) = 3
0 (2)
w = W (t, u, v, w, x) w (0) = 1
0
x = X (t, u, v, w, x) x (0) = −1
where
U (t, u, v, w, x) = w,
V (t, u, v, w, x) = x,
W (t, u, v, w, x) = (t + 1) uv − x + cos t
X (t, u, v, w, x) = w + uv.
The basic idea is to adapt the algorithm for solving problems of the form
8
to solving problems of the form (2), by replacing y by u, v, w, and x, and replacing f by U, V, W
and X. Note that the algorithm is not carried out separately for each unknown, but simultaneously
for all four.
2 1 3
In this case of the second–order Runge–Kutta method with a = , b = , α = β = , the algorithm
3 3 2
for (3) is
2 1
yn+1 = yn + k1 + k2 ,
3 3
k1 = hf (tn , yn ) ,
3 3
k2 = hf tn + h, yn + k1 .
2 2
9
Adapted to the system (2), this gives us the algorithm
2 1
un+1 = un + ku1 + ku2
3 3
2 1
vn+1 = vn + kv1 + kv2
3 3
2 1
wn+1 = wn + kw1 + kw2
3 3
2 1
xn+1 = xn + kx1 + kx2
3 3
ku1 = h U (tn , un , vn , wn , xn )
kv1 = hV (tn , un , vn , wn , xn )
kw1 = hW (tn , un , vn , wn , xn )
kx1 = hX (tn , un , vn , wn , xn )
3 3 3 3 3
ku2 = hU tn + h, un + ku1 , vn + kv1 , wn + kw1 , xn + kx1
2 2 2 2 2
3 3 3 3 3
kv2 = hV tn + h, un + ku1 , vn + kv1 , wn + kw1 , xn + kx1
2 2 2 2 2
3 3 3 3 3
kw2 = hW tn + h, un + ku1 , vn + kv1 , wn + kw1 , xn + kx1
2 2 2 2 2
3 3 3 3 3
kx2 = hX tn + h, un + ku1 , vn + kv1 , wn + kw1 , xn + kx1 .
2 2 2 2 2
Hence, with h = 0.2, we calculate u (0.2) , v (0.2) , u0 (0.2) and v 0 (0.2) from the given initial values as
follows:
10
ku1 = (0.2) U (0, 2, 3, 1, −1) = (0.2) (1) = 0.2
kw1 = (0.2) W (0, 2, 3, 1, −1) = (0.2) [(0 + 1) 2 · 3 − (−1) + cos (0)] = 0.2 · 8 = 1.6
2 1
u (0.2) = 2 + (0.2) + (0.68) = 2.36
3 3
2 1
v (0.2) = 3 + (−0.2) + (0.22) = 2.94
3 3
2 1
u0 (0.2) = w (0.2) = 1 + (1.6) + (0.84887) = 2.595
3 3
2 1
v 0 (0.2) = x (0.2) = −1 + (1.4) + (1.922) = 0.574
3 3
QUESTION 4
Consider the initial value problem
1. Find approximate solutions to this problem in the range 0 ≤ x ≤ 3 using the methods below.
In each case perform the computations for two steplengths, h = 0.1 and h = 0.01. In both
11