Professional Documents
Culture Documents
Midterm Examination: MATH 2072U
Midterm Examination: MATH 2072U
Midterm Examination
Instructor: L. van Veen
February 14th, 2018
14:10-15:30
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.
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
(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).
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
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:
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.