Table of contents
Tutorial Video
Perquisites
We will need the numpy, scipy and matplotlib.pylot libraries:
Data Points
Supposing we have recorded the speed of a rocket at the following 6 time points:
We can create NumPy arrays from the data:
To plot the figure we can use:

Manual Interpolation of a Single Point
Supposing we want to know the velocity at t=16 s, how can we estimate it from the data provided?
We can look at the time points and see how much they differ from the time point of t=16

Nearest Point Interpolation
We can first look at a single data point and interpolate using its value. If the nearest datapoint does not differ much from the value being estimated it can be a good enough estimate in some cases. In this case the nearest datapoint is t=15, so we just take the value from it.

This is the 2nd index of the vector t and v, so we just use our single time point t[2] and its velocity v[2] to guess the value when t=16.

362.78
Using this data, the plot can be made using the code:

Linear Interpolation
For Linear Interpolation we need to use 2 datapoints, these should be the nearest 2 datapoints to time point we are interested in namely in this case t=15 and t=20, the 2nd and 3rd index of t and v
Here instead of referencing only the nearest data point, we reference the nearest 2 data points, we then fit a line between them and calculate the value of v on the line at t=16 and use that as our estimate for v[16].

The equation for a straight line is:
To solve for 2 unknowns:
We require 2 equations:
Inputting our known values:
Creating these 3 as matrices:
We can re-write the above:
Solving for alin:
Where:
Rearranging:
Once we have these coefficients, it is simply a case of inserting them into the equation at t=16:
We can do these steps in Python by using the 2nd and 3rd index of t to create our tlin matrix and the 2nd and 3rd index of v to create our vlin vector.

[[ 1. 15.]
[ 1. 20.]]
[362.78 517.35]
We can solve to get the coefficients, alin by using the linear algebra library. We can first of all calculate the inverse of the square matrix tlin and then use this to perform array multiplication on the vector vlin, alternatively we can use the function solve from the linear algebra library, which employs the same method, calculating the inverse of tlin in the background and finally we can also use the least square method to calculate the coefficients of alin.
For a simple set of equations, all three methods yield the same result.
[-100.93 30.914]
[-100.93 30.914]
[-100.93 30.914]
Once alin is calculated we can use it to perform array multiplication on a t16_lin vector.
393.694
The linear line with coefficients a0 and a1 will in only work well in most cases, within the specified lower and upper bound, in this case in the regime between t=15 and t=20. Attempting to use the equation to interpolate values at t=0 and t=30 gives:
[1, 0]
-100.93000000000028
[1, 30]
826.4900000000001
The v0_lin value is well out the 0,0 value of the origin which we were given, while the v30_lin is also lower than the last value we were given in our data set. These can be plotted with respect to one another:

Quadratic Interpolation
For quadratic interpolation, we interpolate using the nearest 3 data points, in the case of t=16, this is t=15, t=20 and t=10. we then fit a quadratic curve between them and calculate the value of v on the line at t=16 and use that as our estimate for v[16]:
The equation for a quadratic curve is:
To solve for 3 unknowns:
We require 3 equations:
Inputting our known values:
Creating these 3 as matrices:
We can re-write the above:
Solving for aquad using the inverse:
Once we have these coefficients it is simply a case of inserting them into the equation at t=16:
We can do these steps in Python by using the 1st, 2nd and 3rd index of t to create our tquad matrix and the 1st, 2nd and 3rd index of v to create our vquad vector. We can solve to get the coefficients, aquad by using solve from the linear algebra library and perform array multiplication with the t16_quad vector to get our interpolated value.

392.188
Cubic Interpolation
For cubic interpolation, we interpolate using the nearest 4 data points, in the case of t=16, this is t=15, t=20 and t=10. we then fit a quadratic curve between them and calculate the value of v on the line at t=16 and use that as our estimate for v[16]:
The equation for a cubic function is:
To solve for 4 unknowns:
We require 4 equations:
Inputting our known values:
Creating these 3 as matrices:
We can re-write the above:
Solving for a cubic function using the inverse:
Once we have these, it is simply a case of inserting them into the equation at t=16:
We can do these steps in Python by using the 1st, 2nd, 3rd and 4th index of t to create our tcubic matrix and the 1st, 2nd, 3rd and 4th index of v to create our vcubicvector. We can solve to get the coefficients, acubic by using solve from the linear algebra library and perform array multiplication with the t16_cubic vector to get our interpolated value.

392.057
Comparison
As we can see with the more data points used the value begins to converge:
Interpolation of Multiple Data Points using an Interpolation Function
To use the interpolation function we need to import the scipy.interpolate library (line 5).
Nearest Point Interpolation
To interpolate, we must first create an interpolation function from the existing x and y (in this case t and v) data. We can do this by loading interp1d from the interpolate library of SciPy. For the input arguments, we specify our origina x and y co-ordinates and we specify the method as a keyword input argument, in this case we will select the method nearest:
Next we must specify new x values. In this case we want to create a variable tnew which starts from 0 and ends at 30 consisting of integer values, we can use the function arange to do this:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
To get the y values at these x values, we assign them as an input argument into our created interpolation function. In our case our new x values are tnew, we can calculate vnearest by using the nearest interpolation function fvnearest with tnew as an input argument:
[ 0. 0. 0. 0. 0. 0. 227.04 227.04 227.04 227.04 227.04 227.04 227.04 362.78 362.78 362.78 362.78 362.78 517.35 517.35 517.35 517.35 602.97 602.97 602.97 602.97 602.97 901.67 901.67 901.67]
Of course before we calculated manually the value at v16nearest earlier we can compare this to the value of vnearest[16]
362.78
We can plot out these values with respect to the original data

Linear Interpolation
The form for Linear interpolation is the same for the Nearest interpolation except for the fact that we use linear as the kind. We can print the value out at t=16:
393.694
And once again we see it matches what we manually calculated.

Quadratic Interpolation
Once again the procedure for quadratic interpolation is the same for the Nearest interpolation except for the fact that we use linear as the kind. We can print the value out at t=16:
392.093
This is very similar to the value manually calculated (392.188) and any differences is likely to do with additional smoothing carried out when using the interpolation function.

Cubic Interpolation
Following out the form as used for the other methods of interpolation:
392.071
This is once again very similar to the value manually calculated (392.071) and any differences is likely due to smoothing carried out by the interpolation function.
