File tree 2 files changed +11
-5
lines changed
2 files changed +11
-5
lines changed Original file line number Diff line number Diff line change 1
1
from typing import Callable
2
2
3
+
3
4
def fixed_point_iteration (
4
5
iteration_function : Callable [[float ], float ],
5
6
initial_guess : float ,
6
7
tolerance : float = 1e-7 ,
7
- max_iterations : int = 1000
8
+ max_iterations : int = 1000 ,
8
9
) -> float :
9
10
"""
10
11
Perform Fixed Point Iteration to find the root of the equation x = g(x).
@@ -38,11 +39,12 @@ def fixed_point_iteration(
38
39
x = x_new
39
40
raise ValueError ("Fixed Point Iteration did not converge" )
40
41
42
+
41
43
if __name__ == "__main__" :
42
44
43
45
def quadratic_transform (current_value : float ) -> float :
44
46
"""Quadratic transformation function for iteration."""
45
47
return (current_value ** 2 + 2 ) / 3
46
48
47
49
root = fixed_point_iteration (quadratic_transform , initial_guess = 1.0 )
48
- print (f"Approximate root: { root } " )
50
+ print (f"Approximate root: { root } " )
Original file line number Diff line number Diff line change 1
1
from typing import Callable
2
2
3
+
3
4
def modified_newton_raphson (
4
5
function : Callable [[float ], float ],
5
6
derivative_function : Callable [[float ], float ],
6
7
initial_guess : float ,
7
8
tolerance : float = 1e-7 ,
8
- max_iterations : int = 1000
9
+ max_iterations : int = 1000 ,
9
10
) -> float :
10
11
"""
11
12
Perform the Modified Newton-Raphson method to find the root of f(x) = 0.
@@ -47,6 +48,7 @@ def modified_newton_raphson(
47
48
x = x_new
48
49
raise ValueError ("Modified Newton-Raphson did not converge" )
49
50
51
+
50
52
if __name__ == "__main__" :
51
53
import math
52
54
@@ -56,5 +58,7 @@ def compute_function(current_x: float) -> float:
56
58
def compute_derivative (current_x : float ) -> float :
57
59
return 3 * current_x ** 2 - 2
58
60
59
- root = modified_newton_raphson (compute_function , compute_derivative , initial_guess = 2.0 )
60
- print (f"Approximate root: { root } " )
61
+ root = modified_newton_raphson (
62
+ compute_function , compute_derivative , initial_guess = 2.0
63
+ )
64
+ print (f"Approximate root: { root } " )
You can’t perform that action at this time.
0 commit comments