-
Notifications
You must be signed in to change notification settings - Fork 1
/
4_otherIntergrationTechniques.py
48 lines (35 loc) · 1.06 KB
/
4_otherIntergrationTechniques.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
"""Intergration using peicewise linear method, trapezoidal intergrator, and Simpson's rule"""
import numpy as np
import math as math
def frange(start,stop,step):
i = start
while i < stop:
yield i
i+= step
def trapRule (a,b,f):
total = 0.
h = (b-a)/2
for i in frange(a,b,0.01):
area =.01*(f(i+.01)+f(i))/2.0
"""print(area)"""
total = total + area
return total
def midPoint(a,b,f):
total = 0
for i in frange(a,b,.01):
area = .01*(f((i+(i+.01))/2))
total = total + area
return total
def simpson(a,b,f):
total = 0
for i in frange(a,b,.01):
area = (.01/6)*(f(i)+4*f((i+(i+.01))/2)+f(i+.01))
total = total + area
return total
def f(x):
return (x**3+(2*x**2)-4)
print("Trap rule = ", trapRule(-1,1,f))
print("Midpoint = ", midPoint(-1,1,f))
print ("Simpson = ", simpson(-1,1,f))
print ("Gaussian method in special case of interval [-1,1]",f(-1/(math.sqrt(3)))+f(1/(math.sqrt(3))))
print("The error on all of these values is less than 1% from the original.")