MATLAB: Index exceeds the number of array elements(1)

for loop

initialVelocity = (3.0 : 0.05 : 3.5);
hold on
for i = 1 : length(initialVelocity)
[range, rangeAngle] = ProjectileRange2(d,initialVelocity(i));
scatter(initialVelocity(i), range(i), 'r*');
end
I am trying to scatter plot initalVelocity vs range and error says "index exceeds the number of array elements(1)" I cant figure out why

Best Answer

I cant figure out why
That is because although ‘i’ increments, the values of ‘range’ and ‘rangeAngle’ remain (1x1) scalars in every iteration of your loop.
Try this:
[range(i), rangeAngle(i)] = ProjectileRange2(d,initialVelocity(i));
Obviously this assumes that for every scalar value of ‘initialVelocity’ your ‘ProjectileRange’ function produces scalar values for each of the two outputs.
Also, take the scatter call out of the loop and put it after the loop:
figure
scatter(initialVelocity, range, 'r-*')
That should do what you want.
Make appropriate changes to get your desired result.