Using scipy to perform discrete sample integration

I am trying to transition from labview to python.

In laboratory mode, there is the Integral x (t) VI function, which takes a set of samples as input, performs discrete integration of samples, and returns a list of values ​​(the area under the curve) in accordance with the Simpsons rule.

I tried to find an equivalent function in scipy, for example. scipy.integrate.simps, but these functions return a summed integral over many samples in the form of a float.

How to get a list of integrated values, not the sum of integrated values?

Am I just looking at the problem wrong?

+4
source share
2 answers

I think you may use scipy.integrate.simps a little incorrectly. The area returned by scipy.integrate.simps is the total area under y (the first parameter has passed). The second parameter is optional and represents sample values ​​for the x axis (actual x values ​​for each of the y values). i.e:

 >>> import numpy as np >>> import scipy >>> a=np.array([1,1,1,1,1]) >>> scipy.integrate.simps(a) 4.0 >>> scipy.integrate.simps(a,np.array([0,10,20,30,40])) 40.0 

I think you want to return the areas under the same curve between different limits? To do this, you pass the part of the curve that you want, for example:

 >>> a=np.array([0,1,1,1,1,10,10,10,10,0]) >>> scipy.integrate.simps(a) 44.916666666666671 >>> scipy.integrate.simps(a[:5]) 3.6666666666666665 >>> scipy.integrate.simps(a[5:]) 36.666666666666664 
+5
source

There is only one method in SciPy that performs cumulative integration, which scipy.integrate.cumtrapz() does what you want, unless you specifically need to use the Simpson rule or another method. To do this, you can offer, as always, write a cycle yourself.

0
source

Source: https://habr.com/ru/post/1415125/


All Articles