How do I integrate two 1-D data arrays in Python?

57,385

Solution 1

Scipy has some nice tools to perform numerical integration.

For example, you can use scipy.integrate.simps to perform simpson's Rule, and you can pass it the following:

scipy.integrate.simps(y, x=None, dx=1, axis=-1, even='avg')

Parameters :
y : array_like Array to be integrated.

x : array_like, optional If given, the points at which y is sampled.

dx : int, optional Spacing of integration points along axis of y. Only used when x is None. Default is 1.

axis : int, optional Axis along which to integrate. Default is the last axis.

even : {‘avg’, ‘first’, ‘str’}, optional

‘avg’ : Average two results:1) use the first N-2 intervals with a trapezoidal rule on the last interval and 2) use the last N-2 intervals with a trapezoidal rule on the first interval.

‘first’ : Use Simpson’s rule for the first N-2 intervals with a trapezoidal rule on the last interval.

‘last’ : Use Simpson’s rule for the last N-2 intervals with a trapezoidal rule on the first interval.

So you can use your two arrays to do numerical integration.

Solution 2

Scipy has an integration feature that can help you.

If you want to use the cumulative sum of trapezoids for integration, which would probably be best for a series of points.

You can do this:

>>> from scipy import integrate
>>> x = np.linspace(-2, 2, num=20)
>>> y = x
>>> y_int = integrate.cumtrapz(y, x, initial=0)
>>> plt.plot(x, y_int, 'ro', x, y[0] + 0.5 * x**2, 'b-')
>>> plt.show()

This will also plot the data and show it to you graphically. This is the integration call integrate.cumtrapz(y, x, initial=0) where x, and y are your two arrays.

Share:
57,385

Related videos on Youtube

user2565770
Author by

user2565770

Updated on April 01, 2022

Comments

  • user2565770
    user2565770 about 2 years

    I have two tabulated data arrays, x and y, and I don't know the function that generated the data. I want to be able to evaluate the integral of the line produced by the data at any point along the x-axis.

    Rather than interpolating a piecewise function to the data and then attempting to integrate that, which I am having trouble with, is there something I can use that will simply provide the integral by evaluating the arrays?

    When searching for solutions, I have seen references to iPython and Pandas, but I haven't been able to find the parts of those packages that will aid in this task.

    If there isn't a way to simply integrate the arrays, could you provide some advice on the best way to handle this task?

  • WDpad159
    WDpad159 over 3 years
    What if you were dealing with multiple arrays/lists? How can you perform integration?
  • Mi Be
    Mi Be over 3 years
    can not edit your post. the actual correct link to cumulative sum of trapezoids would be here: docs.scipy.org/doc/scipy/reference/generated/…
  • Ahmad Asmndr
    Ahmad Asmndr almost 3 years
    You make my day, thank you a lot... it is working like magic and save a lot of time