Differentiation

Derivatives can be found numerically in python and compared to analytic solutions.

Let's start with a derivative from the practise problems $$\frac{d}{dx} \ln(\cos(3x)) = -3 \tan (3x).$$

But what's going on inside np.gradient()?

You know from looking at first principles differentiation that you can define the derivative as a forwards derivative $$\frac{d}{dx} f(x) = \frac{ f(x + h) - f(x) }{h},$$ a backwards derivative $$\frac{d}{dx} f(x) = \frac{ f(x) - f(x-h) }{h},$$ or a symmetric derivative $$\frac{d}{dx} f(x) = \frac{ f(x + h) - f(x-h) }{2 h},$$ where in every case $h$ should be small.

This is exaclty how computers approximate integrals and is the key to the finite difference method of solving differential equations. Let's see how this works.

We can now look at how different each of the numerical approximations is from the true solution. Try changing $h$ and see how this effects the error.

Being able to find derivatives numerically is useful for when derivatives are very hard to find analytically. How would you find $$\frac{d}{dx} \left[ \sin (x+1) \right]^x$$ analytically?