Skip to content
Snippets Groups Projects
Commit 7274bdda authored by iliya's avatar iliya
Browse files

feat: added iterative solutions for both root-finding methods

parent d45c7554
No related branches found
No related tags found
No related merge requests found
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)}")
......@@ -17,7 +17,7 @@ def z(x: float) -> float:
return x ** 3 - x ** 2 - 1
def regula_falsi(start: float, stop: float, func) -> float:
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)):
......@@ -26,16 +26,40 @@ def regula_falsi(start: float, stop: float, func) -> float:
start = cN
if np.abs(func(stop) - func(start)) > 1e-9:
print(f"Intermediate value of Cn = {cN}\tf(Cn) = {func(cN)}")
if debug:
print(f"x = {cN}\tf(x) = {func(cN)}")
# Testing the y-axis
if np.abs(func(cN)) < 1e-9:
return cN
return regula_falsi(start, stop, func)
return rec_regula_falsi(start, stop, func, debug)
else:
return cN
def bissect_method(start: float, stop: float, func) -> float:
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)):
......@@ -44,14 +68,41 @@ def bissect_method(start: float, stop: float, func) -> float:
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)
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 = {regula_falsi(0, 3, z)}")
# 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)}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment