11"""
22Numerical integration or quadrature for a smooth function f with known values at x_i
3-
4- This method is the classical approach of suming 'Equally Spaced Abscissas'
5-
6- method 1:
7- "extended trapezoidal rule"
8- int(f) = dx/2 * (f1 + 2f2 + ... + fn)
9-
103"""
114
125
13- def method_1 (boundary , steps ):
6+ def trapezoidal_rule (boundary , steps ):
147 """
15- Apply the extended trapezoidal rule to approximate the integral of function f(x)
16- over the interval defined by 'boundary' with the number of 'steps'.
17-
18- Args:
19- boundary (list of floats): A list containing the start and end values [a, b].
20- steps (int): The number of steps or subintervals.
21- Returns:
22- float: Approximation of the integral of f(x) over [a, b].
23- Examples:
24- >>> method_1([0, 1], 10)
25- 0.3349999999999999
8+ Implements the extended trapezoidal rule for numerical integration.
9+ The function f(x) is provided below.
10+
11+ :param boundary: List containing the lower and upper bounds of integration [a, b]
12+ :param steps: The number of steps (intervals) used in the approximation
13+ :return: The numerical approximation of the integral
14+
15+ >>> abs(trapezoidal_rule([0, 1], 10) - 0.33333) < 0.01
16+ True
17+ >>> abs(trapezoidal_rule([0, 1], 100) - 0.33333) < 0.01
18+ True
19+ >>> abs(trapezoidal_rule([0, 2], 1000) - 2.66667) < 0.01
20+ True
21+ >>> abs(trapezoidal_rule([1, 2], 1000) - 2.33333) < 0.01
22+ True
2623 """
2724 h = (boundary [1 ] - boundary [0 ]) / steps
2825 a = boundary [0 ]
@@ -31,57 +28,73 @@ def method_1(boundary, steps):
3128 y = 0.0
3229 y += (h / 2.0 ) * f (a )
3330 for i in x_i :
34- # print(i)
3531 y += h * f (i )
3632 y += (h / 2.0 ) * f (b )
3733 return y
3834
3935
4036def make_points (a , b , h ):
4137 """
42- Generates points between 'a' and 'b' with step size 'h', excluding the end points.
43- Args:
44- a (float): Start value
45- b (float): End value
46- h (float): Step size
47- Examples:
38+ Generates points between a and b with step size h for trapezoidal integration.
39+
40+ :param a: The lower bound of integration
41+ :param b: The upper bound of integration
42+ :param h: The step size
43+ :yield: The next x-value in the range (a, b)
44+
45+ >>> list(make_points(0, 1, 0.1)) # doctest: +NORMALIZE_WHITESPACE
46+ [0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, \
47+ 0.8999999999999999]
4848 >>> list(make_points(0, 10, 2.5))
4949 [2.5, 5.0, 7.5]
50-
5150 >>> list(make_points(0, 10, 2))
5251 [2, 4, 6, 8]
53-
5452 >>> list(make_points(1, 21, 5))
5553 [6, 11, 16]
56-
5754 >>> list(make_points(1, 5, 2))
5855 [3]
59-
6056 >>> list(make_points(1, 4, 3))
6157 []
6258 """
6359 x = a + h
6460 while x <= (b - h ):
6561 yield x
66- x = x + h
62+ x += h
6763
6864
69- def f (x ): # enter your function here
65+ def f (x ):
7066 """
71- Example:
72- >>> f(2)
73- 4
67+ This is the function to integrate, f(x) = (x - 0)^2 = x^2.
68+
69+ :param x: The input value
70+ :return: The value of f(x)
71+
72+ >>> f(0)
73+ 0
74+ >>> f(1)
75+ 1
76+ >>> f(0.5)
77+ 0.25
7478 """
75- y = (x - 0 ) * (x - 0 )
76- return y
79+ return x ** 2
7780
7881
7982def main ():
80- a = 0.0 # Lower bound of integration
81- b = 1.0 # Upper bound of integration
82- steps = 10.0 # define number of steps or resolution
83- boundary = [a , b ] # define boundary of integration
84- y = method_1 (boundary , steps )
83+ """
84+ Main function to test the trapezoidal rule.
85+ :a: Lower bound of integration
86+ :b: Upper bound of integration
87+ :steps: define number of steps or resolution
88+ :boundary: define boundary of integration
89+
90+ >>> main()
91+ y = 0.3349999999999999
92+ """
93+ a = 0.0
94+ b = 1.0
95+ steps = 10.0
96+ boundary = [a , b ]
97+ y = trapezoidal_rule (boundary , steps )
8598 print (f"y = { y } " )
8699
87100
0 commit comments