Due 23:59 2021-03-28
double
?float
.)a <- 0.7
, b <- 0.2
, and c <- 0.1
. Make sure setting options(digits=20)
.
(a + b) + c
equals 1.a + (b + c)
equals 1.(a + c) + b.
equals 1.Consider the evaluation of $e^x$ using the Taylor series
\(e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \dotsb\)
in R. Write function expTaylor()
that takes a double-precision floating point number x
and executes the following.
stop <- 100
ex <- 1
xi <- 1
ifac <- 1
for (i in 1:stop) {
xi <- xi * x
ifac <- ifac * i
ex <- ex + xi / ifac
}
ex
Now let x <- 20.0
. A correct implementation of expTaylor()
should yield 485165195.40979015827
(with options(digits=20)
, which is close enough to 485165195.40979027748
that the built-in fuction exp()
yields.
Compare the values of expTaylor(-20.0)
and 1 / expTaylor(20.0)
. Calculate the relative error of the two values, assuming that exp(-20.0)
is the true value. Explain what you found.
We can think of the algorithm given in the R code above as an iterative algorithm in i
. At each value of i
, there is a difference in the value of ex and the true value ex
. (The exact value of this difference is the truncation error.) Modify the code (or use different code) to determine the relative error in ex
for each value of i
. For x = 20.0
, make a plot of the relative error and the number of iterations (that is, of i
) for 1 to 100 iterations. Now, repeat this for x = −20.0
. Explain what you found.
Determine the order of the error for approximating ex
with the Taylor series for x = 20
in terms of the number of iterations (that is, the number of terms in the Taylor series).
Rcpp::cppFunction('int float32bin(double x) {
float flx = (float) x;
unsigned int binx = *((unsigned int*)&flx);
return binx;
}')
to inspect single-precision floating point numbers. Figure out and explain what the included C++ code does.
Show the following facts about triangular matrices. A unit triangular matrix is a triangular matrix with all diagonal entries being 1.
The product of two upper (lower) triangular matrices is upper (lower) triangular.
The inverse of an upper (lower) triangular matrix is upper (lower) triangular.
The product of two unit upper (lower) triangular matrices is unit upper (lower) triangular.
The inverse of a unit upper (lower) triangular matrix is unit upper (lower) triangular.
An orthogonal upper (lower) triangular matrix is diagonal.