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

 採用された回答

Star Strider
Star Strider 2016 年 2 月 7 日

8 投票

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 件のコメント

WhatIsMatlab-
WhatIsMatlab- 2016 年 2 月 7 日
Thank you!! I see what I was doing wrong compared to yours.
Star Strider
Star Strider 2016 年 2 月 7 日
My pleasure!
If my Answer solved your problem, please Accept it.
WhatIsMatlab-
WhatIsMatlab- 2016 年 2 月 8 日
編集済み: WhatIsMatlab- 2016 年 2 月 8 日
I will accept it. But I have one more question what if I wanted to start the process at a certain value instead. What would I do?
I would like the value to start at like 40 and then count down from there.
Thanks
Star Strider
Star Strider 2016 年 2 月 8 日
If I understand correctly what you want to do, to start the loop from 40 and count down, just change the beginning loop index:
for i = 41:-1:2
To start at ‘N’, the loop index has to start at N+1 and stop at 2 because of the way the indexing works.
My pleasure.
WhatIsMatlab-
WhatIsMatlab- 2016 年 2 月 8 日
Yes that is correct as to what I want to do.
Star Strider
Star Strider 2016 年 2 月 8 日
My pleasure!
Chakradhar Kakumani
Chakradhar Kakumani 2022 年 5 月 15 日
helped a lot, thanks!
Star Strider
Star Strider 2022 年 5 月 15 日
My pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by