In [1]:
from scipy import *
import FNC
Recall the grade-school approximation to the number $\pi$.
In [2]:
p = 22/7
print(p)
Note that not all the digits displayed for p
are the same as for $\pi$. As an approximation, its absolute and relative accuracy are
In [3]:
print("absolute accuracy: ",abs(p-pi))
In [4]:
print("relative accuracy: ",abs(p-pi)/pi)
print("accurate digits: ",-log10(abs(p-pi)/pi))
There is no double precision number between $1$ and $1+\varepsilon_\text{mach}$. Thus the following difference is zero despite its appearance.
In [5]:
eps = finfo(float).eps
e = eps/2
(1.0 + e) - 1.0
Out[5]:
However, $1-\varepsilon_\text{mach}/2$ is a double precision number, so it and its negative are represented exactly:
In [6]:
1.0 + (e - 1.0)
Out[6]:
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.
In [7]:
c = array([1,-3,3,-1])
print( FNC.horner(c,1.6) )
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.
In [8]:
r = [-2.0,-1,1,1,3,6]
p = poly(r)
print(p)
Now we use a standard numerical method for finding those roots, pretending that we don't know them already.
In [9]:
r_computed = sort(poly1d(p).roots)
print(r_computed)
Here are the relative errors in each of the computed roots.
In [10]:
print(abs(r - r_computed) / r)
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.
In [11]:
p_computed = poly(r_computed)
print(p_computed)
We find that in a relative sense, these coefficients are very close to those of the original, exact polynomial:
In [12]:
print(abs(p-p_computed)/p)
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.
In [13]:
a = 1.0
b = -(1e6+1e-6)
c = 1.0
In [14]:
x1 = (-b + sqrt(b*b-4*a*c)) / (2*a)
print(x1)
So far, so good. But:
In [15]:
x2 = (-b - sqrt(b*b-4*a*c)) / (2*a)
print(x2)
The first value is correct to all stored digits, but the second has fewer than six accurate digits:
In [16]:
print( -log10(abs(1e-6-x2)/1e-6 ) )
In [17]:
a = 1.0
b = -(1e6+1e-6)
c = 1.0
First, we find the "good" root using the quadratic forumla.
In [18]:
x1 = (-b + sqrt(b*b-4*a*c)) / (2*a)
print(x1)
Then we use the alternative formula for computing the other root.
In [19]:
x2 = c/(a*x1)
print(x2 - 1e-6)