Programming in python with numpy and sympy, and my attempts to use derivatives in my code are falling flat. I frequently get either
"TypeError: 'Add' object is not callable"
and,
"ValueError: First variable cannot be a number: 1".
This is for a program meant to define Newton's Method for solving a root-finding problem. The sample equation I've used is 1/x+log(x)-2. I mention this because I've had a few issues with numpy's log function, as well. I think my problem has to do with the diff I'm using, as I'm not entirely certain how to use it to return an actual value, and the literature I've read on it isn't incredibly helpful.
def newton(p0, f, n, t):
global p
p = 0
for i in range(1, n+1):
p = p0 - f(p0)/diff(f(x),p0)
if abs(p-p0) < t:
return p
p0 = p
i = i + 1
return f"The method failed after {n} iterations. The procedure was unsuccessful."
print(newton(p0=1, f=1/x+log(x)-2, n=10, t=5e-324))
I'm at least expecting a number, but I'm getting the errors I describe above.