thereiswaldo
thereiswaldo

Reputation: 87

Integrating data from txt file in python

I have three columns of data (time (t), Thrust (T), distance (s)) from which I would like to calculate the work performed. The text (logdata.txt) file containing the data is structured as follows:

time, thrust, distance

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

The examples I have encountered only show how to use the scipy.integrate functions when setting up your own function for f(x), however in this case I have the raw data (hence no function). I would like to integrate the thrust over the distance covered using the Simpson rule. The code I use is as follows:

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)

However, this does not give me a reasonable value. Does anyone know how to go about this?

Upvotes: 0

Views: 323

Answers (1)

Mortz
Mortz

Reputation: 4879

You need to provide the x, or in this case, the distance as well -

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

I can verify this is roughly right by checking the average "thrust" and the distance moved

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

Upvotes: 0

Related Questions