$25
Consider a floating-point (FP) system with 4 decimal digits and rounding to the nearest. Give an example for each of the following:
a. (a + b)/2 ∈/ [a,b]
b. a + (b + c) =~ (a + b) + c
c. a ∗ (b ∗ c) =~ (a ∗ b) ∗ c
where a, b, c are FP numbers in this system.
[4 points] Let a and b be IEEE-754 FP numbers and a ≤ b. Can (a + b)/2 ∈/ [a, b] occur? Explain.
Hint: for x ≤ y, fl(x) ≤ fl(y).
Problem 3 [8 points] Consider the series expansion of ex:
x2
ex = 1 + x + 2! + Write MATLAB functions as follows x3
3! + · · · .
function s = expsum1(x) computes an approximation to ex by adding terms until the sum does not change.
function s = expsum2(x) returns expsum1(x) for x ≥ 0 and 1/expsum1(−x) for x < 0.
function s = expsum3(x) accumulates the positive and negative terms separately and then adds the corresponding sums.
Write a main program main_expsum.m that calls these functions for
x = −20, −15, −5, −1, 1, 5, 15, 20
and outputs for each function results in the form
x accurate value approx. value abs. error rel. error
-20.0 2.061153622439e-09 6.138259738609e-09 4.08e-09 1.98e+00
-15.0 3.059023205018e-07 3.059300523747e-07 2.77e-11 9.07e-05
.
.
20.0
(If y is an accurate value and y~ is an approximation, the absolute (abs.) error is |y − ~y| and the relative (rel.) error is |y − ~y|/|y|.)
a. [6 points] Explain the accuracy of expsum1, expsum2, expsum3.
b. [2 points] Can expsum3 produce accurate results for x < 0? Explain. Submit
• Your MATLAB files to Avenue.
• PDF should contain MATLAB code, output, and discussion.
Problem 4 [6 points] We are not going to do C/C++ in this course, but this is a good exercise to learn about benchmarking and performance.
Read about
• the LINPACK benchmark, https://en.wikipedia.org/wiki/LINPACK_benchmarks.
• long double and __float128, https://en.wikipedia.org/wiki/Long_double
I have modified a bit the original benchmark, see the file linpack.cc. In Linux, save the given files linpack.cc and makefile and type make. This should produce the output file
benchmark.out.
• Modify linpack.cc, see lines 48 and 52, so it can produce results for these two datatypes.
• Then uncomment in the makefile
• [4 points] the output benchmark.out
• [2 points] discussion about the performance of these data types. Problem 5 [10 points] Given a scalar function f, from
f(x + h) = f(x) + f'(x)h + O(h2),
we can approximate the first derivative
f'(x) ≈ g1(x, h) := f(x + h) — f(x)
where the error in this approximation is O(h).
From
f(x ± h) = f(x) ± f'(x)h + f''(x)
2 h2 + O(h3),
we can approximate
f'(x) ≈ g2(x, h) := f(x + h) — f(x — h)
2h
where the error is O(h2).
a. [4 points] Let f(x) = xex and x0 = 1. Write a MATLAB program that computes the errors |f'(x0) - g1(x0, h)| and |f'(x0) - g2(x0, h)| for each h = 10-k, k = 1, 2, . . . , 16.
Using loglog, plot on the same plot these errors versus h.
b. [2 points] For what h is the minimum of the errors achieved in each of these two approx-imations?
c. [4 points] Explain the behaviour of these errors. In particular, discuss the role of the cancellation and truncation errors.