Interpolation Exercise 1


In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

In [3]:
from scipy.interpolate import interp1d

2D trajectory interpolation

The file trajectory.npz contains 3 Numpy arrays that describe a 2d trajectory of a particle as a function of time:

  • t which has discrete values of time t[i].
  • x which has values of the x position at those times: x[i] = x(t[i]).
  • y which has values of the y position at those times: y[i] = y(t[i]).

Load those arrays into this notebook and save them as variables x, y and t:


In [37]:
with np.load('trajectory.npz') as data:
    t = data['t']
    x = data['x']
    y = data['y']

In [73]:
print(x)


[ 0.          0.46611028  0.77169706  0.85695573  0.72231261  0.42244838
  0.04750816 -0.30261323 -0.54388671 -0.62734994 -0.54777832 -0.34071147
 -0.06970442  0.19209565  0.38094522  0.45732667  0.41334402  0.27159009
  0.07659602 -0.11830239 -0.26499214 -0.33195345 -0.31042482 -0.21434785
 -0.07471184  0.06975958  0.18292364  0.23989085  0.23207351  0.16771697
  0.06822291 -0.0384174  -0.12517681 -0.17257156 -0.17273666 -0.13023529
 -0.05972056  0.018666    0.08480265  0.1235537 ]

In [22]:
assert isinstance(x, np.ndarray) and len(x)==40
assert isinstance(y, np.ndarray) and len(y)==40
assert isinstance(t, np.ndarray) and len(t)==40

Use these arrays to create interpolated functions $x(t)$ and $y(t)$. Then use those functions to create the following arrays:

  • newt which has 200 points between $\{t_{min},t_{max}\}$.
  • newx which has the interpolated values of $x(t)$ at those times.
  • newy which has the interpolated values of $y(t)$ at those times.

In [64]:
newt = np.linspace(min(t),max(t), 200)
f = np.sin(newt)

In [125]:
approxx= interp1d(x,t,kind = 'cubic')
newx = np.linspace(np.min(t), np.max(t), 200)

approxy = interp1d(y,t,kind = 'cubic')
newy = np.linspace(np.min(t), np.max(t), 200)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-125-c28e6d4dc9d8> in <module>()
      1 approxx= interp1d(x,t,kind = 'cubic')
----> 2 newx = approxx(newt)
      3 
      4 approxy = interp1d(y,t,kind = 'cubic')
      5 newy = np.linspace(np.min(t), np.max(t), 200)

/usr/local/lib/python3.4/dist-packages/scipy/interpolate/polyint.py in __call__(self, x)
     77         """
     78         x, x_shape = self._prepare_x(x)
---> 79         y = self._evaluate(x)
     80         return self._finish_y(y, x_shape)
     81 

/usr/local/lib/python3.4/dist-packages/scipy/interpolate/interpolate.py in _evaluate(self, x_new)
    496         #    The behavior is set by the bounds_error variable.
    497         x_new = asarray(x_new)
--> 498         out_of_bounds = self._check_bounds(x_new)
    499         y_new = self._call(self, x_new)
    500         if len(y_new) > 0:

/usr/local/lib/python3.4/dist-packages/scipy/interpolate/interpolate.py in _check_bounds(self, x_new)
    526                 "range.")
    527         if self.bounds_error and above_bounds.any():
--> 528             raise ValueError("A value in x_new is above the interpolation "
    529                 "range.")
    530 

ValueError: A value in x_new is above the interpolation range.

In [63]:
?interp1d

In [122]:
assert newt[0]==t.min()
assert newt[-1]==t.max()
assert len(newt)==200
assert len(newx)==200
assert len(newy)==200

Make a parametric plot of $\{x(t),y(t)\}$ that shows the interpolated values and the original points:

  • For the interpolated points, use a solid line.
  • For the original points, use circles of a different color and no line.
  • Customize you plot to make it effective and beautiful.

In [124]:
plt.plot(newt, f, marker='o', linestyle='', label='original data')
plt.plot(newx, newy, marker='.', label='interpolated');
plt.legend();
plt.xlabel('x')
plt.ylabel('f(x)');



In [ ]:
assert True # leave this to grade the trajectory plot