SciPy - integrate.cumulative_simpson() Method



The SciPy integrate.cumulative_simpson() method is used to calculate the coordinates at every pairs by considering the quadratic relationship between an individual point and two adjacent points.

Syntax

Following is the syntax of the SciPy integrate.cumulative_simpson() method −

cumulative_simpson(var1, var2)
or,
cumulative_simpson(var1, var2, sample_point)

Parameters

This method accepts the following parameter −

  • var1: This parameter is used to represent the various built-in function such as linspace(), sin() etc.
  • var2: This parameter represent the simple mathematical calculation.
  • sample_point: This is an optional poarameter required for coordinates/sample point.

Return value

This method returns the result in two different forms which are list of values and matplotlib graph.

Example 1

Following is the basic example that demonstrates the usage of SciPy integrate.cumulative_simpson() method.

from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, num=10)
y = x**2
y_int = integrate.cumulative_simpson(y, x=x, initial=0)
fig, ax = plt.subplots()
ax.plot(x, y_int, 'go', x, x**3/3 - (x[0])**3/3, 'b-')
ax.grid()
plt.show()

Output

The above code produces the following result −

scipy_cumulative_simpson_one

Example 2

The program uses sin() function to calculate the sample(coords) point over the interval of 0 and 2pi where space between two point set at 0.1. After using the cumulative_simpson() method, the result is stored as a list.

import numpy as np
from scipy.integrate import cumulative_simpson

# declare y and dx values
y = np.sin(np.linspace(0, 2 * np.pi, 50))
dx = 0.1 # spacing between element of y

# operation on cumulative integral
cum_integral = cumulative_simpson(y, dx=dx)

print(cum_integral)

Output

The above code produces the following result −

[ 6.41135591e-03  2.55054410e-02  5.70028694e-02  1.00353446e-01
  1.54876692e-01  2.19648187e-01  2.93630869e-01  3.75586562e-01
  4.64189453e-01  5.57968599e-01  6.55396160e-01  7.54864639e-01
  8.54744106e-01  9.53395658e-01  1.04919389e+00  1.14057569e+00
  1.22602649e+00  1.30416123e+00  1.37367525e+00  1.43345212e+00
  1.48248242e+00  1.51999138e+00  1.54533088e+00  1.55811848e+00
  1.55810972e+00  1.54533951e+00  1.51998305e+00  1.48249033e+00
  1.43344475e+00  1.37368194e+00  1.30415533e+00  1.22603152e+00
  1.14057163e+00  1.04919692e+00  9.53393703e-01  8.54744950e-01
  7.54864920e-01  6.55394758e-01  5.57971098e-01  4.64185897e-01
  3.75591116e-01  2.93625392e-01  2.19654498e-01  1.54869651e-01
  1.00361101e-01  5.69947265e-02  2.55139387e-02  6.40264306e-03
 -8.71285215e-06]
scipy_reference.htm
Advertisements