import numpy as np
import matplotlib.pyplot as pt
Let us estimate the sensitivity of evaluating the $\tan$ function:
x = np.linspace(-5, 5, 1000)
pt.ylim([-10, 10])
pt.plot(x, np.tan(x))
[<matplotlib.lines.Line2D at 0x7f448ae83ba8>]
x = np.pi/2 - 0.0001
#x = 0.1
x
1.5706963267948966
np.tan(x)
9999.9999666616441
dx = 0.00005
np.tan(x+dx)
19999.99998335545
np.abs(np.tan(x+dx) - np.tan(x))/np.abs(np.tan(x)) / (np.abs(dx) / np.abs(x))
31413.926693068603
import sympy as sp
xsym = sp.Symbol("x")
f = sp.tan(xsym)
df = f.diff(xsym)
df
tan(x)**2 + 1
Evaluate the derivative estimate. Use .subs(xsym, x)
to substitute in the value of x
.
(xsym*df/f).subs(xsym, x)
15706.9633726542