from sympy import *
x = Symbol('x')
f = (2*x) + 1
fprime = f.diff()
f = lambdify(x, f)
fprime = lambdify(x, fprime)
newtons_expr = lambdify(x, x - (f(x) / fprime(x)) )
def newtons_method(guess):
approx = newtons_expr(guess)
if Abs(approx - guess) < 0.001:
print(approx) # <-- returns a float (-0.5)
print(type(approx)) # <-- returns "<class 'float'>"
return approx # <-- returns a None
else:
newtons_method(approx)
print(newtons_method(1))
I built this function using SymPy to evaluate one of the solutions to an equation using Newton's method. For some reason, however, its returning a None
instead of a float
, even though it explicitly prints a float before it returns the value of approx
. What's wrong here? I've never seen this before.