On 03/12/2021 19:22, Swadhin Satapathy wrote:
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)
--
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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6fdc90f7-7013-4dc5-a96a-5a9db8b5a122n%40googlegroups.com <https://groups.google.com/d/msgid/sympy/6fdc90f7-7013-4dc5-a96a-5a9db8b5a122n%40googlegroups.com?utm_medium=email&utm_source=footer>.

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

--
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/491de999-a5b5-2091-8820-e587a0f380a3%40dbailey.co.uk.

Reply via email to