This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
107 lines (86 loc) · 2.62 KB
/
main.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
106
107
def get_values():
try:
global L, LimitN, Limit0, h,constant,n
coefficient = str(input("Enter the co-efficient of the integrand F(x): "))
Limit0 = int(input("Enter the Lower Limit: "))
LimitN = int(input("Enter the Upper Limit: "))
h = float(input("Enter the value of h: "))
L = coefficient.split()
constant = int(input("Enter Constant: "))
n = (LimitN - Limit0)/h
except Exception as e:
print("Oops! Value Error Occurred.")
print("Try Again !!\n\n")
get_values()
table = {}
def simpson_rule():
generate_y(n)
if n %2 == 0:
print("I'm Using Simpson's 1/3rd Rule")
part1 = 0
part2 = 0
for keys in table:
if keys % 2 == 0 and keys != 0 and keys != n:
part2 += table[keys]
elif keys % 2 != 0 and keys !=0 and keys !=n:
part1 +=table[keys]
Integral = ((table[0] + table[final]) + 4*part1 + 2*part2)
Integral = Integral * h/3
print(Integral)
elif n%3 == 0:
print("I'm Using Simpson's 3/8th Rule")
part1 = 0
part2 = 0
for keys in table:
if keys % 3 == 0 and keys != 0 and keys != n:
part2 += table[keys]
elif keys %3 != 0 and keys !=0 and keys !=n:
part1 +=table[keys]
Integral = 3*h/8 * ((table[0] + table[final]) + 3*part1 + 2*part2)
print(Integral)
def trapezoidal_rule():
print("I'm using Trapezoidal Rule")
generate_y(n)
part1 = 0
for keys in table:
if keys!=0 and keys!=n:
part1+=table[keys]
Integral = h/2 * ((table[0] + table[final]) + 2*part1)
print(Integral)
coefficient = []
def equation(coefficient):
counter=len(coefficient)
for i in coefficient:
print("{}x^{}".format(i,counter),end=" ")
if(int(i)>0 and counter!=1):
print("+",end=" ")
counter-=1
if(not constant<0):
print("+",end=" ")
print(constant,end=" ")
def find_degree():
for i in L:
int(i)
coefficient.append(i)
equation(coefficient)
if len(L) >= 3 and n!=1:
simpson_rule()
elif len(L) == 1 or len(L) == 2 or n==1:
trapezoidal_rule()
def generate_y(limit):
x = Limit0
for i in range(int(limit+1)):
counter = len(coefficient)
temp = 0
for j in coefficient:
temp += int(j) * pow(x,counter)
counter -= 1
temp += constant
x += h
table[i] = temp
global final
final = i
print(table)
if __name__ == "__main__":
get_values()
find_degree()