Differentiation Exercises


Instructions: Create a new notebook called DifferentiationExercises in your Differentiation directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.

Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title Differentiation Exercises. Then include the inline functions libraries by adding a code cell that invokes the %pylab inline magic and imports the needed packages.


Question 1

Let’s explore the accuracy of numerical differentiation.

(a) Write two functions called twopt_forward_diff(x,y) and twopt_centered_diff(x,y) that take as arguments arrays of values $x$ and $y(x)$ and return the derivatives of $y$ with respect to $x$. Make sure that the arrays returned by the function have the same shape as the input arrays.

def twopt_forward_diff(x,y):
        """include a docstring to describe what your function does"""

        # your code goes here ...

        return dydx

    def twopt_centered_diff(x,y):
        """include a docstring to describe what your function does"""

        # your code goes here ...

        return dydx

In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Use your functions to take the derivative of the function $y=8x^4 + 3x$ over the range 0 $< x <$ 10, with steps of 0.1. How do you know they work correctly? (Hint: compare to the analytical result).


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(c) What is the accuracy of the results if you use a spacing of 0.01? (Plot the relative error)


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(d) For what minimum spacing is the 2pt-centered difference derivative accurate to 5 significant figures over its full range (excluding the last point)?


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 2

Numerical differentiation plays an important role in fast electronic signal processing in many experimental contexts, such as the data acquisition systems of high energy particle and nuclear physics. The data collected is often a series of fast time samples of energy deposited in a detector by a fast-moving charged particle. The sheer volume of data can be enormous, so some signal processing within the electronics chain is necessary to reduce the size of the collected dataset.

Due to the stochastic nature of the particle interactions and the inherent noise in the readout electronics, these signals can be very noisy. Unfortunately, numerical differentiation can amplify these fluctuations. By choosing your differentiation algorithm carefully, you can minimize the impact of these fluctuations on the collected signals.

If we have a dataset for which we do not control the spacing of the data points, we have to use higher order differencing methods to reach a smoother and more accurate derivative. The four-point center differencing method is defined by

$$ dydx[i] \simeq \frac{y[i-2] - 8y[i-1] + 8y[i+1] - y[i+2]}{12\Delta x} $$

where $\Delta x$ is the spacing between points on the $x$ axis (assumed to be equal). This comes from keeping higher order terms from the Taylor expansion used to define the derivative. This method can be used to obtain dydx[2:-2]. In order to calculate values for dydx[0], dydx[1], dydx[-1] and dydx[-2] you must use the lowest order finite difference method (like the ones you defined in your twopt_forward_diff(x,y) and twopt_centered_diff(x,y) functions).

(a) Create another function, fourpt_centered_diff(x,y), that computes the derivative using the higher order method above.

Test that it works for $y=8x^4+3x$ and plot the relative error from the analytical result.


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Let's generate an example of a noisy sinusoidally varying dataset with the following:


In [12]:
x = np.linspace(0,2*pi,100)
y = np.sin(x) + 0.1*random.random(size=x.shape)

Now plot the noisy sine function along with the derivatives computed analytically, using the lowest order forward finite difference method and the four-point centered difference. Your result should look something like the plot shown here:


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(c) Write your three differentiating functions to a file called Differentiators.py. Be sure to include meaningful docstrings that describe what the functions do. The docstrings should include enough information for someone to understand the differences between the algorithms. Test that the code works by importing the functions and calling them on $y=8x^4+3x$. Plot the results from all three methods.


In [12]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.