MATLAB: Unable to perform assignment because the left and right sides have a different number of elements. Line “u(i) = u(I+1) + acceleration * dt” seems to be the problem.

error

How can I fix this code to run and print the table?
% Default parameter values
mass = 1;
Spring = 200;
Initial = 1;
staticf = 0.2;
dynamicf = 0.1;
Period = 7;
dt = 0.01;
angle = 30;
g = 9.81;
i = 3;
u(i-2) = 0;
u(i-1) = 0;
displacement_data(i-1) = 0;
for time = 0:dt:Period
if u(i-1) > 0
multiplier = -1;
else
multiplier = 1;
end
if u(i-2) <= 0 && u(i-1) >= 0
fres = (mass*g*(sind(angle)) - (staticf*cosd(angle)));
elseif (u(i-2) >= 0 && u(i-1) >= 0 && displacement_data(i-1) < Initial) || (u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) < Initial)
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle)));
elseif u(i-2) >= 0 && u(i-1) <= 0
fres = mass*g*(sind(angle) + staticf*cosd(angle)) - spring*(displacement_data(i-1) - Initial);
elseif u(i-2) >=0 && u(i-1) >= 0 && displacement_data(i-1) >= Initial || u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) >= Initial
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle))) - spring*(displacement_data(i-1) - Initial);
end
acceleration = fres/mass;
u(i) = u(i-1) + (acceleration * dt);
displacement_data(i) = displacement_data(i-1) + u(i) * dt;
i = i + 1;
end
fprintf('displacement\t\tacceleration\t\ttime\t\tvelocity\n')
for(i=3: length(u-3))
fprintf('%.4f\t\t%.4f\t\t%.4f\t\t%.4f\n', displacement_data(i-1), acceleration_data(i-1), time_data(i), u(i-1));
end

Best Answer

When I ran your code, I got the same error. I tracked it down to your use of ‘spring’ as a variable name, as well as noting that in your ‘fres’ assignments that use it, you did not initially capitalise it.. This is actually the name of a colormap, so was creating a (64x3) matrix for your variable, and that was throwing the error. I corrected it to ‘Spring’ everywhere, and still got the error. What I find interesting is that as a general rule, MATLAB is case-sensitive, so ‘Spring’ and ‘spring’ should not be the same. (I am running this on R2019a.) I finally changed it to ‘SpringK’ everywhere, and the error disappeared.
The changes are:
SpringK = 200;
and:
if u(i-2) <= 0 && u(i-1) >= 0
fres = (mass*g*(sind(angle)) - (staticf*cosd(angle)));
elseif (u(i-2) >= 0 && u(i-1) >= 0 && displacement_data(i-1) < Initial) || (u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) < Initial)
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle)));
elseif u(i-2) >= 0 && u(i-1) <= 0
fres = mass*g*(sind(angle) + staticf*cosd(angle)) - SpringK*(displacement_data(i-1) - Initial);
elseif u(i-2) >=0 && u(i-1) >= 0 && displacement_data(i-1) >= Initial || u(i-2) < 0 && u(i-1) < 0 && displacement_data(i-1) >= Initial
fres = mass*g*(sind(angle) + multiplier*(dynamicf*cosd(angle))) - SpringK*(displacement_data(i-1) - Initial);
end
We are left with one error:
Undefined function or variable 'acceleration_data'.
fprintf('%.4f\t\t%.4f\t\t%.4f\t\t%.4f\n', displacement_data(i-1), acceleration_data(i-1),
time_data(i), u(i-1));
I defer to you to solve that.
Related Question