Python for Everybody
Chapter 1
Exercise 1.1
Use any function for which you can evaluate the derivatives analytically, investigate the accuracy of the formulas in Table 1.2 for various values of h.
Summary 1.1
To approximate the derivative numerically, we will utilize five equations based off the Maclaurin Series. The Maclaurin Series of function, f(x), is given by the equation,
Solving the equation above for f' yields,
The above equation assumes the function is accurately described by a linear function over the interval (0,h). This gives the first of the "2-point" methods; the forware 2-point method.
Where x is the value where the derivative is being evaluated and h is the step-sized used. Using the same methods as above but using the previous point produces the equation,
This assumes the function is accurately described by a linear function over the interval (-h,0). Notice each of these equations are accurate up to one order of h. The following are Python functions utilizing each respective 2-point method,
We can improve tha accuracy by including more terms. Using the immediate values forwards and backwards allos for the terms with even-powered values of hto cancel out. Eq. 1 is the quadratic interpolation of the function over the two previous intervals,
Python interpretation of Eq 5. ,
def symmetric3(x, h): #Performs the symmetric 3-point method on the function defined by myFunc #Input: x -- independent variable # h -- step-size #Output: dependent variable return((myFunc(x+h) - myFunc(x-h)) / (2*h))
The final two equations, the "4-point" and "5-point" formulas, are similarly found and include more interactions forwards and backwards to create higher-order approximations of the function over the given interval. They, along with their higher-order derivatives, are given in the Table 2 . The 5-point formula is the five-point stencil in one-dimension.