MATLAB: Index exceeds matrix dimensions help

array

I am writing a script for determining the displacement and velocity of an arm on a complex slider system relative to the angle of rotation of the first slider. I am trying to plot the displacement and velocity on the same graph, to a precision of 0.001rad intervals. Displacement is working perfectly, but my velocity array is coming up as "Index exceeds matrix dimensions" which I cannot figure out given I have used the same size array to store the output values.
I have also played around with making the array much larger but still get the same message.
Code attached, error "Index exceeds matrix dimensions" in line 43 "Yv=(y(n+1)-(y(n)))/0.0005;"
Thanks for any help

Best Answer

There are so many issues. Let's address your error first. Your counter variable n is bigger than the size of your y vector (on this line, Yv=(y(n+1)-(y(n)))/0.0005;). Hence you get the error.
Nevertheless you don't really calculate velocity. You simply initialize v vector with zeros and and trying to calculate only one element (because m is just 1 and you don't have a loop to calculate the other elements).
If you want to carry out your calculations with a loop (Matlab is much more powerful, no loops are needed for your problem though), simply use one loop with a counter variable like below.
your constants here
Theta=0:0.001:(2*pi);
numOfElements = numel(Theta);
y = zeros(1,numOfElements);
v = zeros(1,numOfElements);
for count = 1:numOfElements
y(count) = something like a*theta(count)*bla*blabla
v(count) = something else
end