使用scipy.integrate.trapz进行数值积分会返回结果,但scipy.integrate.simps不会

时间:2018-11-16 07:45:39

标签: python math scipy integrate

我正在尝试通过数字方式整合以下数据:

t,y
9.960000000000001e-05,8.334379999999999e-05
9.98e-05,0.000126125
0.0001,0.00018125
0.0001002,0.0002385
0.00010040000000000001,0.000289625
0.0001006,0.000347313
0.0001008,0.000391719
0.000101,0.00043284399999999994
0.00010120000000000001,0.000472375
0.00010140000000000001,0.000502469
0.00010160000000000002,0.000528594
0.00010180000000000001,0.000546219
0.000102,0.000569188
0.0001022,0.000584719
0.0001024,0.000593969
0.00010260000000000001,0.000607375
0.0001028,0.000618906
0.000103,0.000629344
0.0001032,0.000629344
0.0001034,0.000636313
0.00010360000000000001,0.0006374999999999999
0.0001038,0.000630375
0.00010400000000000001,0.00059875
0.0001042,0.00046825
0.0001044,0.000287344
0.00010460000000000002,0.000208594
0.0001048,0.000196094
0.000105,0.000172625
0.00010520000000000001,0.000134781
0.0001054,0.000115906
0.0001056,0.00010306300000000001
0.00010580000000000001,9.31563e-05

如下所示,我从csv文件读取了此数据集。 这是我的代码:

import pandas as pd
from scipy.integrate import trapz, simps

root="my/root/dir"

df = pd.read_csv(root+r"simps.csv")
print(trapz(df["t"], df["y"]))
print(simps(df["t"], df["y"]))

trapz返回一个值(-9.5865055e-10),而simps返回nan和以下错误:

anaconda\path\scipy\integrate\quadrature.py:324: RuntimeWarning: divide by zero encountered in true_divide
  h0divh1 = h0 / h1
anaconda\path\scipy\integrate\quadrature.py:326: RuntimeWarning: divide by zero encountered in true_divide
  y[slice1]*hsum*hsum/hprod +
anaconda\path\scipy\integrate\quadrature.py:327: RuntimeWarning: invalid value encountered in add
  y[slice2]*(2-h0divh1))
anaconda\path\scipy\integrate\quadrature.py:326: RuntimeWarning: invalid value encountered in add
  y[slice1]*hsum*hsum/hprod +

为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

一个非常愚蠢的错误:我交换了x和y。 Simps要求y作为第一个参数