Matrix Dimension mismatch

Hi

I checked all similar topics but, I think they did not match my case,
I faced mismatch matrix size several times, for different conditions,
(while I am sure the matrices and sizes are true and because I am trying to use examples, I am also sure, there must be a solution, too)

For example, I am trying to find a stabilized gain matrix for a selected closed loop sys during an LMI constraint,

Code:

using LinearAlgebra
using Convex
using SCS

# Define the state space model
A = [-0.0366 0.0271 0.0188 -0.4555; 0.0482 -1.01 0.0024 -4.0208; 0.1002 0.3681 -0.7070 1.4200; 0 0 1 0]
B = [0.4422 0.1761;3.5446 -7.5922;-5.5200 4.49;0 0]
C = [0 1 0 0]

# Define dimensions
n = size(A, 1)
m = size(B, 2)

# Define decision variable X
X = Semidefinite(n)

# Problem parameters
eps = 1e-3
max_iterations = 100  # Specify a maximum number of iterations

# Iterate until a stabilizing K is found
for iteration in 1:max_iterations
    constraints = [
        X >= eps * I(n),
        A' * X + X * A + B * B' <= -eps * I(n)
    ]

    # Define objective function
    problem = minimize(tr(X), constraints)

    @time begin
        solve!(problem, SCS.Optimizer)
    end

    X_value = evaluate(X)
    K = -inv(X_value) * (A * inv(X_value) + B * C)

    # Check eigenvalues of A+BKC
    closed_loop_A = A + B * K * C
    eigenvalues = eigvals(closed_loop_A)

    if all(eigenvalues .< 0)
        println("Stabilizing gain matrix K found in $(iteration) iterations:")
        println(K)
        break  # Exit the loop if a stabilizing K is found
    else
        println("Iteration $(iteration): Eigenvalues not negative. Retrying...")
    end
end

if iteration == max_iterations + 1
    println("Failed to find a stabilizing K within $(max_iterations) iterations.")
end

Error:

------------------------------------------------------------------
               SCS v3.2.4 - Splitting Conic Solver
        (c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem:  variables n: 17, constraints m: 49
cones:    z: primal zero / dual free vars: 7
          l: linear vars: 32
          s: psd vars: 10, ssize: 1
settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07
          alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
          max_iters: 100000, normalize: 1, rho_x: 1.00e-06
          acceleration_lookback: 10, acceleration_interval: 10
lin-sys:  sparse-direct-amd-qdldl
          nnz(A): 138, nnz(P): 0
------------------------------------------------------------------
 iter | pri res | dua res |   gap   |   obj   |  scale  | time (s)
------------------------------------------------------------------
     0| 7.32e+01  1.87e+01  8.86e+02  4.40e+02  1.00e-01  1.59e-04 
   250| 4.61e-02  7.91e-03  6.94e-01  9.59e+01  2.41e-02  7.73e-04 
   325| 1.14e-03  7.11e-05  3.49e-03  9.60e+01  2.41e-02  9.75e-04 
------------------------------------------------------------------
status:  solved
timings: total: 9.80e-04s = setup: 1.12e-04s + solve: 8.68e-04s
         lin-sys: 2.61e-04s, cones: 4.19e-04s, accel: 3.29e-05s
------------------------------------------------------------------
objective = 96.025178
------------------------------------------------------------------
  8.017841 seconds (22.94 M allocations: 1.549 GiB, 5.55% gc time, 99.15% compilation time)
ERROR: DimensionMismatch: matrix A has dimensions (4,2), matrix B has dimensions (1,4)
Stacktrace:
 [1] _generic_matmatmul!(C::Matrix{…}, tA::Char, tB::Char, A::Matrix{…}, B::Matrix{…}, _add::LinearAlgebra.MulAddMul{…})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:794
 [2] generic_matmatmul!(C::Matrix{…}, tA::Char, tB::Char, A::Matrix{…}, B::Matrix{…}, _add::LinearAlgebra.MulAddMul{…})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:783
 [3] mul!
   @ ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:263 [inlined]
 [4] mul!
   @ ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:237 [inlined]
 [5] *(A::Matrix{Float64}, B::Matrix{Int64})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:106
 [6] top-level scope
   @ /Volumes/University/Codes/Julia/to_learn.jl:36
Some type information was truncated. Use `show(err)` to see complete types.

Please help me…

1 Like

Hi there!
Please for your future post try to format them a bit nicer (have a look at Please read: make it easier to help you).

That being said, I could identify the issue nonetheless.

does not work because B has size 4x2 and C has size 1x4. You probably wanted to create these with different shapes. I recommend reading the manual section on array literals to figure out how the syntax works:
https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals

2 Likes

Thank you for your reply and sorry about the problem in my previous post.

Here B=4 by 2 and C= 1 by 4
we want to make BKC where K= 2 by 1 and
BKC= 4 by 4

You wrote (2nd line below the call to solve!):

and this fails because B (4x2) and C (1x4) are incompatible. If I understand you correctly, you say K should have dimension (2x1) so perhaps you wanted to transpose B and C?
K = -inv(X_value) * (A * inv(X_value) + B' * C')

1 Like