Numerical differentiation: Difference formulas for the first derivative
Computing a numerical first derivative (programming assignment)
In this assignment we look at how we can approximate with a simple difference method some discretisation of the sine function on the interval \([0,2\pi]\). We divide the interval into 25 equidistant points. This assignment is specified in the Python language, but other programming languages like Matlab and R provide similar built-in functions. So, in other programming language you can also do this assignment.
Use the numpy
and matplotlib
package to calculate the derivatives in the points on the interval via forward and central difference approximation and make a scatterplot of the function and its derivatives in one diagram
- Do it via forward differential the first time and use the numpy function diff for this purpose
- Do it the second time via central difference and cross-correlation (and use the 'same' option to get an array of the same length as the number of points) and compare the result of central difference with the result of forward difference.
- Do the central difference the third time using the
numpy
functionroll
which allows you to rotate an array. - Repeat part (2) with \(101\) equidistant points, with \(1001\) equidistant points, and with \(1\,000\,001\) equidistant points. Do the approximations improve as you take more points on the interval?
Unlock full access