diff --git a/derive_and_min-max.py b/derive_and_min-max.py index ec74886aff864d4f52401d3e8d6859147ade56e0..b9a58a00d93b994583d1cebf1e7b594e828b4870 100644 --- a/derive_and_min-max.py +++ b/derive_and_min-max.py @@ -3,20 +3,20 @@ from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \ from Dual_Numbers import Dual_Number -def f(x: Dual_Number): +def f(x: Dual_Number) -> float: return ((x * x - 1) * Dual_Number.exp(x - 1)).d -def g(x: Dual_Number): +def g(x: Dual_Number) -> float: return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) * Dual_Number.cos(5 - x)).d -def dfdx(x: float): +def dfdx(x: float) -> float: return f(Dual_Number(x, 1)) -def dgdx(x: float): +def dgdx(x: float) -> float: return g(Dual_Number(x, 1)) diff --git a/rf_methods.py b/rf_methods.py index bd373e2499fb418a0a07b4e125a6692343f54953..3fdf34e258168d1d86cec149399fea66ea46b4ee 100644 --- a/rf_methods.py +++ b/rf_methods.py @@ -1,4 +1,5 @@ import numpy as np +from typing import Callable def f(x: float) -> float: @@ -17,7 +18,8 @@ def z(x: float) -> float: return x ** 3 - x ** 2 - 1 -def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float: +def rec_regula_falsi(start: float, stop: float, func: Callable[[float], float], + debug: bool) -> float: cN = (start * func(stop) - stop * func(start)) / (func(stop) - func(start)) if np.sign(func(cN)) != np.sign(func(start)): @@ -31,13 +33,14 @@ def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float: # Testing the y-axis if np.abs(func(cN)) < 1e-9: return cN + return rec_regula_falsi(start, stop, func, debug) else: return cN -def iter_regula_falsi(init_start: float, init_stop: float, func, debug: bool) \ - -> float: +def iter_regula_falsi(init_start: float, init_stop: float, func: + Callable[[float], float], debug: bool) -> float: cN = (init_start * func(init_stop) - init_stop * func(init_start)) / (func(init_stop) - func(init_start)) start = init_start @@ -48,18 +51,22 @@ def iter_regula_falsi(init_start: float, init_stop: float, func, debug: bool) \ while np.abs(func(stop) - func(start)) > 1e-9 or np.abs(func(cN)) > 1e-9: if debug: print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") + cN = (start * func(stop) - stop * func(start)) / \ (func(stop) - func(start)) + if np.sign(func(cN)) != np.sign(func(start)): stop = cN elif np.sign(func(cN)) != np.sign(func(stop)): start = cN + iter += 1 return cN -def rec_bisection(start: float, stop: float, func, debug: bool) -> float: +def rec_bisection(start: float, stop: float, func: Callable[[float], float], + debug: bool) -> float: cN = (stop + start) / 2 if np.sign(func(cN)) != np.sign(func(start)): @@ -70,13 +77,14 @@ def rec_bisection(start: float, stop: float, func, debug: bool) -> float: if np.abs(stop - start) > 1e-9: if debug: print(f"x = {cN}\tf(x) = {func(cN)}") + return rec_bisection(start, stop, func, debug) else: return cN -def iter_bisection(init_start: float, init_stop: float, func, debug: bool) -> \ - float: +def iter_bisection(init_start: float, init_stop: float, func: + Callable[[float], float], debug: bool) -> float: cN = (init_stop + init_start) / 2 start = init_start stop = init_stop @@ -86,7 +94,9 @@ def iter_bisection(init_start: float, init_stop: float, func, debug: bool) -> \ while np.abs(stop - start) > 1e-9: if debug: print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") + cN = (stop + start) / 2 + if np.sign(func(cN)) != np.sign(func(start)): stop = cN elif np.sign(func(cN)) != np.sign(func(stop)):