Conditioning of evaluating tan()

In [2]:
import numpy as np
import matplotlib.pyplot as pt

Let us estimate the sensitivity of evaluating the $\tan$ function:

In [3]:
x = np.linspace(-5, 5, 1000)
pt.ylim([-10, 10])
pt.plot(x, np.tan(x))
Out[3]:
[<matplotlib.lines.Line2D at 0x7f448ae83ba8>]
In [18]:
x = np.pi/2 - 0.0001
#x = 0.1
x
Out[18]:
1.5706963267948966
In [19]:
np.tan(x)
Out[19]:
9999.9999666616441
In [20]:
dx = 0.00005
np.tan(x+dx)
Out[20]:
19999.99998335545

Condition number estimates

From evaluation data

In [21]:
np.abs(np.tan(x+dx) - np.tan(x))/np.abs(np.tan(x)) / (np.abs(dx) / np.abs(x))
Out[21]:
31413.926693068603

Using the derivative estimate

In [22]:
import sympy as sp

xsym = sp.Symbol("x")

f = sp.tan(xsym)
df = f.diff(xsym)
df
Out[22]:
tan(x)**2 + 1

Evaluate the derivative estimate. Use .subs(xsym, x) to substitute in the value of x.

In [23]:
(xsym*df/f).subs(xsym, x)
Out[23]:
15706.9633726542
In [ ]: