pythonsympydifferentiation

Numerical value of differention in sympy in python


I can evaluate differential function in sympy python using g(x) = diff(f(x), x). My question is how do I evaluate numerical value of g(x)? for example at x = 2?

from sympy import *
f = diff(cos(x), x)
f(3.14)

This gives following error:

print(f(3.14))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Mul' object is not callable

Please help me out. Is there any other package?


Solution

  • You can try

    >>> from sympy import *
    >>> x=Symbol('x')
    >>> f = diff(cos(x), x)
    >>> f
    -sin(x)
    >>> f.subs(x,3.14)
    -0.00159265291648683
    >>> f.subs(x,pi)
    0
    >>> f.subs(x,2)
    -sin(2)
    >>> f.subs(x,2).evalf()
    -0.909297426825682
    >>>