# Symbolics and vector

So in a similar way to this post I wonder why I am getting the following:

``````using Symbolics
@variables x[1:5] y

ex1 = x[1]^2+3*x[5]+x[3]*y
ex2 = x[1]*x[2]*x[4]^2+10

Symbolics.jacobian([ex2], x[1:5]) ###Works, gives :  x[2]*(x[4]^2)  x[1]*(x[4]^2)  0  2x[1]*x[2]*x[4]  0
Symbolics.jacobian([ex2], x) ###Works, gives :  x[2]*(x[4]^2)  x[1]*(x[4]^2)  0  2x[1]*x[2]*x[4]  0
Symbolics.jacobian([ex1], x) ###Works, gives  2x[1]  0  y  0  3
Symbolics.jacobian([ex1], [x, y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)
Symbolics.jacobian([ex1], [x[1:5], y]) ####Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)
Symbolics.jacobian([ex1], [x[1],x[2],x[3],x[4],x[5], y]) ###Works, gives:  2x[1]  0  y  0  3  x[3]
Symbolics.jacobian([ex1], [x[1], y]) ###Works, gives:   2x[1]  x[3]
Symbolics.jacobian([ex1], [x[1:2], y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64)

###############Different definition of the vector x, found [here](https://github.com/JuliaSymbolics/Symbolics.jl/issues/767#issuecomment-1291476389)
@variables y
x= Symbolics.variables(:x, 1:5)

ex1 = x[1]^2+3*x[5]+x[3]*y
ex2 = x[1]*x[2]*x[4]^2+10

Symbolics.jacobian([ex2], x[1:5]) ###Works, gives x₂*(x₄^2)  x₁*(x₄^2)  0  2x₁*x₂*x₄  0
# Symbolics.jacobian([ex2], x) ###Works, gives  x₂*(x₄^2)  x₁*(x₄^2)  0  2x₁*x₂*x₄  0
# Symbolics.jacobian([ex1], x) ###Works, gives  2x₁  0  y  0  3
# Symbolics.jacobian([ex1], [x, y]) ###Gives  0  x₃
# Symbolics.jacobian([ex1], [x[1:5], y]) ####Gives  0  x₃
# Symbolics.jacobian([ex1], [x[1],x[2],x[3],x[4],x[5], y]) ####Works, gives 2x₁  0  y  0  3  x₃
# Symbolics.jacobian([ex1], [x[1], y]) ###Gives  2x₁  x₃
# Symbolics.jacobian([ex1], [x[1:2], y]) ###Gives  0  x₃

``````

Two questions:

• Is there a way to avoid writing the vector explicitly to get the jacobian? With five elements it’s OK, but more will be rather tedious.
• Which way of defining vector variables is better, `x= Symbolics.variables(:x, 1:5) ` or `@variables x[1:5]`?

Neither is necessarily “better”, they are just different representations.

Instead of just answering here, I improved the docs. See:

Let me know if you have more questions.

1 Like

Thanks! The documentation was quite helpful and now the example makes more sense:

``````#######Differences
@variables x[1:5] y
ex1 = x[1]^2+3*x[5]+x[3]*y
# Symbolics.jacobian([ex1], [x, y]) ###Doesn't work, error: ERROR: MethodError: no method matching occursin(::Vector{Num}, ::Int64) - no jacobian for an entire array x
Symbolics.jacobian([ex1], vcat(x, y)) ###Works, gives 2x₁  0  y  0  3  x₃

@variables y
x= Symbolics.variables(:x, 1:5)
ex1 = x[1]^2+3*x[5]+x[3]*y
Symbolics.jacobian([ex1], [x, y]) ###Gives  0  x₃ - there is no element x in [x,y] here, so we get zero
Symbolics.jacobian([ex1], vcat(x, y)) ###Works, gives 2x₁  0  y  0  3  x₃

``````

For future reference, the problem from the initial post (how to do Jacobians without writing out all the elements) turned out to be my expectation of Matlab-like behaviour of `[]`:

and