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…