MATLAB: Is the code only outputting one value instead of a vector

loopvectors

v_0=input('Enter the inital Velocity');
g=input('Enter Gravity Constant');
for t=0:0.05:(pi/2)
r=(v_0^2/g)*sin(2*t)%#ok<NOPTS>
end
I am newer to matlab so I am unsure of why this is happening.

Best Answer

That is because you did not index ‘r’.
Either do this (without the loop):
t=0:0.05:(pi/2)
r=(v_0^2/g)*sin(2*t);%#ok<NOPTS>

or this (with the loop):
t=0:0.05:(pi/2);
for k = 1:numel(t)
r(k)=(v_0^2/g)*sin(2*t(k))%#ok<NOPTS>
end
Both will work, and both will produce the required result.
Related Question