In this lecture, we will look at two other common sub-packages of Scipy: scipy.interpolate and scipy.integrate.
The simplest interpolation routine in scipy.interpolate is interp1d:
from scipy.interpolate import interp1d
If we create a fake dataset:
import numpy as np
x = np.array([0., 1., 3., 4.])
y = np.array([0., 4., 3., 2.])
we can interpolate linearly by first creating an interpolating function:
f = interp1d(x, y)
and we can then interpolate to any value of x within the original bounds:
f(0.5)
array(2.0)
f(3.3)
array(2.7)
It is also possible to interpolate to several values at the same time:
f(np.array([0.5, 1.5, 2.5, 3.5]))
array([ 2. , 3.75, 3.25, 2.5 ])
If the interpolating function is called outside the original range, an error is raised:
f(-1.)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-7-3ff19066e54c> in <module>() ----> 1 f(-1.) /Volumes/Raptor/Library/Python/3.4/lib/python/site-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 /Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/scipy/interpolate/interpolate.py in _evaluate(self, x_new) 495 # The behavior is set by the bounds_error variable. 496 x_new = asarray(x_new) --> 497 out_of_bounds = self._check_bounds(x_new) 498 y_new = self._call(self, x_new) 499 if len(y_new) > 0: /Volumes/Raptor/Library/Python/3.4/lib/python/site-packages/scipy/interpolate/interpolate.py in _check_bounds(self, x_new) 522 # !! Could provide more information about which values are out of bounds 523 if self.bounds_error and below_bounds.any(): --> 524 raise ValueError("A value in x_new is below the interpolation " 525 "range.") 526 if self.bounds_error and above_bounds.any(): ValueError: A value in x_new is below the interpolation range.
You can change this behavior by telling interp1d
to not give an error in this case, but to use a set value:
f = interp1d(x, y, bounds_error=False, fill_value=-10.)
f(-1.0)
array(-10.0)
f(np.array([-1., 1., 3., 6.]))
array([-10., 4., 3., -10.])
By default, interp1d
uses linear interpolation, but it is also possible to use e.g. cubic interpolation:
f = interp1d(x, y, kind='cubic')
f(0.5)
array(2.583333333333333)
For more information, see the documentation for interp1d. There are also other interpolation functions available (for example for spline interpolation), which you can read up about at scipy.interpolate.
The available integration functions are listed at scipy.integrate. You will notice there are two kinds of functions - those that integrate actual Python functions, and those that integrate numerical functions defined by Numpy arrays.
First, we can take a look at one of the functions that can integrate actual Python functions. If we define a function:
def simple_function(x):
return 3. * x**2 + 2. * x + 1.
we can integrate it between limits using:
from scipy.integrate import quad
print(quad(simple_function, 1., 2.))
(10.999999999999998, 1.221245327087672e-13)
As described in the documentation for quad, the first value returned is the integral, and the second is the error on the integral. If we had solved the integral analytically, we would expect 11, so the result is correct.
We can also define functions as Numpy arrays:
x = np.linspace(1., 2., 1000)
y = 3. * x**2 + 2. * x + 1.
And in this case we can use for example the simps function to integrate using Simpson's rule:
from scipy.integrate import simps
print(simps(y, x=x))
11.0000000005
This can be very useful in cases where one wants to integral actual data that cannot be represented as a simple function or when the function is only available numerically.
Note that there is an issue on the scipy.integrate page - there should also be a menton of the trapz
function which works similarly to simps
but does trapezium integration:
from scipy.integrate import trapz
print(trapz(y, x=x))
11.000000501
Write a function that takes x
, and the parameters for a Gaussian (amplitude, displacement, width) and returns the value of the Gaussian at x
:
# your solution here
Use quad
to compute the integral and compare to what you would expect.
# your solution here
Now create two arrays x
and y
that contain the Gaussian for fixed values x
, and try and compute the integral using simps
.
# your solution here
Compare this to what you found with quad
and analytically.