# Derivatives and Symbolics.jacobian in Symbolics.jl

**URL:** https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581
**Category:** New to Julia
**Tags:** question
**Created:** [October 31, 2022, 8:31pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581 "2022-10-31T20:31:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![FelixHeox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felixheox/32/44015_2.png) [@FelixHeox](https://discourse.julialang.org/u/FelixHeox)
#### Post date: [October 31, 2022, 8:31pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/1 "2022-10-31T20:31:11Z")

</div>

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:

```julia
@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:

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

```

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)`

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [October 31, 2022, 9:09pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/2 "2022-10-31T21:09:26Z")

</div>

- `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](https://symbolics.juliasymbolics.org/dev/tutorials/symbolic_functions/#Building-Functions-1)
- If you want to calculate the derivative, you can use the `derivative` function

---

<div class="post-metadata">

### Author: ![FelixHeox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felixheox/32/44015_2.png) [@FelixHeox](https://discourse.julialang.org/u/FelixHeox)
#### Post date: [October 31, 2022, 10:01pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/3 "2022-10-31T22:01:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [October 31, 2022, 10:55pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/4 "2022-10-31T22:55:38Z")

</div>

You need to write

```julia
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.

---

<div class="post-metadata">

### Author: ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)
#### Post date: [May 3, 2025, 7:22pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/5 "2025-05-03T19:22:47Z")

</div>

I would expect to get the same results between fun1 and fun2:  
@variables x y  
f = x^3+sin(y+x)  
fun1(x,y)=cos(x+y)+3\*x^2  
df=Symbolics.derivative(f,x)  
df  
fun2 = (build\_function(df, [x,y];  
expression=Val{false},  
target=Symbolics.JuliaTarget())) # expression=Val{true} is default  
fun1(1,2)  
fun2(1,2)

But:  
julia\> fun1(1,2)  
2.010007503399555

julia\> fun2(1,2)  
2.5838531634528574

---

<div class="post-metadata">

### Author: ![herm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herm/32/14268_2.png) [@herm](https://discourse.julialang.org/u/herm)
#### Post date: [May 3, 2025, 8:18pm UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/6 "2025-05-03T20:18:36Z")

</div>

The correct syntax is:

fun3 = (build\_function(df, x,y;  
expression=Val{false},  
target=Symbolics.JuliaTarget())

we have therefore:

julia\> fun1(1,2)  
2.010007503399555

julia\> fun2([1,2])  
2.010007503399555

julia\> fun3(1,2)  
2.010007503399555

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 4, 2025, 2:48am UTC](https://discourse.julialang.org/t/derivatives-and-symbolics-jacobian-in-symbolics-jl/89581/7 "2025-05-04T02:48:51Z")

</div>

@cryptic.ax does the example not error because of inbounds defaults?
