a code that compute numbers and numbers does not !

2 ビュー (過去 30 日間)
basim almutairi
basim almutairi 2021 年 9 月 15 日
回答済み: Sreedhar Arumugam 2021 年 9 月 15 日
greetings all,
the following code I've created to compute an amount. whenever I try number of years let's say 2 it will compute correctly. However, when I want to compute something like 10 years it wont work ! please can you adivse
p = input('enter the amount you want to deposit initially:');
n = input('enter number of years:');
t = input(['enter the frequency of interest compounding\n'...
'type 1: for annually\n'...
'type 2: for semi annually\n'...
'type 4: for quarterly\n'...
'type 12: for monthly\n']);
r = input('enter the interest rate:');
switch n
case (1)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['annually:',num2str(a)]);
case (2)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['semi annually:',num2str(a)]);
case (4)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['quarterly:',num2str(a)]);
case (12)
a = p*((1+((0.01*r)/t))^(n*t));
disp(['monthly:',num2str(a)]);
end

回答 (1 件)

Sreedhar Arumugam
Sreedhar Arumugam 2021 年 9 月 15 日
I am assuming you want to compute interest based on the frequency of compounding.
Your switch statement is with respect to n (number of years) when it should actually be with respect to t (frequency of compounding).
switch t
case(1)
case (2)
case (4)
case(12)
end
This is why it fails for n=10 in your code as there is no case corresponding for this input.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by