How do you evaluate a derivative in python?

Viewed 22162

I'm a beginner in python. I've recently learned about Sympy and its symbolic manipulation capabilities, in particular, differentiation. I am trying to do the following in the easiest way possible:

  1. Define f(x,y) = x^2 + xy^2.
  2. Differentiate f with respect to x. So f'(x,y) = 2x + xy^2.
  3. Evaluate the derivative, e.g., f'(1,1) = 2 + 1 = 3.

I know how to do 1 and 2. The problem is, when I try to evaluate the derivative in step 3, I get an error that python can't calculate the derivative. Here is a minimal working example:

import sympy as sym
import math


def f(x,y):
    return x**2 + x*y**2


x, y = sym.symbols('x y')

def fprime(x,y):
    return sym.diff(f(x,y),x)

print(fprime(x,y)) #This works.

print(fprime(1,1)) 

I expect the last line to print 3. It doesn't print anything, and says "can't calculate the 1st derivative wrt 1".

3 Answers
Related