a code that compute numbers and numbers does not !

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by