how to fix an error:'' error in port widths or dimensions.''

60 views (last 30 days)
Hello everyone,
I made a state space representation with disturbances using blocks in simulink, then I connected this state space to model predictive control, and when I run the system it gives an error as shown below: the red spot in the figure is where the error is,
and my state space model is as follow:
so A, B, C and D and Bd should be correct as I made them, right?
  4 Comments
Daniel
Daniel on 14 Apr 2023
Thanks for the screenshots with dimensions. They help a lot.
Let's start by discussing the state space equations. You've got a column vector output: 2 rows by 1 column. So the inputs Ax(n) and Bu(n) should both be 2x1.
Presuming that your input ref is supposed to be the same dimensionality as your output mv, you're currently driving a 1x2 row vector rather than a 2x1 column vector. That will throw off the state space in your plant model.
Now to discuss the different multiplication options...
In MATLAB we have a few different ways of multiplying, which Simulink mimics, following standard linear algebra techniques.
First method: Elementwise
If you have a matrix A, of dimension , and you want to multiply each element in A by the matching element in a matrix B, then you can do that as:
A = rand(2,3) % Create a 2x3 matrix A
A = 2×3
0.7650 0.2530 0.2346 0.8413 0.9868 0.0356
B = rand(2,3) % Create a matrix B of the same dimensionality
B = 2×3
0.9897 0.6145 0.6273 0.0900 0.5769 0.7224
C = A.*B % Multiply each element by the corresponding element with .*
C = 2×3
0.7571 0.1555 0.1472 0.0757 0.5693 0.0257
This is what Simulink does by default.
Second and third methods: K*u and u*K
If you want to do a matrix multiplication on A (see this simple explanation or this more formal explanation), then how you're multiplying by the Gain block constant (K) matters. For matrix multiplication, the two inputs A and B can have different sizes, following this rule:
For A having dimension and B having dimension , requires and produces an output with dimension .
Back to the gain block
So if you use elementwise multiplication in your Gain block A, your (presumably) 2x1 Integrator output will be multiplied by a 2x2 matrix A. Simulink can't perform elementwise multiplication unless the two arrays have the same dimensionality, so it will fail. This applies to the other gain blocks in the design as well. You'll want to left-multiply: K*u.
One more thing. You will have to set the initial conditions in the integrator to a 2x1 vector as well. Otherwise your integrator doesn't know what dimensionality it's supposed to have.
I'll try to put a more condensed version of this in as an answer.
Munna
Munna on 9 Aug 2024
function [M,V,G,F] = fcn(q3,q4,qd1, qd2, qd3, qd4, qd5)
%#codegen
% Parameters considered in the manipulator.
l2 = 0.2; l3 = 0.2; l4 = 0.2;
lc2 = 0.0229; lc3 = 0.0229; lc4 = 0.02229;
m1 = 1.228; m2 = 1.023; m3 = 1.023; m4 = 1.023; m5 = 0.5114;
I2zz = 0.0058; I3zz = 0.0058; I4zz = 0.0058;
Fv1 = 0.03; Fv2 = 0.025; Fv3 = 0.025; Fv4 = 0.025; Fv5 = 0.02;
gz = 9.8;
% Inertia Matrix M
M11 = m1 + m2 + m3 + m4 + m5;
M12 = 0; M21 = 0; M13 = 0; M31 = 0; M41 = 0; M14 = 0; M25 = 0;
M35 = 0; M53 = 0; M52 = 0; M45 = 0; M54 = 0;
M15 = -m5;
M51 = -m5;
M22 = lc2^2*m2 + (l2^2 + lc3^2 + 2*l2*lc3*cos(q3))*m3 + ...
(l2^2 + l3^2 + lc4^2 + 2*l2*l3*cos(q3))*m4 + ...
2*(l3*lc4*cos(q4) + l2*lc4*cos(q3+q4))*m4 + ...
(l2^2 + l3^2 + l4^2 + 2*l2*l3*cos(q3))*m5 + ...
2*(l3*l4*cos(q4) + l2*l4*cos(q3+q4))*m5 + I2zz + I3zz + I4zz;
M23 = (lc3^2 + l2*lc3*cos(q3))*m3 + ...
(l3^2 + lc4^2 + l2*l3*cos(q3) + 2*l3*lc4*cos(q4))*m4 + ...
(l2*lc4*cos(q3+q4))*m4 + ...
(l2*l4*cos(q3+q4))*m5 + ...
(l3^2 + l4^2 + l2*l3*cos(q3) + 2*l3*l4*cos(q4))*m5 + I3zz + I4zz;
M32 = M23; % Symmetric property
M24 = (lc4^2 + l3*lc4*cos(q4) + l2*lc4*cos(q3+q4))*m4 + ...
(l4^2 + l3*l4*cos(q4) + l2*l4*cos(q3+q4))*m5 + I4zz;
M42 = M24; % Symmetric property
M33 = lc3^2*m3 + (l3^2 + lc4^2 + 2*l3*lc4*cos(q4))*m4 + ...
(l3^2 + lc4^2 + 2*l2*l4*cos(q4))*m5 + I3zz + I4zz;
M34 = (lc4^2 + l3*lc4*cos(q4))*m4 + (l4^2 + l3*l4*cos(q4))*m5 + I4zz;
M43 = M34; % Symmetric property
M44 = lc4^2*m4 + l4^2*m5 + I4zz;
M55 = m5;
M = [M11 M12 M13 M14 M15; M21 M22 M23 M24 M25; ...
M31 M32 M33 M34 M35; M41 M42 M43 M44 M45; ...
M51 M52 M53 M54 M55];
% Coriolis and Centrifugal Matrix V
Vm11 = 0; Vm12 = 0; Vm13 = 0; Vm14 = 0; Vm15 = 0; Vm21 = 0; Vm25 = 0;
Vm31 = 0; Vm35 = 0; Vm41 = 0; Vm44 = 0; Vm45 = 0; Vm51 = 0; Vm52 = 0;
Vm53 = 0; Vm54 = 0; Vm55 = 0;
Vm22 = -(lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd3 + ...
(l2*sin(q3+q4)*qd3 - (l3*sin(q4) + l2*sin(q3+q4)*qd4)*(lc4*m4 + l4*m5));
Vm23 = -(lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd2 - ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd2 - ...
((lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd3) - ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd3 - ...
((lc4*m4 + l4*m5)*(l3*sin(q4) + l2*sin(q3+q4))*qd4);
Vm24 = -(lc4*m4 + l4*m5)*(l3*sin(q4) + l2*sin(q3+q4)*(qd2 + qd3 + qd4));
Vm32 = (lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd2 + ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd2 - ...
(lc4*m4 + l4*m5)*l3*sin(q4)*qd4;
Vm33 = -(lc4*m4 + l4*m5)*l3*sin(q4)*qd4;
Vm34 = -(lc4*m4 + l4*m5)*l3*sin(q4)*(qd2 + qd3 + qd4);
Vm42 = (lc4*m4 + l4*m5)*(l2*sin(q3+q4) + l3*sin(q4))*qd2 + ...
(lc4*m4 + l4*m5)*l3*sin(q4)*qd3;
Vm43 = (lc4*m4 + l4*m5)*l3*sin(q4)*(qd2 + qd3);
V = [Vm11 Vm12 Vm13 Vm14 Vm15; Vm21 Vm22 Vm23 Vm24 Vm25; ...
Vm31 Vm32 Vm33 Vm34 Vm35; Vm41 Vm42 Vm43 Vm44 Vm45; ...
Vm51 Vm52 Vm53 Vm54 Vm55];
% Friction Vector F and Gravity Vector G
F = [Fv1*qd1 Fv2*qd2 Fv3*qd3 Fv4*qd4 Fv5*qd5];
G = [(m1 + m2 + m3 + m4 + m5)*gz ; 0 ; 0; 0; -m5 * gz].';
end
what is eroor in this code

Sign in to comment.

Accepted Answer

Daniel
Daniel on 14 Apr 2023
  1. It looks like you're using elementwise multiplication in the Gain blocks. If you're doing matrix math, you have to choose a matrix multiplication method for the gain blocks. You'll want to choose "Matrix (K*u)".
  2. I'm guessing that you haven't modified the initial condition for your integrator. You'll need to set that to a 2x1 vector as well (say, [0;0]).
  3. Finally, I think you should pass in your constant ref as a column vector rather than a row vector.
Once you've made the dimensionality and mathematics consistent, I think the system should run.
Some optimizations to bypass some of these headaches:
  1. You could remove the A and C Gain blocks (Gain and Gain2).
  2. You could disconnect y from Sum1, if D is 0 anyway, and then you could get rid of Sum1 and Gain1.
Then you would only be left with the mathematically nontrivial Gain blocks (Gain3 and Gain4).

More Answers (1)

Munna
Munna on 9 Aug 2024
function [M,V,G,F] = fcn(q3,q4,qd1, qd2, qd3, qd4, qd5)
%#codegen
% Parameters considered in the manipulator.
l2 = 0.2; l3 = 0.2; l4 = 0.2;
lc2 = 0.0229; lc3 = 0.0229; lc4 = 0.02229;
m1 = 1.228; m2 = 1.023; m3 = 1.023; m4 = 1.023; m5 = 0.5114;
I2zz = 0.0058; I3zz = 0.0058; I4zz = 0.0058;
Fv1 = 0.03; Fv2 = 0.025; Fv3 = 0.025; Fv4 = 0.025; Fv5 = 0.02;
gz = 9.8;
% Inertia Matrix M
M11 = m1 + m2 + m3 + m4 + m5;
M12 = 0; M21 = 0; M13 = 0; M31 = 0; M41 = 0; M14 = 0; M25 = 0;
M35 = 0; M53 = 0; M52 = 0; M45 = 0; M54 = 0;
M15 = -m5;
M51 = -m5;
M22 = lc2^2*m2 + (l2^2 + lc3^2 + 2*l2*lc3*cos(q3))*m3 + ...
(l2^2 + l3^2 + lc4^2 + 2*l2*l3*cos(q3))*m4 + ...
2*(l3*lc4*cos(q4) + l2*lc4*cos(q3+q4))*m4 + ...
(l2^2 + l3^2 + l4^2 + 2*l2*l3*cos(q3))*m5 + ...
2*(l3*l4*cos(q4) + l2*l4*cos(q3+q4))*m5 + I2zz + I3zz + I4zz;
M23 = (lc3^2 + l2*lc3*cos(q3))*m3 + ...
(l3^2 + lc4^2 + l2*l3*cos(q3) + 2*l3*lc4*cos(q4))*m4 + ...
(l2*lc4*cos(q3+q4))*m4 + ...
(l2*l4*cos(q3+q4))*m5 + ...
(l3^2 + l4^2 + l2*l3*cos(q3) + 2*l3*l4*cos(q4))*m5 + I3zz + I4zz;
M32 = M23; % Symmetric property
M24 = (lc4^2 + l3*lc4*cos(q4) + l2*lc4*cos(q3+q4))*m4 + ...
(l4^2 + l3*l4*cos(q4) + l2*l4*cos(q3+q4))*m5 + I4zz;
M42 = M24; % Symmetric property
M33 = lc3^2*m3 + (l3^2 + lc4^2 + 2*l3*lc4*cos(q4))*m4 + ...
(l3^2 + lc4^2 + 2*l2*l4*cos(q4))*m5 + I3zz + I4zz;
M34 = (lc4^2 + l3*lc4*cos(q4))*m4 + (l4^2 + l3*l4*cos(q4))*m5 + I4zz;
M43 = M34; % Symmetric property
M44 = lc4^2*m4 + l4^2*m5 + I4zz;
M55 = m5;
M = [M11 M12 M13 M14 M15; M21 M22 M23 M24 M25; ...
M31 M32 M33 M34 M35; M41 M42 M43 M44 M45; ...
M51 M52 M53 M54 M55];
% Coriolis and Centrifugal Matrix V
Vm11 = 0; Vm12 = 0; Vm13 = 0; Vm14 = 0; Vm15 = 0; Vm21 = 0; Vm25 = 0;
Vm31 = 0; Vm35 = 0; Vm41 = 0; Vm44 = 0; Vm45 = 0; Vm51 = 0; Vm52 = 0;
Vm53 = 0; Vm54 = 0; Vm55 = 0;
Vm22 = -(lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd3 + ...
(l2*sin(q3+q4)*qd3 - (l3*sin(q4) + l2*sin(q3+q4)*qd4)*(lc4*m4 + l4*m5));
Vm23 = -(lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd2 - ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd2 - ...
((lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd3) - ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd3 - ...
((lc4*m4 + l4*m5)*(l3*sin(q4) + l2*sin(q3+q4))*qd4);
Vm24 = -(lc4*m4 + l4*m5)*(l3*sin(q4) + l2*sin(q3+q4)*(qd2 + qd3 + qd4));
Vm32 = (lc3*m3 + l3*m4 + l3*m5)*l2*sin(q3)*qd2 + ...
(lc4*m4 + l4*m5)*l2*sin(q3+q4)*qd2 - ...
(lc4*m4 + l4*m5)*l3*sin(q4)*qd4;
Vm33 = -(lc4*m4 + l4*m5)*l3*sin(q4)*qd4;
Vm34 = -(lc4*m4 + l4*m5)*l3*sin(q4)*(qd2 + qd3 + qd4);
Vm42 = (lc4*m4 + l4*m5)*(l2*sin(q3+q4) + l3*sin(q4))*qd2 + ...
(lc4*m4 + l4*m5)*l3*sin(q4)*qd3;
Vm43 = (lc4*m4 + l4*m5)*l3*sin(q4)*(qd2 + qd3);
V = [Vm11 Vm12 Vm13 Vm14 Vm15; Vm21 Vm22 Vm23 Vm24 Vm25; ...
Vm31 Vm32 Vm33 Vm34 Vm35; Vm41 Vm42 Vm43 Vm44 Vm45; ...
Vm51 Vm52 Vm53 Vm54 Vm55];
% Friction Vector F and Gravity Vector G
F = [Fv1*qd1 Fv2*qd2 Fv3*qd3 Fv4*qd4 Fv5*qd5];
G = [(m1 + m2 + m3 + m4 + m5)*gz ; 0 ; 0; 0; -m5 * gz].';
end
error in this code

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!