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))
x = np.pi/2 - 0.0001
#x = 0.1
x
np.tan(x)
dx = 0.00005
np.tan(x+dx)
#clear
np.abs(np.tan(x+dx) - np.tan(x))/np.abs(np.tan(x)) / (np.abs(dx) / np.abs(x))
import sympy as sp
xsym = sp.Symbol("x")
f = sp.tan(xsym)
df = f.diff(xsym)
df
Evaluate the derivative estimate. Use .subs(xsym, x)
to substitute in the value of x
.
#clear
(xsym*df/f).subs(xsym, x)