Why is my code only outputting one value instead of a vector?

5 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Star Strider
Star Strider am 31 Jan. 2021
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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by