The reason is that in print(df(4))
you are passing the number 4 to df
which passes it to sp.diff
like sp.diff(f(4), 4, 1)
.
You meant to pass sp.Symbol('x')
to sp.diff
which then will return a function (= the derivative of f
with respect to x
) which to that you can pass the number 4 (= evaluate at x = 4
).
import sympy as sp
def f(x):
return (x**2-3)/2
x = sp.Symbol('x')
def df(x):
return sp.diff(f(x), x, 1)
print('la dérivée de f(x) est:', df(x))
print(df(x)(4)) # note the additional (x) here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…