DimensionMismatch: arrays could not be broadcast to a common size: a has axes Base.OneTo(5) and b has axes Base.OneTo(0)

Im trying to find the ODEs of some concentration profiles using SINDy.

r1 and r_deact are known terms that can be in the ODE so I added them to the basis. (also tried already with a simpler basis but same problem). Also, the sizes of Xₙ (5, 161) and t (161,) match. What am I overseeing?

@variables u[1:5]

Reaction rates

f_act = u[4] / C_sites_0
r1 = k1 * u[1] * u[3] * W_cat * f_act / (1 + K_RNO2 * u[1] + sqrt(K_H * u[3]))^3
r_deact = k_deact * u[1] * u[4]

poly_basis = polynomial_basis(u, 2)

basis_functions = vcat(poly_basis, [r1, r_deact])
basis = Basis(basis_functions, u)

Xₙ = Array(true_solutions_noisy[N_values[1]])

full_problem = ContinuousDataDrivenProblem(Xₙ, t)

λ = 1e-1
opt = ADMM(λ)

options = DataDrivenCommonOptions(maxiters = 10_000,
normalize = DataNormalization(ZScoreTransform),
selector = bic, digits = 1,
data_processing = DataProcessing(split = 0.9,
batchsize = 200,
shuffle = true,
rng = StableRNG(1111)))

full_res = solve(full_problem, basis, opt, options = options)
full_eqs = get_basis(full_res)
println(full_res)

