Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 484
user2565770 Avatar asked Jul 11 '13 19:07

user2565770


People also ask

How do you integrate two arrays in Python?

Use numpy. concatenate() to merge the content of two or multiple arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray. Note that this method also takes axis as another argument, when not specified it defaults to 0.

How do you add an array together in Python?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

How do you integrate in Python?

integrate module. It takes as input arguments the function f(x) to be integrated (the “integrand”), and the lower and upper limits a and b. It returns two values (in a tuple): the first one is the computed results and the second one is an estimation of the numerical error of that result.

How do you use quad in Python?

The quad function returns the two values, in which the first number is the value of integral and the second value is the estimate of the absolute error in the value of integral. Note − Since quad requires the function as the first argument, we cannot directly pass exp as the argument.


1 Answers

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.

like image 74
jh314 Avatar answered Oct 22 '22 05:10

jh314