In Symbolics.jl
, I can formulate a set of equations purely symbolically.
I can for example define this differential equation using @syms
:
using Symbolics
@syms α ρ[1:2, 1:2] dαdt dρdt[1:2, 1:2]
eqs = []
push!(eqs, dαdt == α*(ρ[1,1] +ρ[1,2] + ρ[2,1] + ρ[2,2]))
for i in 1:2, j in 1:2
push!(eqs, dρdt[i,j] == α*ρ[j,i])
end
eqs
then has the form:
5-element Vector{Any}:
dαdt == (α*(ρ[1, 1] + ρ[1, 2] + ρ[2, 1] + ρ[2, 2]))
dρdt[1, 1] == (α*ρ[1, 1])
dρdt[1, 2] == (α*ρ[2, 1])
dρdt[2, 1] == (α*ρ[1, 2])
dρdt[2, 2] == (α*ρ[2, 2])
To solve the above equation using e.g. ModelingToolkit.jl
, the symbolic parameters must be replaced by variables, e.g.
@variables t::Real, αvar(t)::Complex{Real}, ρvar(t)[1:2, 1:2]::Complex{Real}
Question: How can I transform the above symbolic equation to the correct variables, so that it can be solved using e.g. an ODEProblem
?
Remark: A trivial solution is of course to just use the variables as defined above in the first place. This is however not the point of this question.
Something like this may work:
using Symbolics
@syms τ α ρ[1:2, 1:2]
D = Differential(τ)
eqs = []
push!(eqs, D(α) ~ α*(ρ[1,1] +ρ[1,2] + ρ[2,1] + ρ[2,2]))
for i in 1:2, j in 1:2
push!(eqs, D(ρ[i,j]) ~ α*ρ[j,i])
end
and then
using ModelingToolkit
@parameters t
@variables αvar(t), ρvar(t)[1:2, 1:2]
subs = Dict(τ => t, α => αvar, ρ => ρvar)
eqs_subbed = substitute.(eqs, Ref(subs))