From b55043a8ad177748254adcad46fe2f3a2173e916 Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 14:33:17 +0530 Subject: [PATCH 01/11] Added Fixed Point Iteration Method in numerical_analysis --- .../fixed_point_iteration.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 maths/numerical_analysis/fixed_point_iteration.py diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py new file mode 100644 index 000000000000..309fcd7cdddc --- /dev/null +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -0,0 +1,53 @@ +""" +Fixed Point Iteration Method + +This method is used to find an approximate solution to a given equation f(x) = 0. +The function g(x) is derived from f(x) such that x = g(x). + +Example: +-------- +>>> def g(x): +... return (x**2 + 2) / 3 +>>> fixed_point_iteration(g, 1.0) +1.618033988749895 +""" + +def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): + """ + Perform Fixed Point Iteration to find the root of the equation x = g(x). + + Parameters: + ----------- + g : function + The function g(x) derived from f(x) such that x = g(x). + x0 : float + Initial guess for the root. + tol : float, optional + Tolerance for the convergence of the method. Default is 1e-7. + max_iter : int, optional + Maximum number of iterations. Default is 1000. + + Returns: + -------- + float + The approximate root of the equation x = g(x). + + Raises: + ------- + ValueError + If the method does not converge within the maximum number of iterations. + """ + x = x0 + for i in range(max_iter): + x_new = g(x) + if abs(x_new - x) < tol: + return x_new + x = x_new + raise ValueError("Fixed Point Iteration did not converge") + +if __name__ == "__main__": + def g(x): + return (x**2 + 2) / 3 + + root = fixed_point_iteration(g, 1.0) + print(f"The root is: {root}") \ No newline at end of file From 9b4569186a4dd7d8663585bd2547d281b283d1f0 Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 14:33:50 +0530 Subject: [PATCH 02/11] Added Modified Newton Raphson Method in numerical_analysis --- .../modified_newton_raphson.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 maths/numerical_analysis/modified_newton_raphson.py diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py new file mode 100644 index 000000000000..9aa22efc8121 --- /dev/null +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -0,0 +1,76 @@ +""" +Modified Newton-Raphson Method + +This method is used to find an approximate solution to a given equation f(x) = 0. +It is an iterative method that modifies the standard Newton-Raphson method to improve +convergence in certain cases, mostly when multiplicity is more than 1. + +Example: +-------- +>>> import math +>>> def f(x): +... return x**3 - 2*x - 5 +>>> def f_prime(x): +... return 3*x**2 - 2 +>>> modified_newton_raphson(f, f_prime, 2.0) +2.0945514815423265 +""" + +from typing import Callable + +def modified_newton_raphson( + f: Callable[[float], float], + f_prime: Callable[[float], float], + x0: float, + tol: float = 1e-7, + max_iter: int = 1000 +) -> float: + """ + Perform the Modified Newton-Raphson method to find the root of the equation f(x) = 0. + + Parameters: + ----------- + f : function + The function for which we want to find the root. + f_prime : function + The derivative of the function f. + x0 : float + Initial guess for the root. + tol : float, optional + Tolerance for the convergence of the method. Default is 1e-7. + max_iter : int, optional + Maximum number of iterations. Default is 1000. + + Returns: + -------- + float + The approximate root of the equation f(x) = 0. + + Raises: + ------- + ValueError + If the method does not converge within the maximum number of iterations. + """ + x = x0 + for i in range(max_iter): + fx = f(x) + fpx = f_prime(x) + if fpx == 0: + raise ValueError("Derivative is zero. No solution found.") + x_new = x - fx / fpx + if abs(x_new - x) < tol: + return x_new + x = x_new + raise ValueError("Modified Newton-Raphson method did not converge") + +if __name__ == "__main__": + import math + + def f(x): + return x**3 - 2*x - 5 + + def f_prime(x): + return 3*x**2 - 2 + + root = modified_newton_raphson(f, f_prime, 2.0) + print(f"The root is: {root}") \ No newline at end of file From fbb023763a9516950e476126ca152de57c6e9717 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:13:06 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/numerical_analysis/fixed_point_iteration.py | 5 ++++- maths/numerical_analysis/modified_newton_raphson.py | 10 ++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index 309fcd7cdddc..2e46f13417cf 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -12,6 +12,7 @@ 1.618033988749895 """ + def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): """ Perform Fixed Point Iteration to find the root of the equation x = g(x). @@ -45,9 +46,11 @@ def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): x = x_new raise ValueError("Fixed Point Iteration did not converge") + if __name__ == "__main__": + def g(x): return (x**2 + 2) / 3 root = fixed_point_iteration(g, 1.0) - print(f"The root is: {root}") \ No newline at end of file + print(f"The root is: {root}") diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index 9aa22efc8121..5f49fee29885 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -18,12 +18,13 @@ from typing import Callable + def modified_newton_raphson( f: Callable[[float], float], f_prime: Callable[[float], float], x0: float, tol: float = 1e-7, - max_iter: int = 1000 + max_iter: int = 1000, ) -> float: """ Perform the Modified Newton-Raphson method to find the root of the equation f(x) = 0. @@ -63,14 +64,15 @@ def modified_newton_raphson( x = x_new raise ValueError("Modified Newton-Raphson method did not converge") + if __name__ == "__main__": import math def f(x): - return x**3 - 2*x - 5 + return x**3 - 2 * x - 5 def f_prime(x): - return 3*x**2 - 2 + return 3 * x**2 - 2 root = modified_newton_raphson(f, f_prime, 2.0) - print(f"The root is: {root}") \ No newline at end of file + print(f"The root is: {root}") From 8e03f899ffa51d78f21714ab1114370bd7f4b4b1 Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 14:45:25 +0530 Subject: [PATCH 04/11] Fixed Point Added --- .../modified_newton_raphson.py | 76 ------------------- 1 file changed, 76 deletions(-) delete mode 100644 maths/numerical_analysis/modified_newton_raphson.py diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py deleted file mode 100644 index 9aa22efc8121..000000000000 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ /dev/null @@ -1,76 +0,0 @@ -""" -Modified Newton-Raphson Method - -This method is used to find an approximate solution to a given equation f(x) = 0. -It is an iterative method that modifies the standard Newton-Raphson method to improve -convergence in certain cases, mostly when multiplicity is more than 1. - -Example: --------- ->>> import math ->>> def f(x): -... return x**3 - 2*x - 5 ->>> def f_prime(x): -... return 3*x**2 - 2 ->>> modified_newton_raphson(f, f_prime, 2.0) -2.0945514815423265 -""" - -from typing import Callable - -def modified_newton_raphson( - f: Callable[[float], float], - f_prime: Callable[[float], float], - x0: float, - tol: float = 1e-7, - max_iter: int = 1000 -) -> float: - """ - Perform the Modified Newton-Raphson method to find the root of the equation f(x) = 0. - - Parameters: - ----------- - f : function - The function for which we want to find the root. - f_prime : function - The derivative of the function f. - x0 : float - Initial guess for the root. - tol : float, optional - Tolerance for the convergence of the method. Default is 1e-7. - max_iter : int, optional - Maximum number of iterations. Default is 1000. - - Returns: - -------- - float - The approximate root of the equation f(x) = 0. - - Raises: - ------- - ValueError - If the method does not converge within the maximum number of iterations. - """ - x = x0 - for i in range(max_iter): - fx = f(x) - fpx = f_prime(x) - if fpx == 0: - raise ValueError("Derivative is zero. No solution found.") - x_new = x - fx / fpx - if abs(x_new - x) < tol: - return x_new - x = x_new - raise ValueError("Modified Newton-Raphson method did not converge") - -if __name__ == "__main__": - import math - - def f(x): - return x**3 - 2*x - 5 - - def f_prime(x): - return 3*x**2 - 2 - - root = modified_newton_raphson(f, f_prime, 2.0) - print(f"The root is: {root}") \ No newline at end of file From 6be480dea18e3ae0dbb67b01ad502b391dbd91bf Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 15:08:02 +0530 Subject: [PATCH 05/11] Fixed Point & Modified Newton Added --- .../fixed_point_iteration.py | 24 ++++++++++++------- .../modified_newton_raphson.py | 10 ++++---- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index 2e46f13417cf..f0bb4c6fe5c8 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -12,8 +12,14 @@ 1.618033988749895 """ - -def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): +from typing import Callable + +def fixed_point_iteration( + iteration_function: Callable[[float], float], + initial_guess: float, + tolerance: float = 1e-7, + max_iterations: int = 1000 +) -> float: """ Perform Fixed Point Iteration to find the root of the equation x = g(x). @@ -38,10 +44,10 @@ def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): ValueError If the method does not converge within the maximum number of iterations. """ - x = x0 - for i in range(max_iter): - x_new = g(x) - if abs(x_new - x) < tol: + x = initial_guess + for i in range(max_iterations): + x_new = iteration_function(x) + if abs(x_new - x) < tolerance: return x_new x = x_new raise ValueError("Fixed Point Iteration did not converge") @@ -49,8 +55,8 @@ def fixed_point_iteration(g, x0, tol=1e-7, max_iter=1000): if __name__ == "__main__": - def g(x): - return (x**2 + 2) / 3 + def quadratic_transform(value: float) -> float: + return (value**2 + 2) / 3 - root = fixed_point_iteration(g, 1.0) + root = fixed_point_iteration(quadratic_transform, 1.0) print(f"The root is: {root}") diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index 5f49fee29885..1bcaa0209c3e 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -68,11 +68,11 @@ def modified_newton_raphson( if __name__ == "__main__": import math - def f(x): - return x**3 - 2 * x - 5 + def polynomial_function(value: float) -> float: + return value**3 - 2 * value - 5 - def f_prime(x): - return 3 * x**2 - 2 + def f_prime(value: float) -> float: + return 3 * value**2 - 2 - root = modified_newton_raphson(f, f_prime, 2.0) + root = modified_newton_raphson(polynomial_function, f_prime, 2.0) print(f"The root is: {root}") From 88e6337fbb0e250c9232137ab01d14af8415e279 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:40:09 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/numerical_analysis/fixed_point_iteration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index f0bb4c6fe5c8..e7fc22539e2e 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -14,11 +14,12 @@ from typing import Callable + def fixed_point_iteration( iteration_function: Callable[[float], float], initial_guess: float, - tolerance: float = 1e-7, - max_iterations: int = 1000 + tolerance: float = 1e-7, + max_iterations: int = 1000, ) -> float: """ Perform Fixed Point Iteration to find the root of the equation x = g(x). From 3fbb0ead7669cc564ec95dd61612af7fab9aea4b Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 15:19:06 +0530 Subject: [PATCH 07/11] Fixed Point & Modified Newton Added --- .../fixed_point_iteration.py | 22 ++++---- .../modified_newton_raphson.py | 51 ++++++++++--------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index f0bb4c6fe5c8..786e48830780 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -25,19 +25,19 @@ def fixed_point_iteration( Parameters: ----------- - g : function - The function g(x) derived from f(x) such that x = g(x). - x0 : float + iteration_function : Callable[[float], float] + The function derived from f(x) such that x = g(x). + initial_guess : float Initial guess for the root. - tol : float, optional - Tolerance for the convergence of the method. Default is 1e-7. - max_iter : int, optional + tolerance : float, optional + Tolerance for convergence. Default is 1e-7. + max_iterations : int, optional Maximum number of iterations. Default is 1000. Returns: -------- float - The approximate root of the equation x = g(x). + The approximate root of the equation. Raises: ------- @@ -45,7 +45,7 @@ def fixed_point_iteration( If the method does not converge within the maximum number of iterations. """ x = initial_guess - for i in range(max_iterations): + for _ in range(max_iterations): x_new = iteration_function(x) if abs(x_new - x) < tolerance: return x_new @@ -56,7 +56,11 @@ def fixed_point_iteration( if __name__ == "__main__": def quadratic_transform(value: float) -> float: + """Quadratic transformation function for iteration.""" return (value**2 + 2) / 3 - root = fixed_point_iteration(quadratic_transform, 1.0) + root = fixed_point_iteration( + iteration_function=quadratic_transform, + initial_guess=1.0 + ) print(f"The root is: {root}") diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index 1bcaa0209c3e..6ba705cf5b2d 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -20,59 +20,64 @@ def modified_newton_raphson( - f: Callable[[float], float], - f_prime: Callable[[float], float], - x0: float, - tol: float = 1e-7, - max_iter: int = 1000, + function: Callable[[float], float], + derivative_function: Callable[[float], float], + initial_guess: float, + tolerance: float = 1e-7, + max_iterations: int = 1000 ) -> float: """ - Perform the Modified Newton-Raphson method to find the root of the equation f(x) = 0. + Perform the Modified Newton-Raphson method to find the root of f(x) = 0. Parameters: ----------- - f : function - The function for which we want to find the root. - f_prime : function - The derivative of the function f. - x0 : float + function : Callable[[float], float] + The function for which the root is sought. + derivative_function : Callable[[float], float] + The derivative of the function. + initial_guess : float Initial guess for the root. - tol : float, optional - Tolerance for the convergence of the method. Default is 1e-7. - max_iter : int, optional + tolerance : float, optional + Tolerance for convergence. Default is 1e-7. + max_iterations : int, optional Maximum number of iterations. Default is 1000. Returns: -------- float - The approximate root of the equation f(x) = 0. + The approximate root of the equation. Raises: ------- ValueError If the method does not converge within the maximum number of iterations. """ - x = x0 - for i in range(max_iter): - fx = f(x) - fpx = f_prime(x) + x = initial_guess + for _ in range(max_iterations): + fx = function(x) + fpx = derivative_function(x) if fpx == 0: raise ValueError("Derivative is zero. No solution found.") x_new = x - fx / fpx - if abs(x_new - x) < tol: + if abs(x_new - x) < tolerance: return x_new x = x_new raise ValueError("Modified Newton-Raphson method did not converge") if __name__ == "__main__": - import math def polynomial_function(value: float) -> float: + """Polynomial function whose root is to be found.""" return value**3 - 2 * value - 5 - def f_prime(value: float) -> float: + def derivative_polynomial_function(value: float) -> float: + """Derivative of the polynomial function.""" return 3 * value**2 - 2 - root = modified_newton_raphson(polynomial_function, f_prime, 2.0) + root = modified_newton_raphson( + function=polynomial_function, + derivative_function=derivative_polynomial_function, + initial_guess=2.0 + ) print(f"The root is: {root}") From e0936a2f84a17512bc5d77d2362c06c890d2691e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:50:37 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/numerical_analysis/fixed_point_iteration.py | 3 +-- maths/numerical_analysis/modified_newton_raphson.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index ef9bffa7cc0a..2857b38cbaed 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -61,7 +61,6 @@ def quadratic_transform(value: float) -> float: return (value**2 + 2) / 3 root = fixed_point_iteration( - iteration_function=quadratic_transform, - initial_guess=1.0 + iteration_function=quadratic_transform, initial_guess=1.0 ) print(f"The root is: {root}") diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index 6ba705cf5b2d..86f8a54ade1b 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -24,7 +24,7 @@ def modified_newton_raphson( derivative_function: Callable[[float], float], initial_guess: float, tolerance: float = 1e-7, - max_iterations: int = 1000 + max_iterations: int = 1000, ) -> float: """ Perform the Modified Newton-Raphson method to find the root of f(x) = 0. @@ -78,6 +78,6 @@ def derivative_polynomial_function(value: float) -> float: root = modified_newton_raphson( function=polynomial_function, derivative_function=derivative_polynomial_function, - initial_guess=2.0 + initial_guess=2.0, ) print(f"The root is: {root}") From 75edb0c184adbc3e2eaab32dad383460a0d862a7 Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 15:55:58 +0530 Subject: [PATCH 09/11] Fixed Point & Modified Newton Added --- .../fixed_point_iteration.py | 29 +++--------- .../modified_newton_raphson.py | 45 +++++-------------- 2 files changed, 16 insertions(+), 58 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index ef9bffa7cc0a..7afbd1386a56 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -1,25 +1,10 @@ -""" -Fixed Point Iteration Method - -This method is used to find an approximate solution to a given equation f(x) = 0. -The function g(x) is derived from f(x) such that x = g(x). - -Example: --------- ->>> def g(x): -... return (x**2 + 2) / 3 ->>> fixed_point_iteration(g, 1.0) -1.618033988749895 -""" - from typing import Callable - def fixed_point_iteration( iteration_function: Callable[[float], float], initial_guess: float, tolerance: float = 1e-7, - max_iterations: int = 1000, + max_iterations: int = 1000 ) -> float: """ Perform Fixed Point Iteration to find the root of the equation x = g(x). @@ -53,15 +38,11 @@ def fixed_point_iteration( x = x_new raise ValueError("Fixed Point Iteration did not converge") - if __name__ == "__main__": - def quadratic_transform(value: float) -> float: + def quadratic_transform(current_value: float) -> float: """Quadratic transformation function for iteration.""" - return (value**2 + 2) / 3 + return (current_value**2 + 2) / 3 - root = fixed_point_iteration( - iteration_function=quadratic_transform, - initial_guess=1.0 - ) - print(f"The root is: {root}") + root = fixed_point_iteration(quadratic_transform, initial_guess=1.0) + print(f"Approximate root: {root}") \ No newline at end of file diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index 6ba705cf5b2d..f0e5a321ac47 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -1,24 +1,5 @@ -""" -Modified Newton-Raphson Method - -This method is used to find an approximate solution to a given equation f(x) = 0. -It is an iterative method that modifies the standard Newton-Raphson method to improve -convergence in certain cases, mostly when multiplicity is more than 1. - -Example: --------- ->>> import math ->>> def f(x): -... return x**3 - 2*x - 5 ->>> def f_prime(x): -... return 3*x**2 - 2 ->>> modified_newton_raphson(f, f_prime, 2.0) -2.0945514815423265 -""" - from typing import Callable - def modified_newton_raphson( function: Callable[[float], float], derivative_function: Callable[[float], float], @@ -49,6 +30,8 @@ def modified_newton_raphson( Raises: ------- + ZeroDivisionError + If the derivative is zero. ValueError If the method does not converge within the maximum number of iterations. """ @@ -57,27 +40,21 @@ def modified_newton_raphson( fx = function(x) fpx = derivative_function(x) if fpx == 0: - raise ValueError("Derivative is zero. No solution found.") + raise ZeroDivisionError("Derivative is zero. No solution found.") x_new = x - fx / fpx if abs(x_new - x) < tolerance: return x_new x = x_new - raise ValueError("Modified Newton-Raphson method did not converge") - + raise ValueError("Modified Newton-Raphson did not converge") if __name__ == "__main__": + import math - def polynomial_function(value: float) -> float: - """Polynomial function whose root is to be found.""" - return value**3 - 2 * value - 5 + def compute_function(current_x: float) -> float: + return current_x**3 - 2 * current_x - 5 - def derivative_polynomial_function(value: float) -> float: - """Derivative of the polynomial function.""" - return 3 * value**2 - 2 + def compute_derivative(current_x: float) -> float: + return 3 * current_x**2 - 2 - root = modified_newton_raphson( - function=polynomial_function, - derivative_function=derivative_polynomial_function, - initial_guess=2.0 - ) - print(f"The root is: {root}") + root = modified_newton_raphson(compute_function, compute_derivative, initial_guess=2.0) + print(f"Approximate root: {root}") \ No newline at end of file From 42d68d9aaadaa2569c415db3e1ba27566c4335df Mon Sep 17 00:00:00 2001 From: Akshat Gupta Date: Thu, 31 Oct 2024 15:57:51 +0530 Subject: [PATCH 10/11] Fixed Point & Modified Newton Added --- maths/numerical_analysis/fixed_point_iteration.py | 7 ++----- maths/numerical_analysis/modified_newton_raphson.py | 10 +++------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index bd69b54de1a0..7afbd1386a56 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -44,8 +44,5 @@ def quadratic_transform(current_value: float) -> float: """Quadratic transformation function for iteration.""" return (current_value**2 + 2) / 3 - root = fixed_point_iteration( - iteration_function=quadratic_transform, - initial_guess=1.0 - ) - print(f"The root is: {root}") + root = fixed_point_iteration(quadratic_transform, initial_guess=1.0) + print(f"Approximate root: {root}") \ No newline at end of file diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index fa9335efb9e6..f0e5a321ac47 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -5,7 +5,7 @@ def modified_newton_raphson( derivative_function: Callable[[float], float], initial_guess: float, tolerance: float = 1e-7, - max_iterations: int = 1000, + max_iterations: int = 1000 ) -> float: """ Perform the Modified Newton-Raphson method to find the root of f(x) = 0. @@ -56,9 +56,5 @@ def compute_function(current_x: float) -> float: def compute_derivative(current_x: float) -> float: return 3 * current_x**2 - 2 - root = modified_newton_raphson( - function=polynomial_function, - derivative_function=derivative_polynomial_function, - initial_guess=2.0 - ) - print(f"The root is: {root}") + root = modified_newton_raphson(compute_function, compute_derivative, initial_guess=2.0) + print(f"Approximate root: {root}") \ No newline at end of file From c61501115e9b105d4b6ae0f71ba0505cb2a0b746 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:28:45 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/numerical_analysis/fixed_point_iteration.py | 6 ++++-- maths/numerical_analysis/modified_newton_raphson.py | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/maths/numerical_analysis/fixed_point_iteration.py b/maths/numerical_analysis/fixed_point_iteration.py index 7afbd1386a56..06317afe9072 100644 --- a/maths/numerical_analysis/fixed_point_iteration.py +++ b/maths/numerical_analysis/fixed_point_iteration.py @@ -1,10 +1,11 @@ from typing import Callable + def fixed_point_iteration( iteration_function: Callable[[float], float], initial_guess: float, tolerance: float = 1e-7, - max_iterations: int = 1000 + max_iterations: int = 1000, ) -> float: """ Perform Fixed Point Iteration to find the root of the equation x = g(x). @@ -38,6 +39,7 @@ def fixed_point_iteration( x = x_new raise ValueError("Fixed Point Iteration did not converge") + if __name__ == "__main__": def quadratic_transform(current_value: float) -> float: @@ -45,4 +47,4 @@ def quadratic_transform(current_value: float) -> float: return (current_value**2 + 2) / 3 root = fixed_point_iteration(quadratic_transform, initial_guess=1.0) - print(f"Approximate root: {root}") \ No newline at end of file + print(f"Approximate root: {root}") diff --git a/maths/numerical_analysis/modified_newton_raphson.py b/maths/numerical_analysis/modified_newton_raphson.py index f0e5a321ac47..8cf5cccec860 100644 --- a/maths/numerical_analysis/modified_newton_raphson.py +++ b/maths/numerical_analysis/modified_newton_raphson.py @@ -1,11 +1,12 @@ from typing import Callable + def modified_newton_raphson( function: Callable[[float], float], derivative_function: Callable[[float], float], initial_guess: float, tolerance: float = 1e-7, - max_iterations: int = 1000 + max_iterations: int = 1000, ) -> float: """ Perform the Modified Newton-Raphson method to find the root of f(x) = 0. @@ -47,6 +48,7 @@ def modified_newton_raphson( x = x_new raise ValueError("Modified Newton-Raphson did not converge") + if __name__ == "__main__": import math @@ -56,5 +58,7 @@ def compute_function(current_x: float) -> float: def compute_derivative(current_x: float) -> float: return 3 * current_x**2 - 2 - root = modified_newton_raphson(compute_function, compute_derivative, initial_guess=2.0) - print(f"Approximate root: {root}") \ No newline at end of file + root = modified_newton_raphson( + compute_function, compute_derivative, initial_guess=2.0 + ) + print(f"Approximate root: {root}")