pythonscipynumerical-integrationsimpsons-rule

Numerical integration for unevenly spaced intervals, Python


I would like to do numerical integration for a given set of samples.

Let say I have x unevenly spaced regions and y = f(x) is the function I want to integrate.

    x       y=f(x)
   0.1      10.5
   1.2      2.0
   3.7      11.0
   7.0      4.0

Now can I use the Simpon's rule from scipy.integrate this way?

from scipy.integrate import simps

I = simps(y,x)

even though my x values are unevenly spaced?


Solution

  • For numerical integration, the above procedure can be followed once we have the values of both x and the function y=f(x).

    One can also use the Trapezoidal rule from numpy like:

    result = np.trapz(y,x)