cmlabs.integrate.test_rectangle_error

cmlabs.integrate.test_rectangle_error()[source]

Reach accuracy \(\epsilon\) error for rectangle rule.

\[\begin{split}\begin{gather} |I_n - I_{2n}| \leq \epsilon, \\ |I_{2n} - I_{4n}| \leq \epsilon, \\ \ldots \\ |I_{2^k n} - I_{2^{k+1} n}| \leq \epsilon, \\ \end{gather}\end{split}\]

where \(I_n\) is the integral of the function \(f(x)\) over the interval \([a, b]\) using the rectangle rule with \(n\) subintervals, and \(\epsilon\) is the desired accuracy.

Notes

\[\begin{split}\begin{gather} \int_a^b f(x) \, dx = \int_{0.5}^{1} x - \log_{10}(x + 2) \, dx \\ = \frac{1\ln{10} + 20\ln{5} - 24\ln{3} - 20\ln{2} + 4}{8\ln{10}} \\ \approx 0.1556335 \end{gather}\end{split}\]

Results

>>> # Test 1: Rectangle Rule Error
>>> # - f(x) = x - lg(x + 2)
>>> # - X_n:  [0.5   0.625 0.75  0.875 1.   ]
>>> # - Y_n:  [0.102 0.206 0.311 0.416 0.523]
>>> # - X_2n:  [0.5   0.556 0.611 ... 0.889 0.944 1.   ]
>>> # - Y_2n:  [0.102 0.148 0.194 ... 0.428 0.475 0.523]
>>> rectangle(X_n, Y_n, method='left')
>>> # - I_n:  0.12937001759125935
>>> rectangle(X_2n, Y_2n, method='left')
>>> # - I_2n:  0.14395153508130418
>>> # |I_n - I_2n|:  0.014581517490044826
>>> # 0.014581517490044826 <= 0.01
>>> # False
>>> # - X_4n:  [0.5   0.526 0.553 ... 0.947 0.974 1.   ]
>>> # - Y_4n:  [0.102 0.124 0.146 ... 0.478 0.5   0.523]
>>> rectangle(X_4n, Y_4n, method='left')
>>> # - I_4n:  0.15009808046684736
>>> # |I_2n - I_4n|:  0.006146545385543184
>>> # 0.006146545385543184 <= 0.01
>>> # True
>>> # |I_2n - I_4n|:  0.006146545385543184

See also

rectangle