Skip to content

"Axes of broadcast not known" with differential matrix equation #1740

Closed
@florian-die

Description

@florian-die

Hi,

I'm trying to create a somewhat generic system of linear differential equations in a matrix form, but it fails.

My code is as follows.

using ModelingToolkit

A = [0. 1. ; 0. 0.] # 2×2 Matrix{Float64}
B = [0. ; 1.0 ;;] # 2×1 Matrix{Float64}

x0 = [0.0 ; 1.0 ;;] # 2×1 Matrix{Float64}
u0 = [0.0 ;;] # 1×1 Matrix{Float64}

dx0 = A*x0 + B*u # 2×1 Matrix{Float64}

function linear_system(A,B;name)
    n = size(A,1) 
    m = size(B,2)

    @variables t x(t)[1:n,1] u(t)[1:m,1]
    D = Differential(t)

    eqs = [ D(x) ~ A*x + B*u ]

    ODESystem(eqs;name)
end

@named double_int = linear_system(A,B)

The last line yields the following error

ERROR: axes of (broadcast(~, Differential(t)((x(t))[1:2,1:1]), broadcast(+, [0.0 1.0; 0.0 0.0]*x(t), [0.0; 1.0;;]*u(t)))) not known
Stacktrace:
  [1] error(s::String)
    @ Base .\error.jl:33
  [2] axes(A::Symbolics.Arr{Any, 2})
    @ Symbolics D:\fdietric\.julia\packages\Symbolics\J8IHJ\src\arrays.jl:527
  [3] scalarize(arr::Symbolics.Arr{Any, 2})
    @ Symbolics D:\fdietric\.julia\packages\Symbolics\J8IHJ\src\arrays.jl:711
  [4] iterate
    @ .\generator.jl:47 [inlined]
  [5] _collect(c::Vector{Symbolics.Arr{Any, 2}}, itr::Base.Generator{Vector{Symbolics.Arr{Any, 2}}, typeof(Symbolics.scalarize)}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base .\array.jl:744
  [6] collect_similar
    @ .\array.jl:653 [inlined]
  [7] map
    @ .\abstractarray.jl:2867 [inlined]
  [8] scalarize
    @ D:\fdietric\.julia\packages\Symbolics\J8IHJ\src\arrays.jl:552 [inlined]
  [9] ODESystem(eqs::Vector{Symbolics.Arr{Any, 2}}, iv::Nothing; kwargs::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:name,), Tuple{Symbol}}})
    @ ModelingToolkit D:\fdietric\.julia\packages\ModelingToolkit\J4yrw\src\systems\diffeqs\odesystem.jl:191
 [10] linear_system(A::Matrix{Float64}, B::Matrix{Float64}; name::Symbol)
    @ Main d:\fdietric\Documents\Julia\ModelingToolkit\linearsystem\linear_system.jl:22
 [11] top-level scope
    @ d:\fdietric\Documents\Julia\ModelingToolkit\linearsystem\linear_system.jl:25

and the status

(linearsystem) pkg> status
      Status `D:\fdietric\Documents\Julia\ModelingToolkit\linearsystem\Project.toml`
  [0c46a032] DifferentialEquations v7.2.0
  [961ee093] ModelingToolkit v8.18.8
  [91a5bcdd] Plots v1.31.6

I tried to play with the dimensions of the variables x and u, but the error remains the same.

I also tried to put . dots before the operator + and ~ operators.

I usually avoid using .+ by enforcing the size of every matrix/vectors in the equation (mostly for the colum vectors so they are n by 1 matrices).

Maybe I made a mistake in setting the system or I missed something.

Anyway, thanks in advance for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions