1
1
"""
2
2
Numerical 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
-
10
3
"""
11
4
12
5
13
- def method_1 (boundary , steps ):
6
+ def trapezoidal_rule (boundary , steps ):
14
7
"""
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
26
23
"""
27
24
h = (boundary [1 ] - boundary [0 ]) / steps
28
25
a = boundary [0 ]
@@ -31,57 +28,73 @@ def method_1(boundary, steps):
31
28
y = 0.0
32
29
y += (h / 2.0 ) * f (a )
33
30
for i in x_i :
34
- # print(i)
35
31
y += h * f (i )
36
32
y += (h / 2.0 ) * f (b )
37
33
return y
38
34
39
35
40
36
def make_points (a , b , h ):
41
37
"""
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]
48
48
>>> list(make_points(0, 10, 2.5))
49
49
[2.5, 5.0, 7.5]
50
-
51
50
>>> list(make_points(0, 10, 2))
52
51
[2, 4, 6, 8]
53
-
54
52
>>> list(make_points(1, 21, 5))
55
53
[6, 11, 16]
56
-
57
54
>>> list(make_points(1, 5, 2))
58
55
[3]
59
-
60
56
>>> list(make_points(1, 4, 3))
61
57
[]
62
58
"""
63
59
x = a + h
64
60
while x <= (b - h ):
65
61
yield x
66
- x = x + h
62
+ x += h
67
63
68
64
69
- def f (x ): # enter your function here
65
+ def f (x ):
70
66
"""
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
74
78
"""
75
- y = (x - 0 ) * (x - 0 )
76
- return y
79
+ return x ** 2
77
80
78
81
79
82
def 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 )
85
98
print (f"y = { y } " )
86
99
87
100
0 commit comments