Homework 7 - Numerical Differentiation
Due Date: November 20, 2023
Problems
- Use the general 3-point formula derived in class to show that when $x_0, x_1, x_n$ are equally spaced nodes we obtain the following: \begin{align*} f'(x_0) &= \frac{1}{2h}\left[-3f(x_0) + 4f(x_1) - f(x_2)\right] + \frac{h^2}{3} f^{(3)}(\xi_0)\\[2ex] f'(x_1) &= \frac{1}{2h}\left[ f(x_2) - f(x_0)\right] - \frac{h^2}{6} f^{(3)}(\xi_1) \\[2ex] f'(x_2) &= \frac{1}{2h}\left[f(x_0)-4f(x_1)+3f(x_2)\right] + \frac{h^2}{3} f^{(3)}(\xi_2) \end{align*}
- Write a Python function called
ThreePointDiff
that takes as input an array $y=(f(x_0), f(x_1), \ldots,$ $f(x_n))$ and a step-size $h$, and returns estimates for the derivatives $y'_j=f'(x_j)$ using 3-point formula estimates. For the end-point derivatives $y'_0$ and $y'_n$ use the appropriate end-point formula, and for the remaining $n-2$ derivatives use the 3-point mid-point formula. Consider the following data:
$x$ $f(x)$ −1.5 1.0 −1.0 0.0 −0.5 −1.0 0.0 2.0 0.5 4.0 Use the 3-point formulas to estimate $f'(x)$ for each $x$. For the end-point derivatives use the appropriate end-point formula, and for the remaining derivatives use the 3-point mid-point formula. Do this by hand for practice, and then use your
ThreePointDiff
function to check your answers.Download the file
xy-data.txt
. The file is a text file containing two columns where the first column are the values \(x=(x_0, x_1, \ldots, x_n)\) and the second column are the values \(y=(y_0, y_1, \ldots, y_n)\). Download the file, save it in an appropriate directory, and then in Python navigate to this directory. Load the contents of the data file and save it to the variablez
as follows:z = np.loadtxt( 'xy-data.txt' )
Use your
ThreePointDiff
function to compute estimates \(\hat{y}'_j\) for the derivatives \(y'_j = f'(x_j)\) for \(j=0,1,\ldots, n\). The value of \(h\) can be determined from the \(x\) data; notice that the nodes are equally spaced.- Plot the \((x_j,y_j)\) data and the data \((x_j, \hat{y}'_j\) on the same axis. Using your knowledge from calculus, comment on how well the derivatives are computed numerically.
- Plot the Hermite polynomial \(H(x)\) interpolating the data set \(\{(x_j, y_j, \hat{y}'_j)\}_{j=0}^n\) on a set of grid points \(u=(u_0,u_1,\ldots,u_N)\) where \(u_0=x_0\) and \(u_N = x_n\). Pick a value of \(N\) so that \(N > n\).