-
Notifications
You must be signed in to change notification settings - Fork 8
/
Integrate_Trap.py
executable file
·92 lines (69 loc) · 2.81 KB
/
Integrate_Trap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
###################################################################
# #
# This program will integrate a series of points using the trap- #
# ezoid rule (average of left and right rectangle method). It is #
# less accurate than the Simpsons method or midpoint method, but #
# it is good for TI when both ends (lambda = 0 and lambda = 1) #
# are in the data set. #
# #
# Last update: 11/21/2009 (by jms) #
# by Jason Swails #
# #
###################################################################
# import needed modules
import math, sys
########################### USER-DEFINED FUNCTIONS ################
def PrintUsage():
print 'Usage: Integrate_Trap.py <data_file>\n'
print '<data_file> must be 2 columns space-delimited <x> <y>'
###################################################################
# get command-line arguments
if len(sys.argv) != 2 or '-help' in sys.argv[1]:
PrintUsage
sys.exit()
# open the file, load the data into memory, and then close the file
try:
datafile = open(sys.argv[1],'r')
except IOError:
print >> sys.stderr, "Error: Data file '" + sys.argv[1] + "' does not exist!"
PrintUsage
sys.exit()
lines = datafile.readlines()
datafile.close()
# end reading data
# strip the lines so that the first two and last two lines are both data.
end = len(lines) - 1
secondlast = len(lines) - 2
while len(lines[0].split()) != 2:
lines.pop(0)
while len(lines[1].split()) != 2:
lines.pop(1)
while len(lines[end].split()) != 2:
lines.pop(end)
end = len(lines) - 1
secondlast = len(lines) - 2
while len(lines[secondlast].split()) != 2:
lines.pop(secondlast)
secondlast = len(lines) - 2
# initialize the sums
line1 = lines[len(lines)-1].split()
line2 = lines[len(lines)-2].split()
right = float(line2[1]) * (float(line2[0]) - float(line1[0]))
line1 = lines[0].split()
line2 = lines[1].split()
left = float(line1[1]) * (float(line2[0]) - float(line1[0]))
# now cycle through all of the data
for x in range(1, len(lines) - 1):
# Get rid of any blank or comment lines
while len(lines[x].split()) != 2:
lines.pop(x)
# load in the lines
line1 = lines[x-1].split()
line2 = lines[x].split()
line3 = lines[x+1].split()
left = left + float(line2[1]) * (float(line3[0]) - float(line2[0]))
right = right + float(line2[1]) * (float(line2[0]) - float(line1[0]))
print 'Left-rectangle approximation : ' + str(left)
print 'Right-rectangle approximation : ' + str(right)
print 'Trapezoidal appoximation : ' + str((left + right) / 2)