CS计算机代考程序代写 python algorithm CSC336H1F

CSC336H1F
Sample Midterm Test 2 February 2018
Duration: Aids Allowed:
50 minutes
A calculator.
A single 8.5 by 11 inch aid sheet, written on one side only.
The aid sheet must be a handwritten original copy. No photocopies.
Student Number: Family Name: First Name:
Do not turn this page until you have received the signal to start. Please fill out the identification section above.
This test consists of 5 questions on 3 double-sided pages (including this one). When you receive the signal to start, please make sure that your copy of the test is complete.
#1: /6
#2: /5
#3: /5
#4: /5
#5: /4
TOTAL: /25
This test is double-sided.
University of Toronto
Page 1 of 6
cont’d. . .
Good Luck

CSC336H1F Sample Midterm Test 2 February 2018
Question 1. [6 marks] Part (a) [2 marks]
A floating-point number system is characterized by four integers: the base β, the precision p, and the lower and upper limits L and U, respectively, of the exponent range.
If β = 10, what are the smallest values of p and U, and the largest value of L, such that both 2365.27 and 0.0000512 can be represented exactly in the resulting normalized floating-point system? Explain.
Part (b) [2 marks]
Consider the floating-point system F (β, p, L, U ) = (2, 24, −126, 127) and suppose that proper rounding is used when mapping from the reals to F. What real numbers x will have the property that fl(1.0+x) = 1.0? Justify your response.
Part (c) [2 marks]
Show that for any n-vector x, ∥x∥∞ ≤ ∥x∥1 ≤ n ∥x∥∞.
University of Toronto Page 2 of 6 cont’d. . .

CSC336H1F Sample Midterm Test 2 February 2018 Question 2. [5 marks]
Ronald needs to evaluate cos(π/2) but he does not remember his trigonometry very well. He does remember the approximation π ≈ 22/7, though. Here is a copy of Ronald’s Python3 session:
>>> import math
>>> p = 22 / 7
>>> cos_p_by_2 = math.cos( p / 2 )
>>> print( “math.cos( p / 2 ) = {:10.4e}”.format( cos_p_by_2 ) )
math.cos( p / 2 ) = -6.3224e-04
Diane recalls that the Python3 math module defines a constant named pi. Here is a copy of Diane’s Python3 session:
>>> import math
>>> cos_pi_by_2 = math.cos( math.pi / 2 )
>>> print( “math.cos( math.pi / 2 ) = {:10.4e}”.format( cos_pi_by_2 ) )
math.cos( math.pi / 2 ) = 6.1232e-17
Diane’s results disturb Ronald. He knows that 22/7 is only an approximation to π (and math.pi), but since the relative error that is made when approximating π by 22/7 is roughly 4.02e-04, he had expected the relative error in his approximation to cos(π/2) to be of a similar size.
Part (a) [1 mark]
Assuming that math.cos( math.pi / 2 ) = 6.1232e-17 is a correct result, what is the relative error in
Ronald’s approximation math.cos( p / 2 ) to math.cos( math.pi / 2 )?
Part (b) [3 marks]
Explain in detail why Ronald computed a result with such a large relative error.
Part (c) [1 mark]
You know that cos(π/2) = 0. Explain why Python3 does not calculate math.cos( math.pi / 2 ) = 0.
University of Toronto Page 3 of 6 cont’d. . .

CSC336H1F Sample Midterm Test 2 February 2018
Question 3. [5 marks] Part (a) [2 marks]
Write down the 5 × 5 Gauss transform matrix M that can be used to implement the elementary row operations:
Row3 ← Row3 −2·Row2 Row5 ← Row5 +3·Row2
Part (b) [1 mark]
Write down M−1, the inverse of M.
Part (c) [2 marks]
Using the norm of your choosing, determine the condition number of M. Show your work. Be sure to
indicate what norm you used.
University of Toronto Page 4 of 6 cont’d. . .

CSC336H1F Sample Midterm Test 2 February 2018 Question 4. [5 marks]
Given an n × n nonsingular matrix A and a vector b, consider the problem of solving A2x = b for x. Note that A2 = A · A.
Part (a) [3 marks]
Propose an efficient algorithm for solving A2x = b for x. Describe the steps in your algorithm at a
“high-level”. Do not write code.
Part (b) [2 marks]
How many floating-point operations (flops) does your algorithm from part (a) require?
University of Toronto Page 5 of 6 cont’d. . .

CSC336H1F Sample Midterm Test 2 February 2018 Question 5. [4 marks]
The infinite series 􏰆∞ 1 is called the harmonic series. It is well known that this series is divergent. That n=1 n
is, if Sk = 􏰆k 1, then Sk → ∞ as k → ∞. The following C program statements were used to sum the n=1 n
terms in the series:
float n, oldSum, newSum;
n = 1.0;
oldSum = 0.0;
newSum = 1.0;
while ( oldSum != newSum ) {
oldSum = newSum;
n = n + 1.0;
newSum = newSum + 1.0 / n;
}
printf(“At n = %e, the series converged to: %e \n”, n, newSum);
The program does not run forever. It terminates and outputs:
At n = 2.097152e+06, the series converged to: 1.540368e+01
Explain why, even though the harmonic series is divergent, a finite sum was calculated.
Note the program was written in C because C supports single-precision floating-point variables (type float). If the same algorithm were run in Python3, a similar outcome would occur. It would just take longer, since in Python3, type float is double-precision.
University of Toronto Page 6 of 6 End of Test