Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

CJ Meyer 19007361

🏠
Working from home
  • Stellenbosch, South Africa
View GitHub Profile
View secant.py (practise)
def secant(p, pOld, tol, iters):
iters -= 1
pOld = p
pNew = p - (f(p))*(p - pOld)
if abs(pNew - p) < tol or iters == 0:
return pNew
else:
secant(pNew, pOld, tol)
def f(x):
View gist:476400ca95fb53be6b90d1ec79b67c05
def secant(f, a, b, tol=1e-9, n=100):
fmt = "{:2} {:>13.10f} {:>13.10f} {:>13.10f}"
p1, p2 = float(a), float(b)
i = 1
print fmt.format(1, p1, p2, f(p2))
while abs(p1 - p2) > tol and i <= n:
p1, p2 = p2, p2 - (f(p2) * (p2 - p1)) / (f(p2) - f(p1))
i += 1
print fmt.format(i, p1, p2, f(p2))
return p2