diff --git a/README.md b/README.md index fb460cc..a34bd7a 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,10 @@ | | Matlab | Python | C++ | | --- | --- | --- | --- | -| Bisection method| :octocat: | | | -| Newton-Raphson method | :octocat: | | | -| Secant method | :octocat: | | | +| Bisection method| :octocat: | :octocat: | | +| Newton-Raphson method | :octocat: | :octocat: | | +| Secant method | :octocat: | :octocat: | | +| False Position method | | :octocat: | | #### Gaussian methods diff --git a/python/nonlinear_equations/bisection_medthod.py b/python/nonlinear_equations/bisection_medthod.py new file mode 100644 index 0000000..ff0d95d --- /dev/null +++ b/python/nonlinear_equations/bisection_medthod.py @@ -0,0 +1,40 @@ +# Bisection Method + +from __future__ import division, print_function +from numpy import abs, cos, sin, exp, sqrt, array, append, ceil, log2, arange, linspace +import matplotlib.pyplot as plt + +def bisection(fun, a, b, c, valerr=100, xtol=1e-6, ftol=1e-06, verbose=False): +# def bisection(fun, a, b, c, valerr=100, nmax=5000, ftol=1e-06, verbose=False): + if fun(a) * fun(b) > 0: + c = None + msg = "The function should have a sign change in the interval." + else: + nmax = int(ceil(log2((b - a)/xtol))) + ax = array([]) + acont = array([]) + avalerr = array([]) + for cont in range(nmax): + + ax = append(ax,c) + acont = append(acont,cont) + avalerr = append(avalerr,valerr) + + if verbose: + print("n: {}, \tx: {}, \terr: {}%".format(cont, c,valerr)) + + valp = c + c = 0.5*(a + b) + valf = c + valerr = abs((valf - valp)/valf) * 100 + + if abs(fun(c)) < ftol: + msg = "Root found with desired accuracy." + break + elif fun(a) * fun(c) < 0: + b = c + elif fun(b) * fun(c) < 0: + a = c + msg = "Maximum number of iterations reached." + return ax, avalerr, acont, msg + \ No newline at end of file diff --git a/python/nonlinear_equations/false_position_method.py b/python/nonlinear_equations/false_position_method.py new file mode 100644 index 0000000..6f4a5ab --- /dev/null +++ b/python/nonlinear_equations/false_position_method.py @@ -0,0 +1,41 @@ +# False Position Method + +from __future__ import division, print_function +from numpy import abs, cos, sin, exp, sqrt, array, append, ceil, log2, arange, linspace +import matplotlib.pyplot as plt + +def regula_falsi(fun, a, b, c, valerr=100, niter=5000, ftol=1e-06, verbose=False): + if fun(a) * fun(b) > 0: + c = None + msg = "The function should have a sign change in the interval." + else: + ax = array([]) + acont = array([]) + avalerr = array([]) + for cont in range(niter): + + ax = append(ax,c) + acont = append(acont,cont) + avalerr = append(avalerr,valerr) + + qa = fun(a) + qb = fun(b) + valp = c + c = (a*qb - b*qa)/(qb - qa) + qc = fun(c) + valf = c + valerr = abs((valf - valp)/valf) * 100 + + if verbose: + print("n: {}, \tx: {}, \terr: {}%".format(cont, c,valerr)) + + msg = "Maximum number of iterations reached." + + if abs(qc) < ftol: + msg = "Root found with desired accuracy." + break + elif qa * qc < 0: + b = c + elif qb * qc < 0: + a = c + return ax, avalerr, acont, msg \ No newline at end of file diff --git a/python/nonlinear_equations/newton-raphson_method.py b/python/nonlinear_equations/newton-raphson_method.py new file mode 100644 index 0000000..25cb3de --- /dev/null +++ b/python/nonlinear_equations/newton-raphson_method.py @@ -0,0 +1,33 @@ +# Newton-Raphson Method + +from __future__ import division, print_function +from numpy import abs, cos, sin, exp, sqrt, array, append, ceil, log2, arange, linspace +import matplotlib.pyplot as plt + +def newton(fun, grad, x, valerr=100, niter=5000, ftol=1e-06, verbose=False): + msg = "Maximum number of iterations reached." + ax = array([]) + acont = array([]) + avalerr = array([]) + for cont in range(niter): + if abs(grad(x)) < ftol: + x = None + msg = "Derivative near to zero." + break + + if verbose: + print("n: {}, \tx: {}, \terr: {}%".format(cont, x,valerr)) + + ax = append(ax,x) + acont = append(acont,cont) + avalerr = append(avalerr,valerr) + + valp = x + x = x - fun(x)/grad(x) + valf = x + valerr = abs((valf - valp)/valf) * 100 + + if abs(fun(x)) < ftol: + msg = "Root found with desired accuracy." + break + return ax, avalerr, acont, msg \ No newline at end of file diff --git a/python/nonlinear_equations/secant_method.py b/python/nonlinear_equations/secant_method.py new file mode 100644 index 0000000..411a009 --- /dev/null +++ b/python/nonlinear_equations/secant_method.py @@ -0,0 +1,29 @@ +# Secant Method + +from __future__ import division, print_function +from numpy import abs, cos, sin, exp, sqrt, array, append, ceil, log2, arange, linspace +import matplotlib.pyplot as plt + +def secant(fun, xp, x, valerr=100, niter=5000, ftol=1e-06, verbose=False): + msg = "Maximum number of iterations reached." + ax = array([]) + acont = array([]) + avalerr = array([]) + for cont in range(niter): + + if verbose: + print("n: {}, \tx: {}, \terr: {}%".format(cont, x,valerr)) + + ax = append(ax,x) + acont = append(acont,cont) + avalerr = append(avalerr,valerr) + + valp = x + x = x - ((x - xp)/(fun(x)-fun(xp)))*fun(x) + valf = x + valerr = abs((valf - valp)/valf) * 100 + + if abs(fun(x)) < ftol: + msg = "Root found with desired accuracy." + break + return ax, avalerr, acont, msg \ No newline at end of file