Why is my code only outputting one value instead of a vector?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matt Stone
am 31 Jan. 2021
Kommentiert: Star Strider
am 31 Jan. 2021
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.
0 Kommentare
Akzeptierte Antwort
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.
2 Kommentare
Weitere Antworten (0)
Siehe auch
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!