Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions python/nonlinear_equations/bisection_medthod.py
Original file line number Diff line number Diff line change
@@ -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

41 changes: 41 additions & 0 deletions python/nonlinear_equations/false_position_method.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions python/nonlinear_equations/newton-raphson_method.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions python/nonlinear_equations/secant_method.py
Original file line number Diff line number Diff line change
@@ -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