Error in using Sympy in newton Raphson

32 views
Skip to first unread message

Swadhin Satapathy

unread,
Dec 3, 2021, 5:03:57 PM12/3/21
to sympy
I got the following error: 

  File "C:\Users\Admin\.spyder-py3\temp.py", line 22, in <module>
    a = newton(p, p_prime,-10, 0)

  File "C:\Users\Admin\.spyder-py3\temp.py", line 15, in newton
    delta = p(x)/p_prime(x)

TypeError: 'Add' object is not callable

 While running the following code: 

import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
acc = 1E-10

x = sym.Symbol('x')
def p(x): #define the function
    return 924*x**6 - 2772*x**5 + 3150*x**4 - 1680*x**3 + 420*x**2 - 42*x + 1

p_prime = sym.diff(p(x))
print(p_prime)

def newton(p,p_prime, acc, start_val):
    x= start_val
    delta = p(x)/p_prime(x)
    while abs(delta) > acc:
        delta = p(x)/p_prime(x)
        print(abs(delta))
        x =x- delta;
    return round(x, acc);

a = newton(p, p_prime,-10, 0)

David Bailey

unread,
Dec 4, 2021, 9:32:45 AM12/4/21
to sy...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6fdc90f7-7013-4dc5-a96a-5a9db8b5a122n%40googlegroups.com.

Try adding this line immediately before your line 15:

    print("p=",p,"  p_prime=",p_prime)

If you do that, you will see that p is a function - so it is OK to call it as in p(x),

however p_prime is an expression (an Add object, since it consists of a number of terms added/subtracted together), so it isn't OK to call it as in p_prime(x).

Hence your error message!

TypeError: 'Add' object is not callable

That error message is super obscure, it would be good to see it upgraded.

Cheers,

David


Swadhin Satapathy

unread,
Dec 4, 2021, 4:03:04 PM12/4/21
to sympy
Thanks, David, I realized that and solved it. Thanks for the heads-up. 
Reply all
Reply to author
Forward
0 new messages