Calculate the integral from the Trapezoidal Rule. Args: f (function): the equation f(x). a (float): the initial point. b (float): the final point. n (int): number of intervals. Returns: xi (float): numerical approximation of the definite integral.
(f, a, b, n)
| 32 | |
| 33 | |
| 34 | def trapezoidal(f, a, b, n): |
| 35 | """Calculate the integral from the Trapezoidal Rule. |
| 36 | |
| 37 | Args: |
| 38 | f (function): the equation f(x). |
| 39 | a (float): the initial point. |
| 40 | b (float): the final point. |
| 41 | n (int): number of intervals. |
| 42 | |
| 43 | Returns: |
| 44 | xi (float): numerical approximation of the definite integral. |
| 45 | """ |
| 46 | h = (b - a) / n |
| 47 | |
| 48 | sum_x = 0 |
| 49 | |
| 50 | for i in range(0, n - 1): |
| 51 | x = a + (i + 1) * h |
| 52 | sum_x += f(x) |
| 53 | |
| 54 | xi = h / 2 * (f(a) + 2 * sum_x + f(b)) |
| 55 | return xi |
| 56 | |
| 57 | |
| 58 | def simpson_array(x, y): |