mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-03 13:28:40 +00:00
added descriptive names
This commit is contained in:
parent
56a052678d
commit
aa66c4b48c
@ -18,9 +18,9 @@ def get_inputs() -> tuple:
|
|||||||
('1/(1+x**2)', 1.0, -1.0)
|
('1/(1+x**2)', 1.0, -1.0)
|
||||||
"""
|
"""
|
||||||
func = input("Enter function with variable as x: ")
|
func = input("Enter function with variable as x: ")
|
||||||
a = float(input("Enter lower limit: "))
|
lower_limit = float(input("Enter lower limit: "))
|
||||||
b = float(input("Enter upper limit: "))
|
upper_limit = float(input("Enter upper limit: "))
|
||||||
return func, a, b
|
return func, lower_limit, upper_limit
|
||||||
|
|
||||||
|
|
||||||
def safe_function_eval(func_str: str) -> float:
|
def safe_function_eval(func_str: str) -> float:
|
||||||
@ -32,7 +32,6 @@ def safe_function_eval(func_str: str) -> float:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
float: The evaluated function result.
|
float: The evaluated function result.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> f = safe_function_eval('x**2')
|
>>> f = safe_function_eval('x**2')
|
||||||
>>> f(3)
|
>>> f(3)
|
||||||
@ -54,14 +53,14 @@ def safe_function_eval(func_str: str) -> float:
|
|||||||
return lambda_func
|
return lambda_func
|
||||||
|
|
||||||
|
|
||||||
def compute_table(func: str, a: float, b: float, acc: int) -> tuple:
|
def compute_table(func: str, lower_limit: float, upper_limit: float, acc: int) -> tuple:
|
||||||
"""
|
"""
|
||||||
Compute the table of function values based on the limits and accuracy.
|
Compute the table of function values based on the limits and accuracy.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
func (str): The mathematical function with the variable 'x' as a string.
|
func (str): The mathematical function with the variable 'x' as a string.
|
||||||
a (float): The lower limit of the integral.
|
lower_limit (float): The lower limit of the integral.
|
||||||
b (float): The upper limit of the integral.
|
upper_limit (float): The upper limit of the integral.
|
||||||
acc (int): The number of subdivisions for accuracy.
|
acc (int): The number of subdivisions for accuracy.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -76,8 +75,8 @@ def compute_table(func: str, a: float, b: float, acc: int) -> tuple:
|
|||||||
"""
|
"""
|
||||||
# Weddle's rule requires number of intervals as a multiple of 6 for accuracy
|
# Weddle's rule requires number of intervals as a multiple of 6 for accuracy
|
||||||
n_points = acc * 6 + 1
|
n_points = acc * 6 + 1
|
||||||
h = (b - a) / (n_points - 1)
|
h = (upper_limit - lower_limit) / (n_points - 1)
|
||||||
x_vals = np.linspace(a, b, n_points)
|
x_vals = np.linspace(lower_limit, upper_limit, n_points)
|
||||||
|
|
||||||
# Evaluate function values at all points
|
# Evaluate function values at all points
|
||||||
table = func(x_vals)
|
table = func(x_vals)
|
||||||
@ -111,14 +110,14 @@ def apply_weights(table: list) -> list:
|
|||||||
return add
|
return add
|
||||||
|
|
||||||
|
|
||||||
def compute_solution(add: list, table: list, h: float) -> float:
|
def compute_solution(add: list, table: list, step_size: float) -> float:
|
||||||
"""
|
"""
|
||||||
Compute the final solution using the weighted values and table.
|
Compute the final solution using the weighted values and table.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
add (list): A list of weighted values from apply_weights.
|
add (list): A list of weighted values from apply_weights.
|
||||||
table (list): A list of function values.
|
table (list): A list of function values.
|
||||||
h (float): The step size (h) calculated from the limits and accuracy.
|
step_size (float): The step size calculated from the limits and accuracy.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
float: The final computed integral solution.
|
float: The final computed integral solution.
|
||||||
@ -128,7 +127,7 @@ def compute_solution(add: list, table: list, h: float) -> float:
|
|||||||
... -0.866, -1.0], 0.5235983333333333)
|
... -0.866, -1.0], 0.5235983333333333)
|
||||||
0.7853975
|
0.7853975
|
||||||
"""
|
"""
|
||||||
return 0.3 * h * (sum(add) + table[0] + table[-1])
|
return 0.3 * step_size * (sum(add) + table[0] + table[-1])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user