diff --git a/derive_and_min-max.py b/derive_and_min-max.py index 8a55be49e1018efb97d83dccadd11ee7a95b00c7..890f063f7dd25d1e7c01fb8d3c128a211735c5d2 100644 --- a/derive_and_min-max.py +++ b/derive_and_min-max.py @@ -1,4 +1,5 @@ -from zeroes_func import bissect_method, regula_falsi +from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \ + iter_regula_falsi from Dual_Numbers import Dual_Number @@ -11,5 +12,14 @@ def dfdx(x: float): if __name__ == "__main__": - print(f"Bissect method = {bissect_method(-6, -1, dfdx)}") - print(f"Regula falsi = {regula_falsi(-6, -1, dfdx)}") + start = -6 + stop = -1 + + print("----------------------------------------------") + print(f"Rec bisection = {rec_bisection(start, stop, dfdx, False)}") + print("----------------------------------------------") + print(f"Rec regula falsi = {rec_regula_falsi(start, stop, dfdx, False)}") + print("----------------------------------------------") + print(f"Iter bisection = {iter_bisection(start, stop, dfdx, False)}") + print("----------------------------------------------") + print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dfdx, False)}") diff --git a/rf_methods.py b/rf_methods.py new file mode 100644 index 0000000000000000000000000000000000000000..bd373e2499fb418a0a07b4e125a6692343f54953 --- /dev/null +++ b/rf_methods.py @@ -0,0 +1,108 @@ +import numpy as np + + +def f(x: float) -> float: + return x ** 4 + x ** 3 + x ** 2 - 1 + + +def g(x: float) -> float: + return x ** 2 - x - 1 + + +def h(x: float) -> float: + return x ** 2 - 25 + + +def z(x: float) -> float: + return x ** 3 - x ** 2 - 1 + + +def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float: + 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 + + if np.abs(func(stop) - func(start)) > 1e-9: + if debug: + print(f"x = {cN}\tf(x) = {func(cN)}") + # 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: + cN = (init_start * func(init_stop) - init_stop * + func(init_start)) / (func(init_stop) - func(init_start)) + start = init_start + stop = init_stop + + iter = 0 + + 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: + cN = (stop + start) / 2 + + if np.sign(func(cN)) != np.sign(func(start)): + stop = cN + elif np.sign(func(cN)) != np.sign(func(stop)): + start = cN + + 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: + cN = (init_stop + init_start) / 2 + start = init_start + stop = init_stop + + iter = 0 + + 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)): + start = cN + + iter += 1 + + return cN + + +if __name__ == "__main__": + # print(f"Bissection = {bissect_method(0.5, 2, g)}") + # print(f"Regula falsi = {regula_falsi(-3, 7, h)}") + # print(f"Bissection = {bissect_method(-3, 7, h)}") + # print(f"Regula falsi = {rec_regula_falsi(0, 3, z)}") + print(f"Iter bisection= {iter_bisection(0.5, 2, g, False)}") + print(f"Rec bisection= {rec_bisection(0.5, 2, g, False)}") + print(f"Iter regula falsi= {iter_regula_falsi(0.5, 2, g, False)}") + print(f"Rec regula falsi = {rec_regula_falsi(0.5, 2, g, False)}") diff --git a/zeroes_func.py b/zeroes_func.py deleted file mode 100644 index a02327828884691711fb9b69b4bfdb78209e3362..0000000000000000000000000000000000000000 --- a/zeroes_func.py +++ /dev/null @@ -1,57 +0,0 @@ -import numpy as np - - -def f(x: float) -> float: - return x ** 4 + x ** 3 + x ** 2 - 1 - - -def g(x: float) -> float: - return x ** 2 - x - 1 - - -def h(x: float) -> float: - return x ** 2 - 25 - - -def z(x: float) -> float: - return x ** 3 - x ** 2 - 1 - - -def regula_falsi(start: float, stop: float, func) -> float: - 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 - - if np.abs(func(stop) - func(start)) > 1e-9: - print(f"Intermediate value of Cn = {cN}\tf(Cn) = {func(cN)}") - # Testing the y-axis - if np.abs(func(cN)) < 1e-9: - return cN - return regula_falsi(start, stop, func) - else: - return cN - - -def bissect_method(start: float, stop: float, func) -> float: - cN = (stop + start) / 2 - - if np.sign(func(cN)) != np.sign(func(start)): - stop = cN - elif np.sign(func(cN)) != np.sign(func(stop)): - start = cN - - if np.abs(stop - start) > 1e-9: - print(f"Intermediate value of Cn = {cN}\tf(Cn) = {func(cN)}") - return bissect_method(start, stop, func) - else: - return cN - - -if __name__ == "__main__": - # print(f"Bissection = {bissect_method(0.5, 2, g)}") - # print(f"Regula falsi = {regula_falsi(-3, 7, h)}") - # print(f"Bissection = {bissect_method(-3, 7, h)}") - print(f"Regula falsi = {regula_falsi(0, 3, z)}")