You are on page 1of 4

MATH 2072U

Midterm Examination
Instructor: L. van Veen
February 14th, 2018
14:10-15:30

1. Consider the nonlinear equation


x exp(− x + 4) − x2 = 0
This equation has a unique solution x ∗ ∈ [1, 4].
(a) [10 marks] Find x ∗ with an error less than 0.1 using bisection. For every iteration, list the left
boundary, the right boundary and an upper bound for the error.
Solution: see table below:
l=1.000000 r=4.000000 err=3.000000e+00
l=2.500000 r=4.000000 err=1.500000e+00
l=2.500000 r=3.250000 err=7.500000e-01
l=2.875000 r=3.250000 err=3.750000e-01
l=2.875000 r=3.062500 err=1.875000e-01
l=2.875000 r=2.968750 err=9.375000e-02
Any point in the interval [2.875000, 2.968750] is an approximation to x ∗ with an error less than or
equal to 0.1.
(b) [10 marks] Starting from the initial guess x (0) = 4, use Newton’s method to find x ∗ with a residual
less than 10−5 . For every iteration, list the approximation xk , the step size | xk+1 − xk | and the
residual.
Solution: see table below:
Iteration 1: x = 4.000000e+00, err=1.090909e+00 res=1.200000e+01
Iteration 2: x = 2.909091e+00, err=1.717107e-02 res=1.974932e-01
Iteration 3: x = 2.926262e+00, err=9.082160e-06 res=1.043480e-04
Iteration 4: x = 2.926271e+00, err=2.550434e-12 res=2.930278e-11
so an approximate solution with a residual less than 10−5 is x = 2.926271.
(c) [5 marks] For each of the following statements, indicate if they are true or false:
1. Since Newton iteration converges to x ∗ starting from one initial point in [1, 4], it must converge
to x ∗ from any initial point in R.
False. There is no guarantee that Newton iteration converges from any point in R and the fact
it converges from some particular initial point says nothing about the convergence from other
initial points.
2. The fact that the equation has a unique solution in [1, 4] is a necessary condition for bisection to
converge.
False. It is a sufficient condition, but bisection can converge even if there are multiple zeros in
the initial interval.
3. Newton iteration takes fewer FLOPS than bisection to compute x ∗ up to 15 significant digits
when starting the former from x (0) = 4 and the latter from [1, 4].
True. Even though one Newton iteration takes about twice as many FLOPs as one bisection
step, the number of necesseray iterations is so much smaller (5 for Newton, 32 for bisection)
that Newton iteration takes far fewer FLOPs in total.
MATH 2072U Midterm examination Page 2 of 4

4. Newton iteration can be generalized to systems of equations with more than one unknown
while bisection works only for a single equation with a single unknown.
True. The generalization of Newton iteration is Newton-Raphson iteration and works for any
number of equations and unknowns. There is no generalization for bisection.
5. If bisection converges, the residual is approximately reduced by the same factor in each itera-
tion.
True. The factor by which the residual goes down is approximately 1/2.

2. [10 marks] Let the matrix A be given by


 
3 −1 1
A=1 −1.5 3 
2.5 6 1.5

Compute the factorisation PA = LU using partial pivoting, where P is a permutation matrix, L is unit
lower triangular, and U is upper triangular. Write down the matrices P, L and U at all intermediate
steps.

     
1 0 0 3 −1 1 1 0 0
0.3333 1 0 0 −1.167 2.667  0 1 0
0.8333 0 1 0 6.833 0.6667 0 0 1
     
1 0 0 3 −1 1 1 0 0
0.8333 1 0 0 6.833 0.6667 0 0 1
0.3333 0 1 0 −1.167 2.667 0 1 0
     
1 0 0 3 −1 1 1 0 0
0.8333 1 0 0 6.833 0.6667 0 1 0
0.3333 −0.1707 1 0 0 2.780 0 0 1
L U P

3. Consider the following linear system:


   
−1 16 0 17.000
Ax = b, where A =  0 4 10 and b = 14.000
1 −4 40 35.000

Using a numerical method, you find the approximate solution to be


 
−0.99900385
x∗ =  0.99991435 
1.0000192

(a) [10 marks] What is the maximal relative error of your approximate solution?

