-
Notifications
You must be signed in to change notification settings - Fork 0
/
romberg.py
70 lines (51 loc) · 1.53 KB
/
romberg.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
from prettytable import PrettyTable
import math
def func(x):
return (1/(1+pow(x, 3)))
lowerBound = int(input("Lower Bound: "))
upperBound = int(input("Upper Bound: "))
iteration = int(input("Iteration: "))
header = []
for i in range(iteration+1):
h = "O(h^{})".format(2*(i))
header.append(h)
table = PrettyTable()
column = []
h = upperBound - lowerBound
for i in range(0, iteration+1):
h = h/2
h_res = 0
j = lowerBound
while(j <= upperBound):
j += h
if(j == lowerBound or j == upperBound):
h_res += func(j)
else:
h_res += 2*func(j)
h_res *= h/2
column.append(h_res)
table.add_column(header[0], column)
for i in range(1, iteration+1):
column = []
j = 0
while j <= iteration - i:
low = table[j]
high = table[j+1]
low.border = False
low.header = False
high.border = False
high.header = False
low_res = (float)(low.get_string(fields=[header[i-1]]).strip())
high_res = (float)(high.get_string(fields=[header[i-1]]).strip())
diff = (pow(4,i)*high_res - low_res)/(pow(4, i)-1)
column.append(diff)
j += 1
for k in range(i):
column.append("-")
table.add_column(header[i], column)
print(table)
result_row = table[0]
result_row.border = False
result_row.header = False
result = result_row.get_string(fields = [header[iteration]]).strip()
print("Result: {}".format(result))