# Why do i get the error "Dimensions of arrays being concatenated are not consistent"

1 view (last 30 days)
Umut Ayyildiz on 23 Mar 2021
Commented: Umut Ayyildiz on 23 Mar 2021
Why do I get this error for one but not the other even though the two codes are the exact same.
This one runs fine:
clear all; close all
% Initial Conditions
th= 0.01*pi/180; % initial theta position (rad)
w=0; % inital theta velocity (rad/s)
g=9.81; %gravity constant (m/s^2)
L=0.3; % length of bar (m)
mB=2; % mass of bar (kg)
mA=1; %mass of collar (kg)
dt=.001;
I = (mB*L^2)/12;
Wa=mA*g;
Wb=mB*g;
x=(mB*L)/2;
z=(L/2);
for ii = 1:5000
A = [-1 0 0 -mA 0
0 -1 1 0 0
1 0 0 -mB -x*cos(th)
0 1 0 0 x*sin(th)
sin(th)*z -cos(th)*z 0 0 -I];
B = [0; Wa; x*(w)^2*sin(th); Wb-x*(w)^2*cos(th); 0];
X=inv(A)*B;
w = w + X(5)*dt;
th=th + w*dt;
time(ii)= ii*dt;
theta(ii)=th;
alpha(ii)= X(5);
omega(ii)=w;
Rx(ii)=X(1);
Ry(ii)=X(2);
N(ii)=X(3);
d_acc(ii)=X(4);
end
But this one gives me an error:
clear all; close all
% Initial Conditions
theta= 0.01*pi/180; % initial theta position (rad)
theta_V=0; % inital theta velocity (rad/s)
g=9.81; %gravity constant (m/s^2)
L=0.3; % length of bar (m)
mB=2; % mass of bar (kg)
mA=1; %mass of collar (kg)
dt=.001;
I = (mB*L^2)/12;
Wa=mA*g;
Wb=mB*g;
x=(mB*L)/2;
z=(L/2);
for i = 1:5000
A = [-1 0 0 -mA 0;
0 -1 1 0 0;
1 0 0 -mB (-x*cos(theta))
0 1 0 0 x*sin(theta)
(sin(theta)*z) (-cos(theta)*z) 0 0 -I];
B = [0; Wa; x*(theta_V)^2*sin(theta); Wb-x*(theta_V)^2*cos(theta); 0];
X=inv(A)*B;
theta_V = theta_V + X(5)*dt;
theta=theta + theta_V*dt;
time(i)= i*dt;
theta(i)=theta;
theta_a(i)= X(5);
omega(i)=theta_V;
Rx(i)=X(1);
Ry(i)=X(2);
N(i)=X(3);
d_acc(i)=X(4);
end
I was changing my code from the one that worked because I realized I messed up my dynamic equations and I ended up with the same error I mention in the title When I was trying to debug it by backtracking I realized that even my original code was giving me an error. I had another file saved with my orignal code and it runs.

William on 23 Mar 2021
The problem in the second program is the statement 'theta(i) = theta', which is changing theta from a scalar into a vector. Then on the second iteration for the for-loop, the matrix A has some elements that are now vectors.
Umut Ayyildiz on 23 Mar 2021
I see what you mean, in the first code theta is being saved into another variable so when it goes to the next iteration it isnt a vector but still a scalar. Thank you!