Python Programming In the on-line resources you will find a file called velociti
ID: 3602701 • Letter: P
Question
Python Programming
In the on-line resources you will find a file called velocities.txt, which contains two columns of numbers, the first representing time t in seconds and the second the x-velocity in meters per second of a particle, measured once every second from time t = 0 to t = 100. The first few lines look like this:
00
1 0.069478
2 0.137694
3 0.204332
4 0.269083
5 0.331656
Write a program to do the following:
a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the particle in the x direction as a function of time. See Section 2.4.3 on page 57 if you want a reminder of how to read data from a file.
b) Extend your program to make a graph that shows, on the same plot, both the original velocity curve and the distance traveled as a function of time.
Explanation / Answer
Ans a)
with open('Test.txt') as inf:
lines=inf.readAllLines()
column1 = [] # This will read the 1st column of the text file.
column2 = [] # This will read the 2nd column of the text file.
for x in lines:
column1.append(x.split(' ')[0])
column2.append(x.split(' ')[1])
u = column2[0] # Initial velocity at t = 0
v = column2[100] # Final velocity at t = 100
t = 100 # total time in seconds
f = (v-u)/t # From the formula (v= u+ft) we find the acceleration f
#S = (u*t1)+ 0.5*f*(t1^2) # This will give the total travelled distance(s) in term of a function of time (t1)
#so the formula will be printed as
Print ("Distance = "u+"t1+0.5"+f+"t1^2") # So this will be the function of t1 which will give the distance in x direction
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.