Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions maths/numerical_analysis/fixed_point_iteration.py
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):

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

"""
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):

Check failure on line 42 in maths/numerical_analysis/fixed_point_iteration.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

maths/numerical_analysis/fixed_point_iteration.py:42:9: B007 Loop control variable `i` not used within loop body
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):

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: g. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the function: g

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

return (x**2 + 2) / 3

root = fixed_point_iteration(g, 1.0)
print(f"The root is: {root}")
78 changes: 78 additions & 0 deletions maths/numerical_analysis/modified_newton_raphson.py
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

Check failure on line 19 in maths/numerical_analysis/modified_newton_raphson.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

maths/numerical_analysis/modified_newton_raphson.py:19:1: UP035 Import from `collections.abc` instead: `Callable`


def modified_newton_raphson(
f: Callable[[float], float],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: f

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.

Check failure on line 30 in maths/numerical_analysis/modified_newton_raphson.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/numerical_analysis/modified_newton_raphson.py:30:89: E501 Line too long (89 > 88)

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):

Check failure on line 56 in maths/numerical_analysis/modified_newton_raphson.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

maths/numerical_analysis/modified_newton_raphson.py:56:9: B007 Loop control variable `i` not used within loop body
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

Check failure on line 69 in maths/numerical_analysis/modified_newton_raphson.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

maths/numerical_analysis/modified_newton_raphson.py:69:12: F401 `math` imported but unused

def f(x):

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: f. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the function: f

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

return x**3 - 2 * x - 5

def f_prime(x):

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: f_prime. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

return 3 * x**2 - 2

root = modified_newton_raphson(f, f_prime, 2.0)
print(f"The root is: {root}")
Loading