-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpsonpi.py
More file actions
executable file
·43 lines (34 loc) · 1.49 KB
/
simpsonpi.py
File metadata and controls
executable file
·43 lines (34 loc) · 1.49 KB
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
#!/usr/bin/python -B
from math import sqrt
from math_parse import eqstr2func
from numpy import arange
import argparse
import sys
def piover4(x):
return sqrt(1-x*x)
def simpson(func, start, end, steps):
width = (end-start) / steps
xvals = arange(start, end+width, width)
area = 0.0
for a in range (0, steps):
try:
area += width * (func(xvals[a]) + func(xvals[a+1]))/2
except ValueError:
print ("Oops! ValueError:")
print ("Step #:", a)
print ("Val #1:", xvals[a])
print ("Val #2", xvals[a+1])
sys.exit(3)
return area
parser = argparse.ArgumentParser(description='Calculate the integral of a function using Simpson\'s method')
parser.add_argument('-s', type=float, required=False, default=0.0, help="The starting value for the definite integral. If none is given, will default to 0.0")
parser.add_argument('-e', type=float, required=False, default=1.0, help="The ending value for the definte intgral. If none is given, will default to 1.0")
parser.add_argument('-n', type=int, required=False, default=10, help="Number of steps to use. If none is given, will default to 10")
parser.add_argument('-f', required=False, default="", help="The function to integrate. (Must contain exactly one variable.) If none is given, will default to sqrt(1-x*x), which if integrated from 0 to 1 will give pi/4")
args = parser.parse_args()
if (args.f == ""):
func = piover4
else:
func = eqstr2func(args.f, forceparam=1)
val = simpson(func, args.s, args.e, args.n)
print val