Unable to perform assignment because the left and right sides have a different number of elements. Error in Assignment1Motorbike (line 139)

2 views (last 30 days)
Hi all, I'm very new to MATLAB and need help. I've written the below code and keep getting the error: Unable to perform assignment because the left and right sides have a different number of elements.
Error in Assignment1Motorbike (line 139)
y1d(n) = ( -k1*( x1(n) - 0.9*x2(n) - x_f ) - k2*( x1(n) + 0.9*x2(n) - x_r ) + k3*( x3(n) - x1(n) - 0.4*x2(n) ) - c1*( x1d(n) - 0.9*x2d(n) - x_fd ) - c2*( x1d(n) + 0.9*x2d(n) - x_rd ) ) / m1;
Here's the code. Would really appreciate any help/advice/tip broken down for a MATLAB newbee like me to understand. Thanks.
close all
clear
clc
% Model parameters in SI Units
m1 = 180;
J = 50;
m2 = 80;
k1 = 12000;
k2 = 16000;
k3 = 2000;
c_up = 180;
c_down = 800;
c2 = 500;
Q = deg2rad(16.7); % 16.7 degrees is the angle of the slope of the trapezoidal road hump and is being converted to radians. Q will be used to calculate the vertical speed component at the wheels for dampers
% Bike Velocity
vb = 2.77778; %Equivalent to 10 km/h
% ODE Integrator/Solver settings for dynamic analysis
ts = 10; % simulation time
dt = 0.001; % time step size
% Initial conditions
%Displacement:
x1_0 = 0;
x2_0 = 0;
x3_0 = 0;
%Velocity
x1d_0 = 0;
x2d_0 = 0;
x3d_0 = 0;
% Dynamic analysis
%Calculate number of od required time steps (N)
N = ceil(ts/dt);
% Defining initial conditions
t(1) = 0;
x1(1) = x1_0;
x2(1) = x2_0;
x3(1) = x3_0;
y1(1) = x1d_0;
y2(1) = x2d_0;
y3(1) = x3d_0;
% Equations of motion
% Mode 1:
% Y1’ = (-K1(X1 – 0.9X2 – Xf) - K2(X1 +0.9X2 – Xb) + K3(X3 – X1 – 0.4X2) - C1(X’1 – 0.9X’2 – Xf’) - C2(X’1 + 0.9X’2 – Xb’))/M1
% Loop along time steps
for n = 1:N
% Solve for time step increments t(n+1)
t(n+1) = dt*n;
% Positions of the front and rear wheels P_f and and P_r respectively
p_f = t(n) * vb; % simulation time X bike velocity
p_r = p_f - 1.8; % the rear wheel trails the front wheel by the wheelbase distance
% Conditional statements to calculate road input based on the position of
% the wheels
% x_f is the road input at the front wheel and x_r is the road input at
% the rear wheel
% road input at the front wheel x_f ------ x_fd is the velocity of the
% road input at the front wheel, effectively the vertical component of
% the velocity of the bike vb
if p_f <= 0
x_f = 0;
x_fd = 0;
elseif p_f >= 0 && p_f < 0.4
x_f = 0.3*p_f;
x_fd = vb * ( sin(Q) );
elseif p_f >= 0.4 && p_f < 2
x_f = 0.12;
xf_d = 0;
elseif p_f >=2 && p_f <= 2.4
x_f = 0.72 - 0.3*p_f;
x_fd = vb * ( sin(Q) );
end
% road input at the rear wheel x_r
% x_rd is the velocity of the road input at the rear wheel, effectively the vertical component of
% the constant velocity of the bike vb
if p_r <= 0
x_r = 0;
x_rd = 0;
elseif p_r >= 0 && p_r < 0.4
x_r = 0.3*p_r;
x_rd = vb * ( sin(Q) );
elseif p_r >= 0.4 && p_r < 2
x_r = 0.12;
x_rd = 0;
elseif p_r >= 2 && p_r <= 2.4
x_r = 0.72 - 0.3*p_r;
x_rd = vb * diff( sin(Q) );
end
% Front damper selection
if y1(n) >= 0 && y2(n) >= 0 && y3(n) >=0
c1 = c_up;
elseif y1(n) < 0 && y2(n) < 0 && y3(n) < 0
c1 = c_down;
end
% Calculating xdot and ydot
% Y1' = (-K1(X1 – 0.9X2 – Xf) - K2(X1 +0.9X2 – Xr) + K3(X3 – X1 – 0.4X2) - C1(Xr1 – 0.9X'2 – Xf’) - C2(X’1 + 0.9X’2 – Xr’))/M1
% Y2' = (0.9K1(X1 – 0.9X2 – Xf) - 0.9K2(X1 +0.9X2 – Xr) - 0.4 K3(X3 – X1 – 0.4X2) + 0.9 C1(X'1 – 0.9X'2 – Xf') - 0.9 C2(X'1 + 0.9X'2 – Xr'))/J
% Y3’ = (-K3(X3 – X1 – 0.4X2))/M2
x1d(n) = y1(n);
x2d(n) = y2(n);
x3d(n) = y3(n);
y1d(n) = ( -k1*( x1(n) - 0.9*x2(n) - x_f ) - k2*( x1(n) + 0.9*x2(n) - x_r ) + k3*( x3(n) - x1(n) - 0.4*x2(n) ) - c1*( x1d(n) - 0.9*x2d(n) - x_fd ) - c2*( x1d(n) + 0.9*x2d(n) - x_rd ) ) / m1; % THIS IS THE LINE WHERE THE ERROR OCCURS
y2d(n) = ( 0.9*k1*( x1(n) - 0.9*x2(n) - x_f ) - 0.9*k2*( x1(n) + 0.9*x2(n) - x_r ) - 0.4*k3*( x3(n) - x1(n) - 0.4*x2(n) ) + 0.9*c1*( x1d(n) - 0.9*x2d(n) - x_fd ) - 0.9*c2*( x1d(n) + 0.9*x2d(n) - x_rd) )/J;
y3d(n) = ( -k3*(x3(n) - x1(n) - 0.4*x2(n)) )/m2;
% Euler equations
% (n+1) solution = existing variable state + slope x time step
x1(n+1) = x1(n) + dt*x1d(n);
x2(n+1) = x2(n) + dt*x2d(n);
x3(n+1) = x3(n) + dt*x3d(n);
y1(n+1) = y1(n) + dt*y1d(n);
y2(n+1) = y2(n) + dt*y2d(n);
y3(n+1) = y3(n) + dt*y3d(n);
end
Unable to perform assignment because the left and right sides have a different number of elements.
figure;
grid on
hold on
% x1 displacement
subplot(3,2,1)
plot(t,x1, 'b');
legend('x1')
xlabel('Time [s]')
ylabel('Displacement [m]')
grid on

Accepted Answer

Voss
Voss on 4 Nov 2023
The culprit is this line:
x_rd = vb * diff( sin(Q) );
Q is a scalar, so sin(Q) is a scalar, so diff(sin(Q)) is empty, so x_rd is empty after that line is executed.
Using empty x_rd in the line with the error produces an empty array on the right-hand side, which cannot be stored as an element of the array on the left-hand side.
The calculation of x_fd has a similar if/elseif block structure to the calculation of x_rd, but the relevant expression for x_fd does not have the diff, so it is ok. Should this expression for x_rd also be vb*sin(Q) instead of vb*diff(sin(Q))?
  2 Comments
Iregbu
Iregbu on 4 Nov 2023
Voss, thank you so much! You found the culprit for me. I used 'diff' in a previous calculation and forgot to take it out in the new calculation. Thanks a lot.
I've been trying to learn about arrays in matlab but keep running into online resources that overwhelm me. Any specific resource/article that you could recommend for a newbee? Would really appreciate.
Thanks again for your help!
Voss
Voss on 4 Nov 2023
You're welcome!
The best way to learn (for me) is to try things out and see how they work. Try simple versions of commands/syntax on the command-line to see how they work before putting them into your code.
Of course, the MATLAB Onramp is another a good place to start:

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 4 Nov 2023
x_rd is null in
y1d(n) = ( -k1*( x1(n) - 0.9*x2(n) - x_f ) - k2*( x1(n) + 0.9*x2(n) - x_r ) + k3*( x3(n) - x1(n) - 0.4*x2(n) ) - c1*( x1d(n) - 0.9*x2d(n) - x_fd ) - c2*( x1d(n) + 0.9*x2d(n) - x_rd ) ) / m1; % THIS IS THE LINE WHERE THE ERROR OCCURS
so the whole expression is bogus.
In general with something like this, break it up into smaller expressions so you can examine smaller, bite-sized chunks of it. Like
term1 = -k1*( x1(n) - 0.9*x2(n) - x_f )
term2 = - k2*( x1(n) + 0.9*x2(n) - x_r )
term3 = + k3*( x3(n) - x1(n) - 0.4*x2(n) )
term4 = - c1*( x1d(n) - 0.9*x2d(n) - x_fd )
term5 = - c2*( x1d(n) + 0.9*x2d(n) - x_rd )
y1d(n) = (term1 + term2 + term3 + term4 + term5) / m1
Something is wrong with term5 -- it's null. I'll leave it to you to debug it from there.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!