Please help me with a while loop problem

A person wants to save for a trip which will cost $12000 , initial deposit $4000 invested at interest of 5.8% per annum and plan to deposit $400 every month,
1)write a code which uses a loop statement to determine how many months are required for the savings to exceed $12000
2)Amount=Amount(1+(rate/1200))+400 using this code get it to display the number of months required and the total amount saved
1)is this right dep = 400; amount = 4000; year = 0; while account = 12000 amount = amount(1+((0.058/1200)) + 400; end
2) dont understand

3 件のコメント

Jan
Jan 2013 年 4 月 28 日
編集済み: Jan 2013 年 4 月 28 日
@Mike: It is a very bad idea to mask your message with rubbish. This is impolite to the ones, who have spent time to create answers. See: http://www.mathworks.com/matlabcentral/answers/16153-is-deleting-threads-helpful-in-this-forum
Cedric
Cedric 2013 年 4 月 28 日
And finally, like many others on this forum, I'll stop helping people with homework questions (unless they provide a full/valid ID).
Randy Souza
Randy Souza 2013 年 5 月 24 日
I have restored the original text of this question.
@Mike: this question has a clear subject and an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.

サインインしてコメントする。

 採用された回答

Cedric
Cedric 2013 年 4 月 28 日

1 投票

The first thing that is not correct is the condition in the WHILE statement; you want to repeat the loop while the amount is below 12,000. Written with the equality, the program will never enter the loop as the condition is wrong initially.
You can test your computation by removing the ";" that you put at the end of the line that updates the variable amount, so the value is displayed at each step of the iteration. If you compute a few values by hand, you'll be able to compare. Note that
amount(1+((0.058/1200))
(which is a copy of the expression in the second part of the question) is not a valid MATLAB expression for what you want to do, as it would be an indexing operation on amount.

4 件のコメント

Cedric
Cedric 2013 年 4 月 28 日
編集済み: Cedric 2013 年 4 月 28 日
The WHILE statement condition is almost correct, in the sense that the question asks for the number of months to "exceed" $1,2000. This seems to indicate that if amount = 12000, it is not enough and you want to make one more iteration.
If you don't understand, experiment on a simpler case (which is what you should always do), for example:
k = 0
while k < 12
k = k + 1
end
Now the formula for computing amount is incorrect. Try to compute one or two iterations by hand to see why (in particular, try to understand where this 1200 comes from and whether the 0.058 rate that you are using is correct). Also, note that the operator for the product is the * (or .* for element-wise operations). Once you master the concept on paper, try to implement the correct formula in MATLAB. Do it in the command line, just for this formula (without the WHILE statement); set amount to 4000 and see whether you are able to define a new amount for months 1, 2, etc, by repeating the execution of the line that defines the new amount using the current value.
Once you are good with that, implement it in the WHILE loop without the final ";" on the line that computes amount, so you can control that it is matching your previous computation.
When this works, see how you could implement a counter of iterations (counting months in fact).
Cedric
Cedric 2013 年 4 月 28 日
Ok, now how would you change the condition if I were asking you to build a loop that iterates until k exceeds 12?
The 1200 has nothing to do with the dollars amount. Try to understand where it comes from, for example by studying how to compute how much money there is in the account after one month when the rate is 5.8% per annum.
Cedric
Cedric 2013 年 4 月 28 日
編集済み: Cedric 2013 年 4 月 28 日
Well, the main issue is that you cannot model with MATLAB something that you don't understand, so you'll have to understand how interest rates work before trying to write code that involves them.
My advice is work on simple cases until you fundamentally understand the material, and then scale up to more complicated cases. A simple case would be: if you have $4000 on an account with an interest rate of 6% per year, how much money do you have after 1 month, and after 3, .. and after 12? When you understand this, you will be able to determine where the 1200 is coming from in your formula.
About the first point (..until k exceeds 12), you should look up for documentation about WHILE statements in MATLAB, and also about relational operators, for example here: http://www.mathworks.com/help/matlab/matlab_prog/operators.html#f0-38145
Cedric
Cedric 2013 年 4 月 28 日
編集済み: Cedric 2013 年 4 月 28 日
If you don't know how this formula was built, you won't know who to express R. Is it 5.8? Is it 5.8/100? Is the 1200 related to the number of months in a year? Did you apply this formula by hand on a simple case as I mentioned, and checked that it is working?
If you have $100 on an account with a 12%/year interest rate, how much money should you have on this account after 1 year? and after 1 month? and how do you obtain this result using your formula?
Your test with the relational operator is almost correct; try with
k = 0
while k <= 12
k = k + 1
end
and observe that the WHILE statement gets out when the condition is not true, which means when k is not >= 12, which happens when k = 13. This answers the question "build a loop that iterates until k exceeds 12". The same reasoning might be relevant to your case with $12000.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2013 年 4 月 27 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by