4
4
5
5
from typing import Callable , Union
6
6
7
- def trapezoidal_area (fnc : Callable [[Union [int , float ]], Union [int , float ]],
8
- x_start : Union [int , float ],
9
- x_end : Union [int , float ],
10
- steps : int = 100 ) -> float :
7
+
8
+ def trapezoidal_area (
9
+ fnc : Callable [[Union [int , float ]], Union [int , float ]],
10
+ x_start : Union [int , float ],
11
+ x_end : Union [int , float ],
12
+ steps : int = 100 ,
13
+ ) -> float :
11
14
12
15
"""
13
16
Treats curve as a collection of linear lines and sums the area of the
@@ -39,9 +42,9 @@ def trapezoidal_area(fnc: Callable[[Union[int, float]], Union[int, float]],
39
42
40
43
# Approximates small segments of curve as linear and solve
41
44
# for trapezoidal area
42
- x2 = (x_end - x_start )/ steps + x1
45
+ x2 = (x_end - x_start ) / steps + x1
43
46
fx2 = fnc (x2 )
44
- area += abs (fx2 + fx1 ) * (x2 - x1 )/ 2
47
+ area += abs (fx2 + fx1 ) * (x2 - x1 ) / 2
45
48
46
49
# Increment step
47
50
x1 = x2
@@ -52,12 +55,12 @@ def trapezoidal_area(fnc: Callable[[Union[int, float]], Union[int, float]],
52
55
if __name__ == "__main__" :
53
56
54
57
def f (x ):
55
- return x ** 3
58
+ return x ** 3
56
59
57
60
print ("f(x) = x^3" )
58
61
print ("The area between the curve, x = -10, x = 10 and the x axis is:" )
59
62
i = 10
60
63
while i <= 100000 :
61
64
area = trapezoidal_area (f , - 5 , 5 , i )
62
65
print ("with {} steps: {}" .format (i , area ))
63
- i *= 10
66
+ i *= 10
0 commit comments