@@ -18,9 +18,9 @@ def get_inputs() -> tuple:
18
18
('1/(1+x**2)', 1.0, -1.0)
19
19
"""
20
20
func = input ("Enter function with variable as x: " )
21
- a = float (input ("Enter lower limit: " ))
22
- b = float (input ("Enter upper limit: " ))
23
- return func , a , b
21
+ lower_limit = float (input ("Enter lower limit: " ))
22
+ upper_limit = float (input ("Enter upper limit: " ))
23
+ return func , lower_limit , upper_limit
24
24
25
25
26
26
def safe_function_eval (func_str : str ) -> float :
@@ -32,7 +32,6 @@ def safe_function_eval(func_str: str) -> float:
32
32
33
33
Returns:
34
34
float: The evaluated function result.
35
-
36
35
Examples:
37
36
>>> f = safe_function_eval('x**2')
38
37
>>> f(3)
@@ -54,14 +53,14 @@ def safe_function_eval(func_str: str) -> float:
54
53
return lambda_func
55
54
56
55
57
- def compute_table (func : str , a : float , b : float , acc : int ) -> tuple :
56
+ def compute_table (func : str , lower_limit : float , upper_limit : float , acc : int ) -> tuple :
58
57
"""
59
58
Compute the table of function values based on the limits and accuracy.
60
59
61
60
Args:
62
61
func (str): The mathematical function with the variable 'x' as a string.
63
- a (float): The lower limit of the integral.
64
- b (float): The upper limit of the integral.
62
+ lower_limit (float): The lower limit of the integral.
63
+ upper_limit (float): The upper limit of the integral.
65
64
acc (int): The number of subdivisions for accuracy.
66
65
67
66
Returns:
@@ -76,8 +75,8 @@ def compute_table(func: str, a: float, b: float, acc: int) -> tuple:
76
75
"""
77
76
# Weddle's rule requires number of intervals as a multiple of 6 for accuracy
78
77
n_points = acc * 6 + 1
79
- h = (b - a ) / (n_points - 1 )
80
- x_vals = np .linspace (a , b , n_points )
78
+ h = (upper_limit - lower_limit ) / (n_points - 1 )
79
+ x_vals = np .linspace (lower_limit , upper_limit , n_points )
81
80
82
81
# Evaluate function values at all points
83
82
table = func (x_vals )
@@ -111,14 +110,14 @@ def apply_weights(table: list) -> list:
111
110
return add
112
111
113
112
114
- def compute_solution (add : list , table : list , h : float ) -> float :
113
+ def compute_solution (add : list , table : list , step_size : float ) -> float :
115
114
"""
116
115
Compute the final solution using the weighted values and table.
117
116
118
117
Args:
119
118
add (list): A list of weighted values from apply_weights.
120
119
table (list): A list of function values.
121
- h (float): The step size (h) calculated from the limits and accuracy.
120
+ step_size (float): The step size calculated from the limits and accuracy.
122
121
123
122
Returns:
124
123
float: The final computed integral solution.
@@ -128,7 +127,7 @@ def compute_solution(add: list, table: list, h: float) -> float:
128
127
... -0.866, -1.0], 0.5235983333333333)
129
128
0.7853975
130
129
"""
131
- return 0.3 * h * (sum (add ) + table [0 ] + table [- 1 ])
130
+ return 0.3 * step_size * (sum (add ) + table [0 ] + table [- 1 ])
132
131
133
132
134
133
if __name__ == "__main__" :
0 commit comments