$30
CSC336F Assignment 1
Please write your family and given names and underline your family name on the front page of your paper.
All answers must be typed (preferably in latex), and compiled to a single pdf. All code and output 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.
What to submit:
(1) The single pdf file 00A1.pdf (with embedded code and output).
(2) Any source code you wrote (for computation, etc).
(3) Any plot file embedded in the latex/pdf.
Thus, the code and plot 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. Let f (x) =
xex
− x
x
2
. (Keep f as given.)
(a) [13 points] What is the (relative) condition number of f (x) as a function of x? For what values of x does it get very
large? Explain.
(b) [12 points] Consider that x is a positive number close to 0. Indicate how you would compute f (x) (or an approximation to it) in a numerically stable way. Justify your answer. What is the condition number of the approximation to f (x)
you indicated for x → 0?
(c) [10 points] Consider that x is a negative number close to 0. Indicate how you would compute f (x) (or an approximation to it) in a numerically stable way. Justify your answer.
2. In this question, we study the quality of two approximations to the first derivative ofafunction and of one approximation to the second derivative ofafunction.
By an instance of Taylor’s theorem, f (x + h) = f (x) + h f′(x) +
h
2
2
f′′(ξ ) for some ξ between x and x + h. Thus
f′(x) =
f (x + h) − f (x)
h
−
h
2
f′′(ξ ), and the approximation f′(x) ≈
f (x + h) − f (x)
h
≡ ga
(x; h; f ) inv olves discretization (truncation) error −
h
2
f′′(ξ ).
Note: The approximation f′(x) ≈
f (x + h) − f (x)
h
is said to be of first order, because the truncation error is O(h).
(a) [6 points] Derive an upper bound for the (absolute value of the) truncation error in f′(x) ≈
f (x + h) − f (x)
h
, assuming
|f′′(x)| ≤ M2
. (The bound should be in terms of h and M2
.)
Assume that the (absolute value of the) rounding error in evaluating
f (x + h) − f (x)
h
is at most 5
εmach
h
.
Assuming M2
is independent of h, study the bound for the (absolute value of the) total computation error in ga
, as a
function of h.
(b) [12 points] Use Taylor’s theorem to derive a more accurate approximation to f′(x) using the values f (x + h) and
f (x − h). Let gb
(x; h; f ) be the approximation to f′(x).
Derive an expression for the truncation error in gb and an upper bound for the (absolute value of the) truncation error
assuming |f′′′(x)| ≤ M3
. (The bound should be in terms of h and M3
.)
Note: The approximation to f′(x) derived in (b) should be of second order, with truncation error of O(h
2
).
Assume that the (absolute value of the) rounding error in evaluating gb
is at most 5
εmach
2h
.
Assuming M3
is independent of h, study the bound for the (absolute value of the) total computation error in gb
, as a
function of h.
(c) [12 points] Use Taylor’s theorem to derive a second-order approximation to f′′(x) using (a linear combination of) the
values f (x + h), f (x) and f (x − h). Let gc
(x; h; f ) be the approximation to f′′(x).
Derive an expression for the truncation error in gc and an upper bound for the (absolute value of the) truncation error,
assuming |f′′′′(x)| ≤ M4
. (The bound should be in terms of h and M4
.)
Note: The truncation error should be of O(h
2
).
Assume that the (absolute value of the) rounding error in evaluating gc
is at most 6
εmach
h
2
.
Assuming M4
is independent of h, study the bound for the (absolute value of the) total computation error, in gc
, as a
CSC336 Assignment 1 C. Christara
-2-
function of h.
(d) [15 points] Let f (x) = ln(x), x = 1, and h > 0. Write a MATLAB program, which, for h = 10−16, 10
−15
,
... , 10
−1
, computes ga
, gb and gc
, and the respective errors. (To calculate the errors in ga
, gb and gc
, use the exact values of ln ′(1)
and ln ′′(1).) Also estimate approximately M2
, M3 and M4
for the given function f (x) = log(x) and x = 1, and compute the bounds for the total computational errors as derived in (a), (b) and (c). In one plot, in log-log scale, plot the
bounds of the errors in ga
(solid line), gb
(dashed line) and gc
(dotted line), versus h, for h = 10−16, 10
−15
,
... , 10
−1
.
Indicate the data points in the plot by ‘‘x’’, i.e. use
loglog(h, abs(erra), ’-’, h, bounda, ’x-’, ...
h, abs(errb), ’--’, h, boundb, ’x--’, ...
h, abs(errc), ’:’, h, boundc, ’x:’)
where h=10.ˆ[-16:-1];. Use axis tight; after the plot. Include a legend to distinguish between lines
plotted. Also output the points (stepsize, error) where the minimum error occurs in each case (a), (b) and (c), using a
%9.2e format. Comment on the results.
Note: In (a), (b) and (c), when you study the bounds of the total computation errors as functions of h, indicate where
minima or maxima occur.
To approximately estimate M2
, M3 and M4
, differentiate (by hand) the function, and find (by hand and approximately)
the upper bound of the absolute value of the respective derivatives in the interval of interest. Then hard-code this into
the MATLAB program. If you find that M3 and M4 are dependent on h, you are not doing anything wrong, but consider that the dependence is mild, so that you can ignore it in the studies of the total computational errors (a), (b) and
(c) as functions of h.
Foravalue of machine epsilon (εmach), use the MATLAB predefined value eps.
3. [20 points] We know from calculus that
n→∞
lim (1 +
1
n
)
n
= e = exp(1).
Write a programme or script that approximately computes e using (1 +
1
n
)
n
, for n = 10i
, i = 0, ... , 12. Tabulate i, the
‘‘exact’’ e (taken from the built-in exp(1) in double precision), the approximate e, the relative error, and the quantity
1 +
1
n
. Use a format compatible with
fprintf(’%13.0f %13.10f %13.10f %11.3e %14.11f\n’, ...
an, eapprox, e, (e-eapprox)/e, 1 + 1/an); Run the programme once in single precision (all floats, except the ‘‘exact’’ e), and once in double precision (all doubles). (Thus you need some conventional
programming language such as C, python, Fortran, etc). Comment on the results.
General note: Do no use any symbolic package; MATLAB may allow you to find derivatives, integrals, Taylor series, etc, of
functions symbolically, but these features should not be used in this course, unless explicitly requested.
CSC336 Assignment 1 C. Christara