Compound interest loops problem (matlab)

Click For Summary
SUMMARY

The discussion focuses on solving a compound interest problem using MATLAB, specifically for a Certificate of Deposit (CD). The user successfully implemented part (a) of the problem, calculating the final value of the CD after a specified term using a for loop. For part (b), the user struggled with determining the number of years required for the investment to exceed twice its original value, initially misusing a while loop. Ultimately, the user discovered the use of a counter variable to track the number of iterations needed to achieve the desired final value.

PREREQUISITES
  • Understanding of MATLAB programming language
  • Knowledge of compound interest calculations
  • Familiarity with loops in programming (for and while loops)
  • Basic concepts of variable assignment and condition checking in MATLAB
NEXT STEPS
  • Learn about MATLAB's while loop and its execution flow
  • Explore the use of counters in loops for tracking iterations
  • Study compound interest formulas and their implementation in programming
  • Investigate MATLAB's built-in functions for financial calculations
USEFUL FOR

Students learning MATLAB, financial analysts calculating investment growth, and anyone interested in programming loops and compound interest calculations.

gfd43tg
Gold Member
Messages
949
Reaction score
48

Homework Statement



(a) Suppose you decide to invest money in a Certificate of Deposit (CD). Write code that assigns
to the variable FinalValue the final value of the CD based on the following variables:
Investment: the initial amount of money invested in the CD,
Rate: the annual interest rate of the CD, and
Term: the term length of the CD in years.
The interest of the CD should be compounded annually (i.e., at the end of each year the
value of the CD increases by the interest rate times the current value of the CD). Example:
If Investment equals 100, Rate = 0.0235 and Term equals 1, then FinalValue should be
assigned a value of 102.35.

(b) Write code that assigns to the variable numYears, an integer number of years required for
the value of the CD to exceed twice the original value given by the variable Investment (as
in Problem 1a) with the interest rate given by the variable Rate (as in Problem 1a).


Homework Equations





The Attempt at a Solution


I got part (a) of the problem
Code:
Rate = 0.05;
Term = 10;
Investment = 1000;

FinalValue = Investment;
for k = 1:Term
    FinalValue = FinalValue + Rate*FinalValue;
end

But now I am stuck on part (b). I don't know how to incorporate numYears into my while loop. Also, when I run what I have right now in its present form, I get FinalValue = 1000
Code:
Rate = 0.05;
Investment = 1000;

FinalValue = Investment;
while FinalValue < 2*Investment;
      FinalValue = FinalValue + Rate*FinalValue;
end

Edit: I didn't realize that the while will skip the commands in the loop if false. I assumed it would check for truth, and if not true, redo the loop until true. Hence I changed the > to a <. That seems backwards to me...


Now I know FinalValue is 2.0789e+03. I ran it without the commas and 15 executions gives me that value. Now I need to figure out how to assign numYears to the number of times the loop executed.

Edit: nevermind, just found out about the Count
 
Last edited:
Physics news on Phys.org
I'm sorry you are not generating any responses at the moment. Is there any additional information you can share with us? Any new findings?
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
4
Views
1K