MATLAB: Index out of bounds because numel(x)=1.

for loopindex-error

I am trying to graph velocity and since I am calculating the position at each time I thought I could just do this:
for t=2:40
theta = v*t;
alfa = asin(C*sin(theta)/CL);
x=C*cos(theta)+CL*cos(alfa);
dv(t)= (x(t)-x(t-1))/t;
end
plot (theta, dv);
but I keep getting an index out of bounds error. Originally I was pulling from the first entry of a vector but when that didn't work I just called it a new variable x, but it didn't seem to help.

Best Answer

You may proceed like this:
clc; clear all ;
N = 40 ;
v = rand(1);
C = rand(1) ;
CL = rand(1) ;
% initialize
theta = zeros(N,1) ;
x = zeros(N,1) ;
dv = zeros(N,1) ;
% for t = 1
theta(1) = v*1 ; % t= 1
alfa = asin(C*sin(theta(1))/CL);
x(1)=C*cos(theta(1))+CL*cos(alfa);
% loop
for t=2:40
theta(t) = v*t;
alfa = asin(C*sin(theta(t))/CL);
x(t)=C*cos(theta(t))+CL*cos(alfa);
dv(t)= (x(t)-x(t-1))/t;
end
plot (theta, dv);