MATLAB: I’m having difficulty getting this code to work, could someone explain how to fix this “In an assignment A(I) = B, the number of elements in B and I must be the same. ” error

a(i) = bMATLABnumber of elements

x0=0;
y0=5;
v0=10;
theta = 35;
g = 9.81; % Gravitational constant
vx0 = v0*cosd(theta); % Inital velocity in the x direction
vy0 = v0*sind(theta); % Inital velocity in the y direction
T = 2*vy0/g ; % Time in flight (y0 = 0)
Hmax = ((v0^2*(sin(theta))^2)/(2*g) + y0); % Maximun height of projectile
VLength = 1000;
x = zeros(VLength,1);
x(1) = x0;
y = zeros(VLength,1);
y(1) = y0;
t = zeros(VLength,1);
t(1) = 0.02;
for k = 2:VLength; % Starts the for loop
x(k) = v0*t*cosd(theta) + x0; % Calculating x direction
y(k) = v0*t*sind(theta) - 1/2*g*t^2 +y0; % Calculating y direction
t(k) = 0.2*(k-1);
if y < 0
break
end
end

Best Answer

x(k) = v0*t*cosd(theta) + x0; uses t, which you have set to zeros(VLength,1) so t is a vector. You are therefore building a vector on the right hand side, and you are trying to store it as a single element x(k)
You have the same problem on the next line.