Numerical Derivatives
Goals: Recognize the first-order forward and first order backwords difference method for approximating a derivative
use python to implement a given finite difference scheme calculation for approximating the derivative of a function at a particular point
use a for loop or slicing to approximate a derivative on a grid (discretization)
example
we want to approximate f’(3) for the function f(x) = 2x + cos(x)
using the first order forward difference formula
change of x = 0.1
code:
import numpy as np
def f(x):
return 2*x + np.cos(x)
deriv = (f(3+0.1) - f(3))/0.1
print(f”f’(3) approximately equals {deriv}”)
^^ that was an example of a def function but there is an easier way called annonymous functions
import numpy as np
f = lambda x: 2*x + np.cos(x)
deriv = (f(3+0.1) - f(3)) / 0.1