Salah satu kelemahan dari metode Trapezoidal adalah kita harus menggunakan jumlah interval yang besar untuk memperoleh akurasi yang diharapkan. Buatlah sebuah program komputer untuk menjelaskan bagaimana metode Integrasi Romberg dapat mengatasi kelemahan tersebut.
Trapezoidal Rule is a rule that evaluates the area under the curves by dividing the total area into smaller trapezoids rather than using rectangles. This integration works by approximating the region under the graph of a function as a trapezoid, and it calculates the area. This rule takes the average of the left and the right sum.
Let f(x) be a continuous function on the interval [a, b]. Now divide the intervals [a, b] into n equal subintervals with each of width,
Then the Trapezoidal Rule formula for area approximating the definite integral
Where,
Richardson Extrapolation is a method for boosting the accuracy of certain numerical procedures. This one does the elimination of errors. Though it can only remove errors of the form:
where
Romberg intergation combines the Composite Trapezoidal Rule with Richardson Extrapolation.
Below is the overview of the integration process:
Let
- Starts with the computation of one panel and two panels via Composite Trapezoidal Rule
- We get the leading error term
$C_1h^2$ then eliminate by Richardson Extrapolation using p = 2 (since that is the exponent of the error)
-
Get the leading error term
$C_2h^4$ then eliminate by Richardson Extrapolation using p = 4 -
We do this until it converges to the wanted solution
this is exactly what we are doing
This tells us that we need to compute where the two arrows are from to compute where the two arrows are pointing at. The most accurate estimate of the integral is always the last diagonal term of the array. This process is continued until the difference between two successive diagonal terms becomes sufficiently small.
The overview above can be summarized into the formula:
*note:
main function
def main():
a = 0 # lower boundary
b = math.pi # upper boundary
iter = 4 # number of iteration
result = romberg(a, b, iter)
print("R({},{}) = {}".format(iter, iter, result))
if __name__ == "__main__":
main()
Romberg function
def romberg(a, b, iter):
matrix = [] # matrix to store the romberg value
# making empty 4 by 4 matrix
for _ in range(0, iter):
matrix.append([None for _ in range(0, iter)])
section = 1 # the number of section for CTR
iteration = 0 # iteration of the while loop
while (section < 2**iter):
# getting the evenly spread points
points = np.linspace(a, b, section + 1)
matrix[iteration][0] = CTR(points)
section *= 2 # increment the section by times 2
iteration += 1 # increment the iteration by 1
# calculating Romberg Integration
for i in range(1, iter):
for j in range(1, iter):
if (i < j):
continue
# Romberg formula
temp = (4**(j) * matrix[i][j - 1] - matrix[i - 1][j - 1]) / (4**(j) - 1)
matrix[i][j] = temp
return matrix[iter - 1][iter - 1]
CTR function
def CTR(points: np.ndarray) -> float:
h = points[1] - points[0]
if(points.size < 2):
return -1
else:
temp = 0
for i in range(1, points.size - 1):
temp += f(points[i])
return h / 2 * (f(points[0]) + 2 * temp + f(points[points.size - 1]))
Integration tested
def f(x):
return math.sin(x)
output:
R(4,4) = 2.0000055499796705