-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromberg_with_trapezoid.py
112 lines (84 loc) · 2.97 KB
/
romberg_with_trapezoid.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
108
109
110
111
112
from prettytable import PrettyTable
import math
#WOLFANS = Hasil integrasi fungsi untuk LOWBOUND dan UPBOUND dihitung menggunakan wolfram
WOLFANS = 0.835648848264721053337103459700110766786522127
# Define function to integrate
def f(x):
return (1/(1+pow(x, 3)))
# Define trapezoidal method
def trapezoidal(x0,xn,n):
# calculating step size
h = (xn - x0) / n
# Finding sum
integration = f(x0) + f(xn)
for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)
# Finding final integration value
integration = integration * h/2
return integration
# Define romberg method
def romberg(lower, upper, iteration):
lowerBound = lower
upperBound = upper
iteration = 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 += f(j)
else:
h_res += 2*f(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 for Romberg's Integration method")
print(table)
result_row = table[0]
result_row.border = False
result_row.header = False
result = result_row.get_string(fields = [header[iteration]]).strip()
return result
# Main
lower_limit = int(input("Lower limit: "))
upper_limit = int(input("Upper limit: "))
iteration = int(input("Iteration: "))
result_trapezoid = trapezoidal(lower_limit, upper_limit, 2**(iteration)-1)
result_romberg = romberg(lower_limit, upper_limit, iteration)
error_trapezoid = (WOLFANS - (float)(result_trapezoid)) / 100
error_trapezoid = "{:.12f}".format(error_trapezoid)
error_romberg = (WOLFANS - (float)(result_romberg)) / 100
error_romberg= "{:.12f}".format(error_romberg)
print("Actual result: " + str(WOLFANS))
print("Result using trapezoid method: " + str(result_trapezoid))
print("Result using romberg method: " + str(result_romberg))
print("Error using trapezoidal method: " + error_trapezoid + "%")
print("Error using romberg method: " + error_romberg + "%")