-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add Fixed Point Iteration and Modified Newton-Raphson Methods #12341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b55043a
9b45691
fbb0237
8e03f89
2eee3fd
6be480d
88e6337
3fbb0ea
8795283
e0936a2
75edb0c
d577698
42d68d9
c615011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
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}") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
fixed_point_iteration
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide type hint for the parameter:
g
Please provide descriptive name for the parameter:
g
Please provide type hint for the parameter:
x0
Please provide type hint for the parameter:
tol
Please provide type hint for the parameter:
max_iter