Problem 1

While computing numerical derivatives we used the following numpy syntax:

data[1:] - data[:-1]

Using $x_i$ to indicate an element of data where $x_0$ is the first element of data, write out the first three terms of the sequence result of this expression. An example of writing in this format would be $x_0, x_1, x_2, \ldots$

$x_1 - x_0, x_2 - x_1, x_3 - x_2$

Problem 2

If I have both x (column 0) and y (column 1) data in an $N \times 2$ numpy array called data, write out in markdown (use backticks to format to look like code) how I would get the sequence of differences between y values.

data[1:, 1] - data[:-1, 1]

Problem 3

Compute the numerical derivative of the following data using the forward, central, and backward derivatives. Plot them all on the same figure with a legend. Plot the forward and backward with the ':' linestyle.


In [14]:
import numpy as np
time = np.array([0.0, 0.2857,  0.5714,  0.8571,  1.1429,  1.4286,  1.7143,  2.0,  2.2857,  2.5714,  2.8571,  3.1429,  3.4286,  3.7143,  4.0,  4.2857,  4.5714,  4.8571,  5.1429,  5.4286,  5.7143,  6.0,  6.2857,  6.5714,  6.8571,  7.1429,  7.4286,  7.7143,  8.0,  8.2857,  8.5714,  8.8571,  9.1429,  9.4286,  9.7143,  10.0,  10.2857,  10.5714,  10.8571,  11.1429,  11.4286,  11.7143,  12.0,  12.2857,  12.5714,  12.8571,  13.1429,  13.4286,  13.7143,  14.0])
temperature = np.array([67.9925, 67.5912,  67.4439,  66.7896,  66.4346,  66.3176,  65.7527,  65.1487,  65.7247,  65.1831,  64.5981,  64.5213,  63.6746,  63.9106,  62.6127,  63.3892,  62.6511,  62.601,  61.9718,  60.5553,  61.5862,  61.3173,  60.5913,  59.7061,  59.6535,  58.9301,  59.346,  59.2083,  60.3429,  58.752,  57.6269,  57.5139,  59.0293,  56.7979,  56.2996,  56.4188,  57.1257,  56.1569,  56.3077,  55.893,  55.4356,  56.7985,  55.6536,  55.8353,  54.4404,  54.2872,  53.9584,  53.3222,  53.2458,  53.7111])

### BEGIN SOLUTION

import matplotlib.pyplot as plt
forward = (temperature[1:] - temperature[:-1]) / (time[1:] - time[:-1])
backward = forward
central = 0.5 * (forward[:-1] + backward[1:])

plt.plot(time[:-1], forward, label='forward', linestyle=':')
plt.plot(time[1:], backward, label='backward', linestyle=':')
plt.plot(time[1:-1], central, label='central')
plt.legend(loc='best')
plt.show()
### END SOLUTION


Problem 4

Someone has given you a file called rdf.dat and claims the third column is a derivative of the second column with respect to the first column. You may read this file using the genfromtxt numpy command. Use the data to answer the following questions:

Problem 4.1

Read the data and plot position (first column) vs the function (second column).


In [18]:
data = np.genfromtxt('rdf.dat')
plt.plot(data[:,0], data[:,1])
plt.show()


Problem 4.2

Compute and plot the numerical derivative and the derivative provided in the file. Use different linestyles for the two lines. Is the derivative in the file correct?


In [17]:
diff = (data[1:, 1] - data[:-1,1]) / (data[1:, 0] - data[:-1,0])
plt.plot(data[1:-1, 0], 0.5 * (diff[1:] + diff[:-1]), label='numerical')
plt.plot(data[:,0], data[:,2], label='file', linestyle='--')
plt.legend(loc='best')
plt.show()