Why does the loop not work for the increment stated?

for t= 0:1:20
a= [t, sin(t), 0];
b= [0, 0, cos(t)];
v= a + b;
end
The loop only calculates the value of v for 20, not 0 to 20 as stated by the increment. What have I done wrong? I need it to calculate the vector v for numbers 0 all the way to 20.

 Réponse acceptée

Your code is essentially correct. If you want all the itermediate results, you need to index ‘v’:
for t= 0:1:20
a= [t, sin(t), 0];
b= [0, 0, cos(t)];
v(t+1,:)= a + b;
end

4 commentaires

Sorry but how would I do that? I changed the code to what you have shown but it doesn't work.
Stephen23
Stephen23 le 14 Nov 2018
Modifié(e) : Stephen23 le 14 Nov 2018
" I changed the code to what you have shown but it doesn't work."
What exactly does "it doesn't work" mean? I tried Star Strider's code and got this:
v =
0.00000 0.00000 1.00000
1.00000 0.84147 0.54030
2.00000 0.90930 -0.41615
3.00000 0.14112 -0.98999
4.00000 -0.75680 -0.65364
5.00000 -0.95892 0.28366
6.00000 -0.27942 0.96017
7.00000 0.65699 0.75390
8.00000 0.98936 -0.14550
9.00000 0.41212 -0.91113
10.00000 -0.54402 -0.83907
11.00000 -0.99999 0.00443
12.00000 -0.53657 0.84385
13.00000 0.42017 0.90745
14.00000 0.99061 0.13674
15.00000 0.65029 -0.75969
16.00000 -0.28790 -0.95766
17.00000 -0.96140 -0.27516
18.00000 -0.75099 0.66032
19.00000 0.14988 0.98870
20.00000 0.91295 0.40808
What do you expect to get?
I tried again and that's what I got. Thanks for the help.
@Stephen — Thank you.
@Hariyali Ramesh — If my Answer helped you solve your problem, please Accept it!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by