For Loop Backwards Help

131 visualizzazioni (ultimi 30 giorni)
WhatIsMatlab-
WhatIsMatlab- il 7 Feb 2016
Commentato: Star Strider il 15 Mag 2022
This is my code. And these are the values it produces.
0 5.0000 9.6000 13.8320 17.7254 21.3074 24.6028 27.6346 30.4238 32.9899 35.3507
x = zeros(1,10);
for i = 1:10
x(i+1) = x(i) + ((5 - (4/50)*x(i)));
end
display(x)
I want the code to now run backwards now. I want to start at the 35.3507 value and run backwards now 10 times. But I cannot seem to make it work. Any help would be great.
Thanks

Risposta accettata

Star Strider
Star Strider il 7 Feb 2016
Here you go:
x = zeros(1,11);
for i = 11:-1:2
x(i-1) = x(i) + ((5 - (4/50)*x(i)));
end
display(x)
x =
35.351 32.99 30.424 27.635 24.603 21.307 17.725 13.832 9.6 5 0
You have to set the loop up to decrement, begin with 11, stop at 2, and decrement the index reference on the LHS of the assignment in the loop.
  8 Commenti
Chakradhar Kakumani
Chakradhar Kakumani il 15 Mag 2022
helped a lot, thanks!
Star Strider
Star Strider il 15 Mag 2022
My pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by