Simulink error: Time-Varying State Space block

I have a Simulink error I can't figure out, relating to the Time-Varying State Space block dimensions.
I'm using this block so that i can also get x : normal SS block doesn't output x.
This is a simple 2x2 system, and the system dimensions are correct, but I'm getting a port dims error -- it seems to be Output Port 2 (the length-2 "x" output). It could be any system, i happen to have a closed-loop state space, but anything triggers the same error.
Any thoughts why?
I've spent way too much time trying to figure out what's going on...
SL model attached, and Runnable code below.
Error:
Matrix multiply dimensions propagation error. Error occurred while setting output port of 'LQR_post/Varying State Space/Product1' to have the dimensions [1 x 2]. A possible cause for this error is that these dimensions do not agree with the partial dimensions information present on other ports
Component:Simulink | Category:Model error
Error in port widths or dimensions. 'Input Port 2' of 'LQR_post/Varying State Space/Sum' is a [1x2] matrix.
Component:Simulink | Category:Model error
Code:
J = 1e-5;
b = 0.0016;
k = 0.632;
A = [0, 1; -k / J, -b / J];
B = [0; 1 / J];
C = [1 0];
D = [0];
sys = ss(A, B, C, D, ...
'StateName', {'x', 'x dot'}, ...
'InputName', {'r(ref)'}, ...
'OutputName', {'theta'});
Q = [1000 0 ; 0 0.001];
R = [ 0.01 ];
[K_lqr, S1, P1] = lqr(sys, Q, R);
sys_lqr = ss(sys.A - sys.B * K_lqr, sys.B * K_lqr * [1;0], sys.C, sys.D);
sys_lqr.StateName = {'theta', 'angVel'};
sys_lqr.InputName = {'r(ref)'};
sys_lqr.OutputName = {'theta'};
sys_lqr
sys_lqr = A = theta angVel theta 0 1 angVel -3.162e+07 -3.261e+04 B = r(ref) theta 0 angVel 3.156e+07 C = theta angVel theta 1 0 D = r(ref) theta 0 Continuous-time state-space model.

 Accepted Answer

If you want to get the state vector x, then you can consider this approach because the output vector y can be constructed from the state vector info. In your case, .
J = 1e-5;
b = 0.0016;
k = 0.632;
A = [0, 1; -k/J, -b/J];
B = [0; 1/J];
C = eye(2); % Change the Output Matrix to an Identity Matrix
D = [0; 0];
Q = [1000 0; 0 0.001];
R = [0.01];
K = lqr(A, B, Q, R)
K = 1×2
315.5964 0.3245

3 Comments

Interesting, thanks @Sam Chak. Just to understand more, since the documentation page seems sparse (if that's the page to look at), what's occurring here? Is State Space configurable and automatically switches to output either x or y, depending on dimensions of C? Or are you turning y into a 2-element output, and treating it as x?
Is there any other state space block that outputs both (I know x, y are basically equivalent), or is the block i used the best one for that?
Hi John,
The Varying State Space block is actually a Block Mask. If you look under mask, you will see the fundamental blocks of the State-Space. It is important to understand how the matrices work in the State-Space.
By default, the option "Interpret vector parameters as 1-D" in the Constant Block is checked. Because we want the Constant block to treat that specified vector as a matrix, this option must be unchecked.
@Sam Chak, ah, thanks. That clarifies. So the output is x (assuming D = 0) as long as C = I. So fundamentally, the State Space block has x available for the output. Thanks :)

Sign in to comment.

More Answers (1)

Hi John,
In the Constant blocks for sys_lqr.B and sys_lqr.C try clearing the block parameter "Interpret vector parameters as 1-D — Treat vectors as 1-D"

5 Comments

John
John on 18 Mar 2023
Edited: John on 18 Mar 2023
Thanks @Paul, good suggestion...those Simulink errors seem super unclear on actionable things -- not sure I'd know how to try that:)
(I forgot to attach the .slx; added now.)
In any case, I tried it, without change. However some additional other errors came up; eg "y" is now undefined dimension as well.
I also tried clearing that block parameter for all matrices, with the same result -- although for anything not interpretable as a vector, perhaps that's not a relevant setting.
For the model shown in the picture with constants feeding the Variable State Space block (with its default block parameters) that is feeding a scope, clearing those check boxes on the Constant blocks for B and C cleared up all errors for me.
Having said that, I think @Sam Chak's solution of redefining the C matrix to include the states in the output is nice approach.
John
John on 20 Mar 2023
Edited: John on 20 Mar 2023
Thanks @Paul. I closed everything and re-opened, and it did work. I must have made a mistake previously, when trying what you proposed. Thanks for the solution :)
What should I have looked for in the error message to know that this was the fix?
The error messages talk about a [1x2] matrix. For what Simulink calls 1D signals Simulink tries to infer dimensions. For example, those 1D signals indicated with a "2" on the signal line could be interpreted as either 1x2 or 2x1 and Simulink tries to figure that out. I thought it likely there was an issue with Simulink not being able to figure out which. By unchecking that box you take away that flexibility and it makes it easier for Simulink to figure out what's what. One tradeoff is matrix signals get logged as 3D, as discussed in your other question.
That's very helpful; thanks @Paul.

Sign in to comment.

Products

Release

R2022b

Asked:

on 18 Mar 2023

Commented:

on 22 Mar 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!