Derivatives and Symbolics.jacobian in Symbolics.jl

Hi, this is my first post here. I am new to using Symbolics.jl (coming from a MATLAB background) and have a couple questions regarding Differential and Symbolics.jacobian. I am using a Mac, and the Pluto environment:

@variables x y 
f1(x,y) = x + sin(y)
f2(x,y)= y + cos(x)

J(x,y) = Symbolics.jacobian([f1(x,y), f2(x,y)], [x, y])
# Pluto cell outputs J(x,y) =  [1 cos(y); -sin(x) 1] 

The type of J(x,y) is Matrix{Num}, witch is confusing because I expected it to be a matrix of functions of x,y. Furthermore, evaluating J(1,2) returns a zero matrix instead of what i expect: [1 cos(1); -sin(2) 1].

A similar problem also occurred using Symbolic.jl Differential:

@variables x y
D = Differential(x)
f(x,y) = x^3+sin(y+x)
df(x,y) = expand_derivatives(D(f(x,y)))

The cell returns 0, instead of` 3+cos(3). df(x,y) correctly outputs as 3x^2+cos(x+y) but again, the type of df(x,y) is Num. My best guess is that in Julia, f(1,2) is evaluated first (1+sin(3)) then the derivative is applied to the constant and thus equals zero. But the order of operations here seem counterintuitive.

Why does this happen? Is there any way to somehow take the derivative of f, and store it as a function in df such that I can call it like df(1,2)

  • J is a julia function, remove (x, y) and you get the desired array
  • Num is the type of an expression in Symbolics
  • Use the substitute function to get the numeric result from a given expression
  • If you want a callable function look here: Symbolic Calculations and Building Callable Functions · Symbolics.jl
  • If you want to calculate the derivative, you can use the derivative function

Thanks for the fast response! using substitute seems to be working really well. I did try using Symbolics.derivative(f, x) but kept on getting 0

You need to write

Symbolics.derivative( f(x,y), x)

since f itself just is a standard Julia method, it doesn’t know if it should depend on the variables x,y in a sense f(x,y) or maybe f(y,x). But if you call f with the variables as input, then you get a symbolic expression f(x,y) which you can take the derivative of.