In: Mechanical Engineering
Use the switch structure to write a MATLAB program to compute the amount of money that accumulates in a savings account in one year. The program should accept the following input: the initial amount of money deposited in the account; the frequency of interest compounding (monthly, quarterly, semiannually, or annually); and the interest rate. Run your program for a $1000 initial deposit for each case; use a 5 percent interest rate. Compare the amounts of money that accumulate for each case.
It is required to use the switch feature to find the amount accumulated for each frequency of interest gained (monthly, quarterly, semi-annually or annually)
It is needed to take user input for interest rate and the initial amount of money. The user input can be taken by using the command input.
Formula for amount is
A = P(1 + r/100n)n
n = 1, 2, 4, 12
The MATLAB code is given below:
Input:
% asking the user for principal amount
P = input('Please input initial amount deposited: ');
% asking the user for coumpounding frequency
n = input(['Please input the frequency of interest \n' ...
'Type 1: For Annually \n' ...
'Type 2: For Semi Annually \n' ...
'Type 4: For quaterly \n' ...
'Type 12: For monthly \n']);
% asking the user for rate of interest
r = input('Please input the interest rate: ');
% taking n as switch
switch n
case (1)
Amount = P*(1 + (0.01*r/n))^(n);
disp(['Yearly: ', num2str(Amount)]);
case (2)
Amount = P*(1 + (0.01*r/n))^(n);
disp(['Semi Annually: ', num2str(Amount)]);
case(4)
Amount = P*(1 + (0.01*r/n))^(n);
disp(['Quaterly: ', num2str(Amount)]);
case(12)
Amount = P*(1 + (0.01*r/n))^(n);
disp(['Monthly: ', num2str(Amount)]);
end
Output:
Please input initial amount deposited: 1000
Please input the frequency of interest
Type 1: For Annually
Type 2: For Semi Annually
Type 4: For quarterly
Type 12: For monthly
12
Please input the interest rate: 5
Monthly: 1051.1619
The case was run for monthly compounding. Similarly, it was run for other frequencies also the results are shown below:
Frequency |
Amount |
Yearly |
$ 1,050 |
Bi-Annually |
$ 1,050.625 |
Quarterly |
$ 1,050.9463 |
Monthly |
$ 1,051.16 |
It is required to use the switch feature to find the amount accumulated for each frequency