Nested if/else loop not stepping through all steps; why?
    12 views (last 30 days)
  
       Show older comments
    
Here is the problem I'm working on:
====================
A plumber opens a savings account with $100,000 at the beginning of January. He then
makes a deposit of $1000 at the end of each month for the next 12 months (starting at
the end of January). Interest is calculated and added to his account at the end of each
month (before the $1000 deposit is made). The monthly interest rate depends on the
amount A in his account at the time interest is calculated, in the following way:
A ≤ 1 10 000 : 1%
1 10 000 < A ≤ 1 25 000 : 1.5%
A > 1 25 000 : 2%
Write a program that displays, under suitable headings, for each of the 12 months, the
situation at the end of the month as follows: the number of the month, the interest rate,
the amount of interest, and the new balance. (Answer:Values in the last row of output
should be 12, 0.02, 2534.58, 130263.78).
====================
For now I'm ignoring the fact that interest is compounded BEFORE the monthly $1000 deposit and assuming it happens after. Here is my script:
====================
A=100000;
Monthly=1000;
Months=[1:12];
disp('MONTHS ELAPSED    INTEREST RATE     AMOUNT OF INTEREST      MONTH-END BALANCE')
   for k=1:length(Months)
       if A <= 110000;
          rate=1.01;
       elseif A <=125000;
           rate=1.015;
       else A>125000;
           rate=1.02;
       end
     Interest=((A+Monthly)*rate)-(A+Monthly);
     A=(A+Monthly)*rate;
     disp([Months(k)' rate'-1 Interest' A']) 
   end
====================
My output looks like this. Some of the calculations are wrong, but what I'm currently trying to figure out is why the interest rate goes from 0.01 to 0.02, never to 0.015. The book I'm using says that with relational operators in if/else statements, as soon as it encounters a true 'if' statement it skips down and goes back to the top again. How can this be possible if the values between 110000 and 125000 are never assigned to rate=0.015?
====================
MONTHS ELAPSED  INTEREST RATE   AMOUNT OF INTEREST  MONTH-END BALANCE
            1.00          0.01       1010.00     102010.00
            2.00          0.01       1030.10     104040.10
            3.00          0.01       1050.40     106090.50
            4.00          0.01       1070.91     108161.41
            5.00          0.01       1091.61     110253.02
            6.00          0.01       1668.80     112921.82
            7.00          0.01       1708.83     115630.64
            8.00          0.01       1749.46     118380.10
            9.00          0.01       1790.70     121170.80
           10.00          0.01       1832.56     124003.37
           11.00          0.01       1875.05     126878.42
           12.00          0.02       2557.57     130435.98
0 Comments
Accepted Answer
  Amit
      
 on 8 Feb 2014
        The interest rates are calculated right. The issue is that 0.015 gets hidden as 0.01.
Try this instead of disp:
fprintf('%d \t %1.3f \t %7.1f \t %7.1f \n',Months(k),rate-1,Interest,A)
More Answers (1)
  Jerry
 on 2 Apr 2018
        Hi, I think I found the solution about the wrong use of calculation. Notice that the question required us to calculate about the interest before the deposit is made. And the Interest is calculated and added to his account at the end of each month. Thus we can change our code slightly to get the right answer.
A=100000;
Monthly=1000;
Months=[1:12];
disp('MONTHS ELAPSED    INTEREST RATE     AMOUNT OF INTEREST      MONTH-END BALANCE')
 for k=1:length(Months)
     if A <= 110000;
        rate=1.01;
     elseif A <=125000;
         rate=1.015;
     else 
         rate=1.02;
     end
   Interest=(A*rate)-A;  %We calculate the interest before the deposit is added.
   A=A*rate+Monthly; %Deposit is added after the interest.
   fprintf('%d \t %1.3f \t %7.2f \t %7.2f \n',Months(k),rate-1,Interest,A) %Change to 7.2f to get the right form.
 end
----------------------Here is the result------------------------------------------------------
MONTHS ELAPSED    INTEREST RATE     AMOUNT OF INTEREST      MONTH-END BALANCE
1    0.010    1000.00    102000.00 
2    0.010    1020.00    104020.00 
3    0.010    1040.20    106060.20 
4    0.010    1060.60    108120.80 
5    0.010    1081.21    110202.01 
6    0.015    1653.03    112855.04 
7    0.015    1692.83    115547.87 
8    0.015    1733.22    118281.08 
9    0.015    1774.22    121055.30 
10    0.015    1815.83    123871.13 
11    0.015    1858.07    126729.20 
12    0.020    2534.58    130263.78
0 Comments
See Also
Categories
				Find more on Dates and Time in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

