I need to make a function for compound interest using for

4 ビュー (過去 30 日間)
Matthew
Matthew 2024 年 4 月 19 日
回答済み: Dyuman Joshi 2024 年 4 月 19 日
I am trying to form a conditional loop to show the growth of money, I put in $1000 and want it to grow by 8% every year for 8 years, but I keep returning the same values.
money=1000;
x=zeros(1,10);
for i=1:10
x(i)=money*1.08
end

採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 4 月 19 日
In case you want the final output -
format shortg
money=1000;
for k=1:10
money=money*1.08;
end
money
money =
2158.9
In case you want the output for each year -
n = 10;
money = 1000*ones(1,n+1);
for k=1:n
money(k+1) = money(k)*1.08;
end
disp(money)
1000 1080 1166.4 1259.7 1360.5 1469.3 1586.9 1713.8 1850.9 1999 2158.9

その他の回答 (1 件)

Voss
Voss 2024 年 4 月 19 日
編集済み: Voss 2024 年 4 月 19 日
money=1000;
x=zeros(1,8); % x is the balance each year for 8 years
x(1) = money; % initially (1st year) x is 1000
for i=2:numel(x) % loop over years 2 through the end
x(i)=x(i-1)*1.08 % each year's balance is 8% more than the previous year's
end
x = 1x8
1000 1080 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 1.2597 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 1.2597 1.3605 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 1.2597 1.3605 1.4693 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 1.2597 1.3605 1.4693 1.5869 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1x8
1.0e+03 * 1.0000 1.0800 1.1664 1.2597 1.3605 1.4693 1.5869 1.7138
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by