Hi there and welcome to the Discourse :wave:
Please make your code easier to read by enclousing it in triple backticks ```
Please include the error message as well and try to indicate which line fails if you can make it out :slight_smile:

@variables u[1:5] 

# Reaction rates
f_act = u[4] / C_sites_0
r1 = k1 * u[1] * u[3] * W_cat * f_act / (1 + K_RNO2 * u[1] + sqrt(K_H * u[3]))^3
r_deact = k_deact * u[1] * u[4]

# Define polynomial basis terms
poly_basis = polynomial_basis(u, 2)

# Include reaction rates in the basis
basis_functions = vcat(poly_basis, [r1, r_deact])
basis = Basis(basis_functions, u)

#only for 1 N value for now
Xₙ = Array(true_solutions_noisy[N_values[1]])

full_problem = ContinuousDataDrivenProblem(Xₙ, t)

λ = 1e-1
opt = ADMM(λ)

options = DataDrivenCommonOptions(maxiters = 10_000,
    normalize = DataNormalization(ZScoreTransform),
    selector = bic, digits = 1,
    data_processing = DataProcessing(split = 0.9,
        batchsize = 200,
        shuffle = true,
        rng = StableRNG(1111)))

full_res = solve(full_problem, basis, opt, options = options)
full_eqs = get_basis(full_res)
println(full_res)

I get the following error message when I try to run full_res = solve(full_problem, basis, opt, options = options):
ERROR: DimensionMismatch: arrays could not be broadcast to a common size: a has axes Base.OneTo(5) and b has axes Base.OneTo(0)

Provide the complete error message. In a code block.

Also this is not the full code. As the definitions of a least true_solutions_noisy and N_values are missing but I think they might be important to understand your issue.

LoadError: DimensionMismatch: arrays could not be broadcast to a common size: a has axes Base.OneTo(5) and b has axes Base.OneTo(0)
Stacktrace:
  [1] _bcs1
    @ .\broadcast.jl:523 [inlined]
  [2] _bcs
    @ .\broadcast.jl:517 [inlined]
  [3] broadcast_shape
    @ .\broadcast.jl:511 [inlined]
  [4] combine_axes
    @ .\broadcast.jl:492 [inlined]
  [5] instantiate
    @ .\broadcast.jl:302 [inlined]
  [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2}, Nothing, typeof(-), Tuple{Matrix{Any}, Matrix{Any}}})
    @ Base.Broadcast .\broadcast.jl:867
  [7] DataDrivenSolution(b::Basis{false, false}, p::DataDrivenProblem{Float64, false, DataDrivenDiffEq.Continuous}, alg::ADMM{Float64, Float64}, result::Vector{DataDrivenSparse.SparseRegressionResult{Matrix{Float64}, Vector{Any}, Vector{Int64}, Float64, Float64, DDReturnCode}}, internal_problem::DataDrivenDiffEq.InternalDataDrivenProblem{ADMM{Float64, Float64}, Basis{false, false}, Tuple{SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}}, DataLoader{Tuple{SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}}, StableRNGs.LehmerRNG, Val{nothing}}, ZScoreTransform{Float64, Vector{Float64}}, Matrix{Bool}, Matrix{Bool}, Vector{Bool}, Matrix{Bool}, DataDrivenCommonOptions{Float64, @NamedTuple{}}, DataDrivenProblem{Float64, false, DataDrivenDiffEq.Continuous}, @Kwargs{}}, retcode::DDReturnCode)
    @ DataDrivenDiffEq C:\Users\Casper Casier\.julia\packages\DataDrivenDiffEq\gpXhb\src\solution.jl:37
  [8] solve!(ps::DataDrivenDiffEq.InternalDataDrivenProblem{ADMM{Float64, Float64}, Basis{false, false}, Tuple{SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}}, DataLoader{Tuple{SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}}, StableRNGs.LehmerRNG, Val{nothing}}, ZScoreTransform{Float64, Vector{Float64}}, Matrix{Bool}, Matrix{Bool}, Vector{Bool}, Matrix{Bool}, DataDrivenCommonOptions{Float64, @NamedTuple{}}, DataDrivenProblem{Float64, false, DataDrivenDiffEq.Continuous}, @Kwargs{}})
    @ DataDrivenSparse C:\Users\Casper Casier\.julia\packages\DataDrivenSparse\0c4Fb\src\commonsolve.jl:21
  [9] solve(::DataDrivenProblem{Float64, false, DataDrivenDiffEq.Continuous}, ::Vararg{Any}; kwargs::@Kwargs{options::DataDrivenCommonOptions{Float64, @NamedTuple{}}})
    @ CommonSolve C:\Users\Casper Casier\.julia\packages\CommonSolve\JfpfI\src\CommonSolve.jl:23
 [10] top-level scope
    @ c:\Users\Casper Casier\Desktop\UGent\6 Master chemical engineering\Thesis\Kinetics simultaneous training.jl:341
 [11] eval
    @ .\boot.jl:430 [inlined]
 [12] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base .\loading.jl:2734
 [13] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::@Kwargs{})
    @ Base .\essentials.jl:1055
 [14] invokelatest(::Any, ::Any, ::Vararg{Any})
    @ Base .\essentials.jl:1052
 [15] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\eval.jl:271
 [16] (::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\eval.jl:181
 [17] withpath(f::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\repl.jl:276
 [18] (::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\eval.jl:179
 [19] hideprompt(f::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\repl.jl:38
 [20] #67
    @ c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\eval.jl:150 [inlined]
 [21] with_logstate(f::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, logstate::Base.CoreLogging.LogState)     
    @ Base.CoreLogging .\logging\logging.jl:522
 [22] with_logger
    @ .\logging\logging.jl:632 [inlined]
 [23] (::VSCodeServer.var"#66#71"{VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\Casper Casier\.vscode\extensions\julialang.language-julia-1.127.2\scripts\packages\VSCodeServer\src\eval.jl:263
 [24] #invokelatest#2
    @ .\essentials.jl:1055 [inlined]
 [25] invokelatest(::Any)
    @ Base .\essentials.jl:1052
 [26] (::VSCodeServer.var"#64#65")()

N values shouldn’t matter that much as true_solutions_noisy is a dictionary with results for different N values, but I’m only using the results of 1 N value for now. true_solutions_noisy contains all concentration profiles over time. For 1 N value, this has a shape of (5,161) like I previously mentioned