Using SymPy.jl with two variables

I ran the following code

using SymPy
@syms x y
f(x,y) = cos(x - y)
df(x,y) = sympy.diff(f(x,y),x)
df(0,pi/2) # it is supposed to be 1, but an error occurs

Please help me to resolve this problem.

Thank you.

Do one of the following:

  • remove the arguments from the definition of df, so write df = sympy.diff(f(x,y),x) instead of df(x,y) = sympy.diff(f(x,y),x)
  • call df(x,y)(0,pi/2) instead of df(0, pi/2)

The reason it fails is because you are creating a Julia function that takes two arguments x, y and evaluates the expression on the right hand side. So df(0, 1) would try to do sympy.diff(f(0,1),0) which is not what you want I suppose.

Instead, SymPy already returns an object from diff which can be called with the remaining variables.

3 Likes