在Python中集成数组

问题描述

我是第一次使用Simpson方法在python中进行集成。 我已经编写了代码,它可以成功运行,但是我不知道它能否给出正确的输出

这是我的输入文件:我想在x的范围内执行y的积分(在这种情况下为57.3到101.8)。

 X        Y
57.3    794.158
59.4    816.297
61.5    748.34
63.8    834.679
66.1    810.91
68.5    882.33
71.0    837.228
73.7    898.849
76.4    882.926
79.1    928.2
82.0    1122.0341
85.1    1120.606
88.2    1213.764
91.4    1268.479
94.7    1303.058
98.2    1433.6331
101.8   1642.562

以下是我的python脚本:

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

def print_integration(filename='infile'):

    df=pd.read_csv(filename,delim_whitespace=True,header=None)
    x=np.array(df[0])
    y=np.array(df[1])

    length=len(x)
    upper_bound=x[1]
    lower_bound=x[length-1]
    del_x=(lower_bound - upper_bound)/length

    intg=simps(y,dx=del_x)
    print('{}'.format(intg))

if __name__ == '__main__':
    print_integration()

这给了我一个答案:

40741.8545537255

这是正确的答案吗? 预先谢谢你。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)