






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Numerical Computing S’10 MATH/CSCI 4800- Homework-1 Solution Outline
In each case where underflow occurs, is it reasonable to set to zero the quantity that underflows?
(a) UFL is the smallest, and OFL the largest, normalized number (in absolute value) in a floating- point system. mach is the difference between 1 and the next larger floating-point number in the system. For the given system,
mach = (1.000 000 000 001) − (1.000 000 000 000) = 2 −^12 = 2. 44140625 × 10 −^4.
(b) i. (^) √ b^2 + c^2 =
1 = 1 (underflow) ii. (^) √ b^2 + c^2 =
10 −^50 + 10−^50 → 0 (underflow) iii. v × w y × z
(underflow in both numerator and denominator)
In case (i) it is reasonable to set to zero the quantity that underflows. In case (ii), doing so would set to zero a quantity whose exact value is
2 × 10 −^25. This may or may not be reasonable, depending upon the context. In case (iii) doing so does not help; the result is a NaN when the exact answer is 1.
i. Computational error and propagated data error. ii. Truncation error and roundoff error. iii. Conditioning and stability. (b) Suppose you are solving a given problem using a given algorithm. For each of the following, state whether it is affected by the stability of the algorithm, and why? i. Propagated data error. ii. Accuracy of computed result. iii. Conditioning of the problem.
(a) i. Propagated data error in the solution of a mathematical problem is the difference between the exact solution of the problem corresponding to correct data, and the exact solution of the problem corresponding to incorrect or perturbed data. Computational error is the difference between the exact solution to a mathematical problem and the numerical solution to the same problem. ii. Truncation error is the difference between the exact solution of the mathematical model and the exact solution of the numerical model. Roundoff error is the difference between the exact solution of the numerical model and the solution of the numerical model obtained by using finite-precision arithmetic. iii. Conditioning measures the sensitivity of a mathematical problem to perturbations in the input. Stability refers to the sensitivity of the result of a numerical computation to roundoff errors. (b) i. Propagated data error is unaffected by the stability of the algorithm, as it is based on exact solution of the mathematical model to different inputs. ii. Accuracy of the computed result is affected by the stability of the algorithm; an unstable algorithm magnifies roundoff errors to produce a contaminated result. iii. Since conditioning is a property of the mathematical model and stability a property of the algorithm, the former is unaffected by the latter.
a 2 = a 0 +
a 1
for a 0 equal to each of 1, 2 and 3. Comment on your results. If the results are unsatisfactory, what minimum precision would you require to produce accurate results? (c) Let x and y be normalized positive floating point numbers on a binary computer with 13 -bit precision. How small can y − x be if x < 6 < y?
(a) i. In both single and double precision x = 2.75 has the exact binary representation
fl(x) = (1.011) 2 × 2.
ii. x = 2.7 can be written as x = (1.35) 10 × 2. The integer part 1 of (1.35) 10 has the binary representation (1.) 2. For the fractional part 0.35 the binary representation is found as follows.
where the underbrace with 12 indicates 12 occurrences of the block 0110 and we have included 52 digits following the radix within curly brackets. Now we round to nearest. Since the first digit outside the curly brackets is 1, we add 1 to the last digit inside the curly brackets, which results in the last two digits inside the curly brackets changing from 01 to 10. Chopping then leads to fl(x) = (1.{01 0110︸︷︷︸ 12
In this procedure we have discarded (.10 0110)2−^52 × 2 and added 2−^52 × 2. Therefore,
fl(2.7) = 2. 7 − (.10 0110)2−^52 × 2 + 2−^52 × 2 = 2. 7 − (.10)2−^52 × 2 − (.00 0110)2−^52 × 2 + 2−^52 × 2 = 2. 7 − (1/2) × 2 −^51 − (.0110)2−^52 × 2 −^2 × 2 + 2−^52 × 2 = 2. 7 − 2 −^52 − 0. 2 × 2 −^52 + 2 × 2 −^52
In the last step we have used the previously derived result (.0110) = 0. 4. Gathering all the terms above, we see that fl(2.7) = 2.7 + 0. 8 × 2 −^52. Recall that for IEEE double-precision, mach = 2−^52. Therefore for x = 2. 7 , the relative rounding error is given by
fl(x) − x x
mach.
(b) For a 0 = 1,
a 2 = a 0 +
a 1 = 1 +
In 3-digit decimal arithmetic, 5.0001 is rounded off to 5.00. Therefore the result of the calculation is 5 × 104. The same result obtains for a 0 = 2 and 3. At least 5-digit precision is required for accurate computations. (c) The floating-point representation of 6 in the 13-bit system is
6 = (1.100 000 000 000) × 22.
For y − x to have the smallest possible value, y is the float just larger than, and x the float just smaller than 6. Therefore,
y = (1.100 000 000 001) × 22 = 6 +
and x = (1.011 111 111 111) × 22 = 6 −
Then, y − x =
F (x) =
∫ (^) x
0
ln(1 + t) t
dt.
(a) Find Pn(x), the Taylor polynomial approximation to F (x), of degree n and centered at x = 0. Also find the derivative form of the remainder Rn(x). Hint: Start with the Taylor polynomial for ln(1 + t). (b) Find n so that Pn approximates f (x) to within an error of 10−^4 on [− 1 / 2 , 1 /2].
(a) We shall proceed by first constructing the Taylor series for the integrand and then integrating the result. We could apply the general expression for the Taylor series directly to ln(1 + t)/t, but that would involve unpleasant differentiation. It is easier to first find the Taylor series for f (t) = ln(1 + t), and then divide by t. Thus we have
f (t) = ln(1 + t), f ′(t) = (1 + t)−^1 , f ′′(t) = (−1)(1 + t)−^2 ,
f ′′′(t) = (−1)^2 2!(1 + t)−^3 , · · · , f (n)(t) = (−1)(n−1)(n − 1)!(1 + t)−n. Correspondingly,
f (0) = 0, f ′(0) = 1, f ′′(t) = − 1 , f ′′′(0) = (−1)^2 , · · · , f (n)(0) = (−1)(n−1)(n − 1)!.
This, after some simplification, leads to the Taylor polynomial
f (t) = f (0) + tf ′(0) +
t^2 2!
f ′′(0) + · · · +
tn n!
f (n)(0) + Rn(t)
= t − t^2 2
t^3 3
· · · + (−1)(n−1)^ tn n
Rn(t).
Here, Rn(t) = (−1)n^
tn+ n + 1 (1 + c)−(n+1),
where c(t) is unknown but lies between 0 and t. Division by t leads to
f (t) t
ln(1 + t) t
t 2
t^2 3
Rn(t) t
Integrating both sides from 0 to x,
F (x) =
∫ (^) x
0
ln(1 + t) t
dt = x − x^2 22
x^3 32
· · · + (−1)(n−1)^ xn n^2
Rˆn(x),
where Rˆn(x) =
∫ (^) x
0
Rn(t) t
dt = (−1)n
∫ (^) x
0
tn n + 1
(1 + c)−(n+1)dt.
Note that c lies between 0 and t and t between 0 and x, so that c lies between 0 and x. Upon using the Mean-Value Theorem for integrals, the above expression for the remainder may be written as Rˆn(x) = (−1)
n (1 + ˆc)(n+1)
∫ (^) x
0
tn n + 1
dt = (−1)nx(n+1) (1 + ˆc)(n+1)(n + 1)^2
where ˆc lies between 0 and x. The Taylor polynomial is
Pn(x) = x −
x^2 22
x^3 32
xn n^2
(b) With x ∈ [− 1 / 2 , 1 /2] and ˆc ∈ [− 1 / 2 , 1 /2], the upper bound for the remainder is found by setting x = 1/2 in the numerator and ˆc = − 1 /2 in the denominator, leading to
| Rˆn(x)| =
|x|(n+1) |1 + ˆc|(n+1)(n + 1)^2
≤
(1/2)(n+1) | 1 − 1 / 2 |(n+1)(n + 1)^2
(n + 1)^2
For | Rˆn(x)| ≤ 10 −^4 , we must have 1 (n + 1)^2
≤ 10 −^4 or n + 1 ≥ 100.
The smallest n satisfying the above condition is n = 99.
f (x) = tanh(x/4) + x ∗ tan(x/4).
The graphs should be labelled and titled, and should have an appropriate legend. The colors and line styles for the two graphs should not be the same. The M-file should be suitably commented. Be sure to include both the M-file and the plot in your report. The script file and the figure appear below.
% Script File: HW1_P6.m % Plots graphs of f and f’ on a specified interval % % Specify plotting interval x = linspace(-4,4,101); % Define f f = tanh(x/4) + x.tan(x/4); % Define f’ fp = (1/4)(sech(x/4)).^2 + tan(x/4) + (1/4)x.(sec(x/4)).^2; % Plot graphs plot(x,f,’r--’,x,fp,’b’,’LineWidth’,2); xlabel(’x’); legend(’f(x)’,’fprime(x)’); title(’Plots of f(x) and fprime(x)’);
SN (x) = 1 +
k=
Tk where Tk =
xk k!
The truncation error of the approximation, in absolute value, is
EN (x) = |ex^ − SN (x)|.
(a) For fixed x, how should EN (x) behave as N is increased?
(b) For x = − 10 , use Matlab to compute EN as N is increased from 1 to 60. Plot the graph of EN (on a logarithmic scale) against N. Explain why the graph is shaped the way it is. Is the shape in accordance with your answer to part (a) above? If there is a discrepancy, explain the reason for it.
(a) We note that all derivatives of ex^ equal ex. Then, according to Taylor’s theorem,
ex^ − SN (x) =
xn+ (n + 1)! ec
where c lies between 0 and x. For x < 0 , as is the case here, ec^ has its maximum value equal to unity at c = 0. Therefore,
EN (x) = |ex^ − SN (x)| ≤ |x|n+ (n + 1)!
= |Tn+1(x)|,
i.e., the truncation error at any stage is no larger than the first term of the series dropped. For x = − 10 , the terms of the series increase in magnitude till the 10th term, and decrease thereafter, approaching zero in the limit. Therefore, as N → ∞, EN (x) → 0.
(b) The Matlab script appears below. The accompanying figure displays the absolute error EN (−10) against the number of terms N ; note the flat line beyond N = 47. A table listing N, the term TN , the partial sum SN and the error EN is also printed to aid in understanding.
0 20 40 60
10 −
10 −
10 −
100
105
Number of Terms
Absolute Error
Taylor Series Approximations to exp(−10)
As observed above, the terms in the Taylor series increase in magnitude up to the 10th term and then decrease. This is confirmed by the accompanying table; note the magnitude of T (N ) at N = 10. Because of roundoff, every term is computed to a relative error of mach, a multiple of 10 −^16. Therefore T (10), which is a multiple of 10^3 , is computed to within an absolute error of 103 × 10 −^16 = 10−^13. As a result the sum SN , for N > 10 , can never approximate e−^10 to an accuracy greater than 10−^13. This the reason why the error EN , even though it is supposed to converge to zero if computed precisely, does not decay due to roundoff beyond the level of 10−^13 , which it first reaches at N = 47 according to the accompanying figure as well as the last column of the table. Beyond N = 47 the error remains at the level 10 −^13 ; hence the flat line in the figure.
% Script File: ExtraCredit.m % % Computes and plots error in the Taylor series representation of exp(x) % Requires: x, the argument of the exponential, and % N, the highest degree of Taylor polynomial clc