CSC336S Assignment 1 Due Tuesday, February 1, 2022
Please write your family and given names and underline your family name on the front page of your paper.
Answers must be typed (preferably in latex), and compiled to a single pdf. Code, output and plots of Q2 and Q3 should be
embedded in latex/pdf. Code and output should be embedded with fixed-width fonts, e.g. Courier. Font size of all
fonts must be 12. Linespacing set to 1.1 or close. Do not use dark backgrounds at any point of the pdf file.
Submit the single pdf file 00A1.pdf (with embedded code, output and plots), as well as your code. Thus, the code will be
available within latex/pdf, as well as separately.
See course website for example of latex, embedding plots, code, using fixed width fonts, etc.
Some points will be given for the quality of presentation.
1. [10 points] By an instance of Taylor’s theorem, f (a + h) = f (a) + h f ′(a) + h
2
2
f ′′(a) + h
3
3!
f ′′′(ξ+) and a similar one
for f (a − h), after some manipulation, we can derive the relation f ′(a) = f (a + h) − f (a − h)
2h
+ O(h2).
Show the details of this manipulation. Show what is included in the O(h2) term and indicate any assumptions on f
and/or its derivatives.
2.(a) [12 points] Consider the function g(a, h) = √ a + h − √ a − h
2h
as a function of h, with 0 < a, and |h| < a. Find the condi-
tion number κ (h) of g and study for what ranges of h it becomes large. Include the study of κ (h) as h → 0 and a is
fixed, and as a → ∞ and h is small.
(b) [12 points] When h → 0, the computation g(x) = √ x + h − √ x − h
2h
is considered a reasonable approximation to the de-
rivative of √ x. Howev er, the numerical stability of the calculation of g(x) (as it is given) is problematic for a certain
range of h. Explain what problems the computation of the expression g(x) = √ x + h − √ x − h
2h
may give rise to and for
what ranges of h. (The answer may be in terms of x and εmach.)
(c) [11 points] Suggest an alternative (but mathematically equivalent) way of calculating g(x), that does not give rise to the
instability problems described in (b), and explain.
(d) [20 points] Write matlab/other code that, for a given value of a, and for h = 10−1, 10−2, . . . , 10−17 (i.e. h = 10−i ,
i = 1, 2, . . . , 17), calculates the ‘‘exact’’ value (d0) of the derivative of √ x at x = a, the approximate value (d1) by
√ a + h − √ a − h
2h
and the approximate value (d2) by the alternative formula suggested in (c), and outputs i, d0,
d1, d2, d0-d1, d0-d2 on one line. Plot the errors |d0-d1| and |d0-d2| in log-log scale versus the respective val-
ues of h in one plot. Run your code for a = 1 and a = 10. (So you will have two plots and two sets of output.) Com-
ment on the results.
Notes: Use
fprintf(’%3d %17.14f %17.14f %17.14f %8.1e %8.1e\n’, ...
i, d0, d1, d2, d0-d1, d0-d2); to format the output.
Save the values of h and of the errors |d0-d1| and |d0-d2| in respective vectors in order to do the plot.
Use loglog for the plot and axis([1e-18 2e-1 1e-17 1]); after the plot and before saving/printing the
plot. Do not worry if some errors are zero. Use solid line for |d0-d1| and dashed for |d0-d2|.
Hand-in a copy of your code, output and plot.
You must use double precision. Matlab uses double precision by default. You cannot use any package that adjusts the
precision, or any symbolic package (such as automatic differentiation).
3. [25 points] The series Σ∞i=1 1i2 are known to converge to s =
pi 2
6
. A straight calculation of the sum in increasing order
of i’s up to saturation, shows that the calculated sum approximates the infinite sum within about 8 decimal digits. Sug-
gest a way to calculate the sum with higher precision and explain.
Write matlab/other code that computes the sum (in increasing order of i’s) up to saturation, and output the last i used,
the ‘‘exact’’ sum s, the calculated sum (s1), and the relative error s − s1
s
. Write also the code to implement the sum-
mation with higher precision, and output the highest i used, the ‘‘exact’’ sum s, the calculated sum (s2), and the rela-
tive error
s − s2
s
. Because this more accurate way includes more terms, it becomes inefficient, so aim for an accuracy
of about 10 decimal digits, and base your comments on these results. Comment on the results. Could you approxi-
mately predict the number of terms needed to reach saturation in the straight calculation of the sum? Explain. How
does the relative error in the more accurate way behaves as the number of terms included increases? Could you
CSC336 Assignment 1 C. Christara
- 2 -
approximately predict the number of terms needed to reach relative error εmach with the more accurate way? (Do not
run this case, just use mathematical/computational arguments to predict.)
Use format fprintf(’%12d %18.16f %18.16f %8.1e\n’, i-1, s0, s1, (s0-s1)/s0) and simi-
larly for s2.
Hand-in a copy of your code and output.
You must use double precision. Matlab uses double precision by default. You cannot use any package that adjusts the
precision, or any symbolic package.
4. [10 points] Prove that the product of two tridiagonal n × n matrices is a pentadiagonal matrix, more precisely, a matrix
with semi-bandwidth at most 2. (Your proof must be general, for any n and any entries of the tridiagonal matrices.)
CSC336 Assignment 1 C. Christara