Always preallocate vectors instead of growing them in a loop, so before the loop:
While this will get rid of the error it won't change the fact that for all n > 2 you're checking the M(n) value before you've actually put something into M(n). There's clearly something wrong with the logic of your loop.
Also, spot the difference:
xc = zeros(1,total_t);
yc = zeros(1,total_t);
V=zeros(1,length(total_t));
length(total_t) is 1, so V is a scalar.
Also, spot the repetition:
V(1) = V0
x(1) = 0
y(1) = 0
V(1) = V0
2 Comments
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/513290-index-exceeds-the-number-of-array-elements-2#comment_816216
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/513290-index-exceeds-the-number-of-array-elements-2#comment_816216
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/513290-index-exceeds-the-number-of-array-elements-2#comment_816218
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/513290-index-exceeds-the-number-of-array-elements-2#comment_816218
Sign in to comment.