Im currently learning about numerical methods in my numerical analysis course and wanted to program Secant Method for finding roots of an equation in python
I checked different sources for that (for example) and found really helpful codes on this method. i even have one on my own with output on a given function.
import numpy as np
def f(x):
f = pow(x, 3) - pow(x,2)-1
return f
def secant(x1, x2, E):
n = 0
xm = 0
x0 = 0
c = 0
if (f(x1) * f(x2) < 0):
while True:
x0 = ((x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)))
c = f(x1) * f(x0)
x1 = x2
x2 = x0
n += 1
if (c == 0):
break
xm = ((x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)))
if (np.abs((xm - x0)/x0) < E):
break
print("Root of the given equation =", np.round(x0, 6))
print("No. of iterations = ", n)
else:
print("Can not find a root in the given inteval")
x1 = 1
x2 = 2
E = 0.0001
secant(x1, x2, E)
then I wanted to create such table for each iteration
(this is for bisection method not secant)
but i failed to do so.
i would really appreciate if someone can help me to do that. even a hint would be appriciated.