Visually comparing the derivative of a function with one variable and its analytic derivative


In [22]:
%%latex
It might be useful to plot the found derivative os a functions. 
This example assumes that 

\[
f(x) = \sin\left(2\pi x + x^2\right)
\]

which has the following derivative:

\[
f'(x) = \cos\left(2\pi x + x^2\right)\left(2\pi + 2x\right) 
\]


It might be useful to plot the found derivative os a functions. This example assumes that \[ f(x) = \sin\left(2\pi x + x^2\right) \] which has the following derivative: \[ f'(x) = \cos\left(2\pi x + x^2\right)\left(2\pi + 2x\right) \]

In [2]:
import autograd.numpy as np

# To do elementwise differentiation:
from autograd import elementwise_grad as egrad 

# To plot:
import matplotlib.pyplot as plt 


def f(x):
    return np.sin(2*np.pi*x + x**2)

def f_grad_analytic(x):
    return np.cos(2*np.pi*x + x**2)*(2*np.pi + 2*x)

# Do the comparison:
x = np.linspace(0,1,1000)

f_grad = egrad(f)

computed = f_grad(x)
analytic = f_grad_analytic(x)

plt.title('Derivative computed from Autograd compared with the analytical derivative')
plt.plot(x,computed,label='autograd')
plt.plot(x,analytic,label='analytic')

plt.xlabel('x')
plt.ylabel('y')
plt.legend()

plt.show()

print("The max absolute difference is: %g"%(np.max(np.abs(computed - analytic))))


The max absolute difference is: 1.77636e-15