- #1
gfd43tg
Gold Member
- 950
- 50
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: