Integrating Discrete point in Python

前端 未结 1 428
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 06:43

I have two numpy array (x,y)-

import numpy as np
import scipy 
from scipy.integrate import simps

y=np.array([1,1,2,1,-2])
x=np.array([0,1,2,3,4])

相关标签:
1条回答
  • 2021-01-01 07:10

    The scipy interpolators (such as InterpolatedUnivariateSpline) have an integral method. For example,

    In [23]: from scipy.interpolate import InterpolatedUnivariateSpline
    
    In [24]: x = np.array([0, 1, 2, 3, 4])
    
    In [25]: y = np.array([1, 1, 2, 1, -2])
    
    In [26]: f = InterpolatedUnivariateSpline(x, y, k=1)  # k=1 gives linear interpolation
    
    In [27]: f.integral(1.5, 2.2)
    Out[27]: 1.2550000000000003
    
    0 讨论(0)
提交回复
热议问题