Home > Blockchain >  Why am I getting derivative of f(x) = x**2, where x = 2, as 0 instead of 4?
Why am I getting derivative of f(x) = x**2, where x = 2, as 0 instead of 4?

Time:12-26

  • I get correct derivative of f(x) = x**2 as 2*x

  • However, when I substitute the value of x with 2, it gives 0 as result instead of 4

  • Whatever value I am giving to x, it gives 0 as a result

snapshot of Google Colab notebook

I tried this:

from sympy import *

x = symbols('x')
f = x**2

diff(f.subs(x,2), x)

I was expecting 4 as a result but I get 0

CodePudding user response:

It's because you're taking a derivative of a constant. You should derive the function of f = x**2 first and then substitute x = 2 to the result afterward. Try something like:

diff(f, x).subs(x, 2)

or

df = diff(f, x)
df.subs(x, 2)
  • Related