How many years do i need to get the amount?

6 views (last 30 days)
Hamzah
Hamzah on 21 Nov 2014
Commented: Image Analyst on 22 Nov 2014
Imagine that you went to the bank and deposited $20,000 in an account that earns 6% interest every year, with each year’s interest being deposited back into the account. Write a MATLAB program that computes the number of years it would take to accumulate $500,000.
The only thing I know is that 6% = 0.06 and I need to use a for loop, with an if statement, to solve this problem. I couldn't even start the program
So I have come this far but I don't know if it correct or not:
y = 1;
r = 0.06;
p = 20000;
A = 500000;
function t = final result
t = log (A/p)/y*log (r/y);

Answers (4)

Image Analyst
Image Analyst on 21 Nov 2014
Wasn't the simple interest formula discussed in the class? Like if P is your principal (initial) amount, then after 1 year you have P + 0.06*P, and after 2 years you have (P + 0.06*P) + 0.06 * (P + 0.06*P) and so on. There is a formula you can use to get the final amount without a for loop, but if you're required to do a for loop you can do
finalAmount = 20000 % Amount you're starting with.
for y = 1 : totalNumberOfYears
finalAmount = finalAmount + ........
end
I think that should be plenty of hint to get you started. Use inputdlg() to ask the user for the number of years, principle amount, interest rate, etc.
  3 Comments
Image Analyst
Image Analyst on 22 Nov 2014
No. Why would you add the number of years to the dollar amount???? that doesn't make any sense. You have to add the amount of money it gained during that year. I really can't do that for you or else I've done the whole thing for you. Just think. If I have $1000 for a year and it gained 6% interested, what is the dollar amount of the interest? What do I multiply by what to get the answer?
You also have to have a way to bail out once you've reached 500000. You can do that with break.
if finalAmount > 500000
break;
end
Also, don't use "sum" as the name of a variable since it's a built-in function.
Image Analyst
Image Analyst on 22 Nov 2014
Still can't figure it out? Here's a little more:
totalNumberOfYears = 100;
finalAmount = 20000 % Amount you're starting with.
for y = 1 : totalNumberOfYears
finalAmount = finalAmount + finalA.......FINISH THIS LINE
fprintf('After %d years, the balance is $%.2f\n', y, finalAmount);
if finalAmount > 500000
break;
end
end
You should be able to get it now since only a few characters are missing.

Sign in to comment.


Roger Stafford
Roger Stafford on 21 Nov 2014
This is a problem in compound interest, the compounding occurring once each year. Think it through like this. After one year you have 20000*1.06, after two years it amounts to 20000*1.06*1.06, and so forth. Does that give you a hint as to how to solve the problem?
  1 Comment
Hamzah
Hamzah on 22 Nov 2014
i know that compound interest formula is A = p(1+r/n)^(n*t)
and the other thing i think its: for i = 1:n %for example sum = sum + i end
sorry but im really bad at this so bare with me

Sign in to comment.


Star Strider
Star Strider on 22 Nov 2014
You can always just ‘brute-force’ your solution and see what fzero, our reliable friend in intractable situations, (although this is hardly intractable) comes up with:
A = 500000; % Workspace Variables & Constants
p = 20000; % Workspace Variables & Constants
r = 0.06; % Workspace Variables & Constants
n = 1; % Workspace Variables & Constants
C = @(t) p*(1+r/n).^(n*t); % Compound Interest Formula (As Anonymous Function)
t = fzero(@(t) (C(t)-A), 5); % Time until Retirement (Years)
  3 Comments
Star Strider
Star Strider on 22 Nov 2014
I completely agree.
I was suggesting a way of getting the correct answer as guidance, since OP’s analytic solution (the last paragraph in OP’s edited Question) is not the same result I got when I solved it. (Mine was a one-line derivation that yielded the correct result.)
Image Analyst
Image Analyst on 22 Nov 2014
It may not be the way we'd solve it but we've all seen homework problems where the instructor specifies a certain method be used. He says he needs to use a for loop and if statement for some reason (presumably the instructor wants that so as to teach them for loops or something): "The only thing I know is that 6% = 0.06 and I need to use a for loop, with an if statement, to solve this problem."

Sign in to comment.


kobi
kobi on 22 Nov 2014
i=0.06;
a=20000;
n=a;
for y=1:a n=n*i+n;
if n>500000
display(n);
display(y);
break;
end
end
there you go buddy, answer is 56 :)

Categories

Find more on Graphics Performance 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!