How do I get a loop that takes a sum to store each value in a vector?

3 ビュー (過去 30 日間)
Adam Palmer
Adam Palmer 2014 年 8 月 5 日
コメント済み: Star Strider 2014 年 8 月 5 日
Hey Matlab community, y'all have been very helpful. I got another one for ya. I have a potential formula that takes the sum of series I made. What I want is each sum to be stored in a vector after each iteration, so that I can plot it. heres my code. Also I think it requires an empty vector of correct dimensions such as zeros(blah,blah) but I must have done it wrong because it returns empty
sumv=0;
n=0;
an=0;
for k=1:120
an=.68+sumv-(.68+sumv).*.0693;
sumv=an;
end
Your help and suggestions are greatly appreciated

採用された回答

Star Strider
Star Strider 2014 年 8 月 5 日
編集済み: Star Strider 2014 年 8 月 5 日
If you want the value of sumv stored at each iteration of your k loop, index it to create a vector from it:
sumv(1)=0;
n=0;
an=0;
for k=1:120
an=.68+sumv(k)-(.68+sumv(k)).*.0693;
sumv(k+1)=an;
end
figure(1)
plot(sumv)
grid
You could preallocate by putting:
sumv = zeros(1,121);
in place of:
sumv(1)=0;
That’s good programming practice, but here it likely doesn’t make much of a difference in terms of speeding up your loop. I decided to keep with your original construction rather than preallocate.
  2 件のコメント
Adam Palmer
Adam Palmer 2014 年 8 月 5 日
Alright, I gotcha. Thanks Star Strider!
Star Strider
Star Strider 2014 年 8 月 5 日
My pleasure!
(Your ‘y’all’ got my attention. W&L and UVA here...)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by