How many years do i need to get the amount?
이전 댓글 표시
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);
답변 (4개)
Image Analyst
2014년 11월 21일
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
Hamzah
2014년 11월 22일
편집: Image Analyst
2014년 11월 22일
Image Analyst
2014년 11월 22일
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
2014년 11월 22일
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.
Roger Stafford
2014년 11월 21일
0 개 추천
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?
Star Strider
2014년 11월 22일
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
Roger Stafford
2014년 11월 22일
There is no need for 'fzero'. By taking the logarithm of the above equation, the variable 't' can be solved directly and then a 'ceil' done. Actually there's no need for matlab. A scientific calculator can do it easily.
Star Strider
2014년 11월 22일
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
2014년 11월 22일
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."
kobi
2014년 11월 22일
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 :)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!