In [1]:
from scipy import *
import FNC

Example 1.1.2

Recall the grade-school approximation to the number $\pi$.


In [2]:
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


In [3]:
print("absolute accuracy: ",abs(p-pi))


absolute accuracy:  0.0012644892673496777

In [4]:
print("relative accuracy: ",abs(p-pi)/pi)
print("accurate digits: ",-log10(abs(p-pi)/pi))


relative accuracy:  0.0004024994347707008
accurate digits:  3.3952347251747166

Example 1.1.3

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]:
0.0

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]:
1.1102230246251565e-16

This is now the "correct" result. But we have found a rather shocking breakdown of the associative law of addition!

Example 1.3.2

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) )


0.21600000000000041

The above is the value of $p(1.6)$, up to a rounding error.

Example 1.3.3

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)


[  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.


In [9]:
r_computed = 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.


In [10]:
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.


In [11]:
p_computed = 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:


In [12]:
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.

Example 1.3.4


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)


1000000.0

So far, so good. But:


In [15]:
x2 = (-b - 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:


In [16]:
print( -log10(abs(1e-6-x2)/1e-6 ) )


5.118358987126217

Example 1.3.5


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)


1000000.0

Then we use the alternative formula for computing the other root.


In [19]:
x2 = c/(a*x1)
print(x2 - 1e-6)


0.0