This was implemented 12/21/2021 and needed no corrections.
from scipy import *
import numpy as np
import FNC
Recall the grade-school approximation to the number $\pi$.
p = 22/7
print(p)
3.142857142857143
Note that not all the digits displayed for p
are the same as for $\pi$. As an approximation, its absolute and relative accuracy are
print("absolute accuracy: ",abs(p-pi))
absolute accuracy: 0.0012644892673496777
print("relative accuracy: ",abs(p-pi)/pi)
print("accurate digits: ",-np.log10(abs(p-pi)/pi))
relative accuracy: 0.0004024994347707008 accurate digits: 3.3952347251747166
There is no double precision number between $1$ and $1+\varepsilon_\text{mach}$. Thus the following difference is zero despite its appearance.
eps = finfo(float).eps
e = eps/2
(1.0 + e) - 1.0
0.0
However, $1-\varepsilon_\text{mach}/2$ is a double precision number, so it and its negative are represented exactly:
1.0 + (e - 1.0)
1.1102230246251565e-16
This is now the "correct" result. But we have found a rather shocking breakdown of the associative law of addition!
Here we show how to use horner
to evaluate a polynomial. We first define a vector of the coefficients of $p(x)=(x−1)^3=x^3−3x^2+3x−1$, in descending degree order. Note that the textbook's functions are all in a namespace called FNC
, to help distinguish them from other Python commands and modules.
c = np.array([1,-3,3,-1])
print( FNC.horner(c,1.6) )
0.21600000000000041
The above is the value of $p(1.6)$, up to a rounding error.
Our first step is to construct a polynomial with six known roots.
r = [-2.0,-1,1,1,3,6]
p = np.poly(r)
print(p)
[ 1. -8. 6. 44. -43. -36. 36.]
Now we use a standard numerical method for finding those roots, pretending that we don't know them already.
r_computed = np.sort(poly1d(p).roots)
print(r_computed)
[-2. -1. 0.99999999 1.00000001 3. 6. ]
Here are the relative errors in each of the computed roots.
print(abs(r - r_computed) / r)
[-6.66133815e-16 -9.99200722e-16 9.72214964e-09 9.72214953e-09 1.48029737e-16 1.33226763e-15]
It seems that the forward error is acceptably close to machine epsilon for double precision in all cases except the double root at $x=1$. This is not a surprise, though, given the poor conditioning at such roots.
Let's consider the backward error. The data in the rootfinding problem are the polynomial coefficients. We can apply poly to find the coefficients of the polynomial (that is, the data) whose roots were actually computed by the numerical algorithm.
p_computed = np.poly(r_computed)
print(p_computed)
[ 1. -8. 6. 44. -43. -36. 36.]
We find that in a relative sense, these coefficients are very close to those of the original, exact polynomial:
print(abs(p-p_computed)/p)
[ 0.00000000e+00 -1.11022302e-15 4.44089210e-15 4.84460956e-16 -6.60969987e-16 -2.36847579e-15 1.97372982e-15]
In summary, even though there are some computed roots relatively far from their correct values, they are nevertheless the roots of a polynomial that is very close to the original.
a = 1.0
b = -(1e6+1e-6)
c = 1.0
x1 = (-b + np.sqrt(b*b-4*a*c)) / (2*a)
print(x1)
1000000.0
So far, so good. But:
x2 = (-b - np.sqrt(b*b-4*a*c)) / (2*a)
print(x2)
1.00000761449337e-06
The first value is correct to all stored digits, but the second has fewer than six accurate digits:
print( -np.log10(abs(1e-6-x2)/1e-6 ) )
5.118358987126217
a = 1.0
b = -(1e6+1e-6)
c = 1.0
First, we find the "good" root using the quadratic forumla.
x1 = (-b + np.sqrt(b*b-4*a*c)) / (2*a)
print(x1)
1000000.0
Then we use the alternative formula for computing the other root.
x2 = c/(a*x1)
print(x2 - 1e-6)
0.0