Simulink function giving an error of incorrect dimensions of matrix multiplication. Using MATLAB 2021b.

function op = fcn(ip)
mass =5 ; % mass of block
x = 0.3; y = 0.2; z = 0.1; % dimensions of block
Jx = (mass / 12) * (y^2 + z^2);
Jy = (mass / 12) * (x^2 + z^2);
Jz = (mass / 12) * (x^2 + y^2);
Jxz = 0;
J = [Jx 0 -Jxz; 0 Jy 0; -Jxz 0 Jz];
% Parse Input
OMG = ip(1:3) ;
EUL = ip(4:6) ;
TRQ = ip(7:9);
P_hi = EUL(1);
t_heta = EUL(2);
p_si = EUL(3);
P = OMG(1);
Q = OMG(2);
R = OMG(3);
OMGG = [0 -R Q; R 0 P; -Q P 0];
H = [1 tan(t_heta)*sin(P_hi) tan(t_heta)*cos(P_hi);
0 cos(P_hi) -sin(P_hi);
0 sin(P_hi)/cos(t_heta) cos(P_hi)/cos(t_heta)];
EUL_dot = H*OMG;
OMG_dot = J\ (TRQ - OMGG*J*OMG);
op = [OMG_dot; EUL_dot];
end
ERROR. Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.

8 Comments

is ip a row vector or a column vector? It would make a difference to the size of some of the variables.
Does the error occur on the first simulation step or later?
You can debug a Matlab Function just like any other m-function. Set a breakpoint at the first executable line, and then check the dimensions of the variables on each line before stepping to the next line.
I'm surprised the error message doesn't give you the exact line number ....
This will be painful if the error shows up more than a few steps into the simulation.
Or does this error show up when you Update the model?
Dear Paul, The error occurs on the first simulation step.
The line number given by the error message is
Function 'MATLAB Function' (#210.569.578), line 26, column 11: "(H)*(OMG)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'. Function 'MATLAB Function' (#210.606.615), line 27, column 27: "(J)*(OMG)"
Because the error occurs on the first step, just set a breakpoint at the first line and step through line by line checking dimensions as you go to see where the problem is, which will shed light on a solution.
And/or you might try explicitly setting the vector variables at the top of the code, like:
OMG = zeros(3,1);
(and similarly for the others) so that the code generator will know the dimensions, and then fill in the actual values as currently coded.
i have performed all these steps, but stil same error. All matrices sizes are perfect as per the multiplication laws of matrices. still giving error:
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'. Function 'MATLAB Function' (#249.797.804), line 33, column 15: "H * OMG" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'. Function 'MATLAB Function' (#249.839.846), line 34, column 34: "J * OMG" Launch diagnostic report.
is it some MATLAB Version error or what

Sign in to comment.

Answers (0)

Categories

Asked:

on 7 Aug 2023

Commented:

on 8 Aug 2023

Community Treasure Hunt

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

Start Hunting!