Solve the equation by the method of tangents (Newton's method). Gives an error message

TypeError: can't convert complex to float

in the line x = x_prev - (func(x_prev)/diff(func(x_prev)))

Here is the code:

 from sympy import diff def func(x): return (math.exp(x)) + (x ** 3) - 2 def perform_tangent_method(a, b, eps): '''Equation solution by tangent(Newton) method''' x_prev = b while True: try: x = x_prev - (func(x_prev)/diff(func(x_prev))) except ZeroDivisionError: return 'ERROR: Zero division!' if abs(x - x_prev) <= eps: break else: x_prev = x return x 

    1 answer 1

    Found a mistake. The fact is that func(x) always returns a constant, and diff(func(x_prev)) always zero.

    • It's great that you yourself figured it out, but since you wrote the answer to your question, it would be nice to indicate the working code in it. Who knows, your answer in the future will help someone? - strawdog