【问题标题】:Integrating data from txt file in python在python中集成来自txt文件的数据
【发布时间】:2022-01-17 16:20:00
【问题描述】:

我有三列数据(时间 (t)、推力 (T)、距离 (s)),我想从中计算完成的工作。包含数据的文本(logdata.txt)文件结构如下:

time, thrust, distance

1 2405 501
2 2500 702
3 2500 903
4 2580 1100
5 2610 1300 

我遇到的示例仅显示了在为 f(x) 设置自己的函数时如何使用 scipy.integrate 函数,但是在这种情况下,我有原始数据(因此没有函数)。我想使用辛普森规则整合推力覆盖的距离。我使用的代码如下:

from scipy import integrate
import numpy as np

data = np.loadtxt("logdata.txt")

#selecting the whole second and third column of the text file
x = data[1]
y = data[2]

integrate.simps(y)

但是,这并没有给我一个合理的价值。有谁知道该怎么做?

【问题讨论】:

  • 不应该是integrate.simps(y=data[1], x=data[2])吗?
  • 你是对的

标签: python scipy simpsons-rule


【解决方案1】:

您需要提供x,或者在这种情况下,还需要提供距离-

from scipy import integrate
thrust = [2405, 2500, 2500, 2580, 2610]
distance = [501, 702, 903, 1100, 1300]
integrate.simps(thrust, distance)
# 2019697.0508037228

我可以通过检查平均“推力”和移动距离来验证这是否大致正确

avg_thrust = sum(thrust) / len(thrust)
# 2519.0
distance_moved = 1300 - 500
# 800
work = 2519 * 800 = 2015200

【讨论】:

  • 非常感谢!对此的进一步跟进,因为似乎我不是通过使用 data[1] 选择列而是选择行,我将如何选择完整的列?
  • 我明白了,我必须使用 data[:,1] 而不是 data[1]
猜你喜欢
  • 2018-11-02
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
相关资源
最近更新 更多