-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.py
105 lines (90 loc) · 2.93 KB
/
integration.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
93
94
95
96
97
98
99
100
101
102
103
104
105
import numpy as np
import math
def f1(x):
return 1 / (1 + x)
def f2(x):
return math.tan(x)
def f3(x):
return x * math.sin(x)
def trapezoidal(start, end, n, f):
h = float((end - start) / n)
ans = f(start) + f(end)
while(start + h != end):
ans += 2 * f(start + h)
start += h
return ans * (h / 2)
def simpson(start, end, n, f):
h = float((end - start) / n)
ans = f(start) + f(end)
i = 0
while(start + h != end):
if i % 2 == 0 :
ans += 4 * f(start + h)
else:
ans += 2 * f(start + h)
start += h
i += 1
return ans * (h / 3)
def midpoint(start, end, f):
return (end - start) * f((start + end) / 2)
def two_points_gauss_quadrature(start, end, f):
x = (end - start) / 2
y = (end + start) / 2
return x * (f(x * (-1 / math.sqrt(3)) + y) + f(x * (1 / math.sqrt(3)) + y))
def test_trapezoidal():
"""
Example:
F1(X) = 1 / (1 + x)
Integration from 0 to 0.1 using 1 segment = 0.09545
F2(X) = tan(x)
Integration from 0 to 0.78539816339 (PI / 4) using 4 segments = 0.3498
F3(X) = x * sin(x)
Integration from 0 to 6.28318530718 (2 * PI) using 1 segment = 0
Integration from 0 to 6.28318530718 (2 * PI) using 2 segments = 0
Integration from 0 to 6.28318530718 (2 * PI) using 4 segments = -4.935
"""
print("Integrating F(x) using Trapezoidal rule:")
start = float(input("Enter the start point: "))
end = float(input("Enter the end point: "))
n = int(input("Enter the number of segments: "))
ans = trapezoidal(start, end, n, f3)
print("The answer is {}".format(ans))
def test_simpson():
"""
Example:
F1(X) = 1 / (1 + x)
Integration from 0 to 0.1 using 1 segment
= 0.09531
"""
print("Integrating F(x) using Simpson rule:")
start = float(input("Enter the start point: "))
end = float(input("Enter the end point: "))
n = int(input("Enter the number of segments: "))
ans = simpson(start, end, n, f1)
print("The answer is {}".format(ans))
def test_midpoint():
"""
Example:
F1(X) = 1 / (1 + x)
Integration from 0 to 0.1 using 1 segment
= 0.09524
"""
print("Integrating F(x) using Midpoint rule:")
start = float(input("Enter the start point: "))
end = float(input("Enter the end point: "))
ans = midpoint(start, end, f1)
print("The answer is {}".format(ans))
def test_gauss_quadrature():
"""
Example:
F1(X) = 1 / (1 + x)
Integration from 0 to 0.1 = 0.09531
F3(X) = x * sin(x)
Integration from 0 to 6.28318530718 (2 * PI) = -11.06
"""
print("Integrating F(x) using Two Points Gauss Quadrature rule:")
start = float(input("Enter the start point: "))
end = float(input("Enter the end point: "))
ans = two_points_gauss_quadrature(start, end, f3)
print("The answer is {}".format(ans))
test_trapezoidal()