MCPcopy Index your code
hub / github.com/cfgnunes/numerical-methods-python / trapezoidal

Function trapezoidal

integration.py:34–55  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

32
33
34def 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
58def simpson_array(x, y):

Callers

nothing calls this directly

Calls 1

fFunction · 0.85

Tested by

no test coverage detected