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]
?