MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Mike on 27 Apr 2013

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

*No products are associated with this question.*

Answer by Cedric Wannaz on 28 Apr 2013

Accepted answer

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`.

Show 1 older comment

Cedric Wannaz on 28 Apr 2013

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 Wannaz on 28 Apr 2013

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 Wannaz on 28 Apr 2013

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.

## 3 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/73793#comment_145980

@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

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/73793#comment_145996

And finally, like many others on this forum, I'll stop helping people with homework questions (unless they provide a full/valid ID).

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/73793#comment_150776

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 notsimply edit your question away.