k x − x∗ k k Ax ∗ − bk 0.003172
≤ K ( A) = 714.4 × = 0.05480
kxk kbk 41.35
(b) [5 marks] Assuming that you know the matrix entries exactly, but you know only five digits of the
entries of b, how many digits of the entries of x∗ can you expect to be correct?
The condition number is of order 102 so we can expect 5 − 2 = 3 digits of the approximate solution
to be correct.
MATH 2072U Midterm examination Page 3 of 4

4. A factorisation of an integer n is a pair of integers i and j such that n = i × j. For instance, 14 has the
factorisations 1 × 14 and 2 × 7. Consider the following algorithm to find all factorisations for given n:

For all integers i from 1 to n and for all integers j from 1 to n check if i × j = n. If the equality holds and
i ≤ j then add the pair [i, j] to the list of factorzations of n.

(a) [10 marks] Write a pseudo-code for a function that implements this algorithm. The input should
be a positive integer n and the output should be a list of all factorisations of n.
Example:
Fuction list factors.
Input: integer n > 0.
1. Initialize empty list: L ← [ ].
2. For i = 1 . . . n do
a. For j = 1 . . . n do
i. p ← i × j
ii. If p = n and i ≤ j then append [i, j] to list L.
Output: list L.
(b) [10 marks] Implement your pseudo-code in a Python function called list factors.py.

def list_factors(n):
L = []
for i in range(1,n+1):
for j in range(1,n+1):
p = i*j
if p==n and i <= j:
L.append([i,j])
return L
Instead of including the condition that i ≤ j in line 6 (corresponding to line ii. of the pseudo-code),
you can also let the second loop go over the values i . . . n. This will change the FLOP count in part
(d).
(c) [10 marks] If the list of factorisations returned by your function has only the pair [i, j] = [1, n] then
n is a prime number. Write a pseudo-code for a function that lists all positive prime numbers less
than or equal to some integer N > 0. It should call the function of part (a–b).

Fuction list primes.


Input: integer N > 0.
1. Initialize empty list: P ← [ ].
2. For i = 1 . . . N do
a. L = list primes(i)
b. If L has only one pair then append i to list P.
Return P.
See the course codes repository.
(d) [10 marks] Count the number of flops it takes to execute the pseudo-code of part (c) as a function
of N.
For the function list factors we have 1 FLOP in line i., which is contained in a double loop. The total
is
n n n
#FLOPs = ∑ ∑1= ∑ n = n2
i =1 j =1 i =1
MATH 2072U Midterm examination Page 4 of 4

or, with the inner loop over i . . . n we get


n n n
1 1
#FLOPs = ∑ ∑ 1 = ∑ ( n − i + 1) = n ( n + 1) − 2 n ( n + 1) = 2 n ( n + 1)
i =1 j = i i =1

This function is called in list primes insided a loop over i from 1 to N with i as input argument, so

N
1 1 1 1
#FLOPs = ∑ i2 = 6 N ( N + 1)(2N + 1) = 3 N 3 + 2 N 2 + 6 N = O( N 3 )
i =1

or, with the second loop in list_factors over i . . . n,

1 N
 
1 1 1 1 3 3 2 1
N + N + N = O( N 3 )
2 i∑
#FLOPs = i ( i + 1) = N ( N + 1)(2N + 1) + N ( N + 1) =
=1
2 6 2 6 4 3

Note, that adjusting the inner loop in the function list_factors changes the FLOP count roughly
by a factor of 2, but it does not change the order of complexity.
(e) [10 marks] Implement the pseudo-code of part (c) in a Python function called list primes.py.
(f) [10 marks (bonus)] Write a Python script that uses the codes from parts (b) and (e) and does the
following:
1. Measures the time it takes to generate a list of all positive prime numbers less than or equal to
N, using the code of part (e), for N = 8, 16, 32, . . . , 2048.
2. plots the wall time against N on a logarithmic scale along with a straight line that corresponds
to the flop count of part (d) to leading order. Do the two graphs agree? If not, do you have an
idea why not?
You can find the codes in the course codes repository. Here is an example of the result:

straight line indicates O(N^3)


102

101

100
wall time (s)

10−1

10−2

10−3

10−4

10−5
101 102 103
N
The agreement is good, except for very small N, simply because the overhead (starting the code,
allocation of variables and so on) dominates there.

You might also like