# I'm new to Matlab and still learning, how do I go about solving this problem in simple terms?

조회 수: 2(최근 30일)
Allison Sims 2022년 7월 18일
편집: Torsten 2022년 7월 19일
You want to save some money. You start with \$1000, and contribute \$100 to the account each month. Using an interest rate of 5% APR compounded monthly (.05/12), how much money will you have after ten years (120 months)? Create a table showing the monthly balance for each month. Plot the monthly balance in a bar graph.
initial=1000;
month = 1:1:120;
interest= (0.05/12);
while month<=120
Amount = initial + interest + 100;
end
fprintf('month Amount\n%2.2f %2.2f', month,amount)
%I'm not sure how to contribute \$100 to the account each month
%Haven't plotted the bar graph yet since the code won't compute

댓글을 달려면 로그인하십시오.

### 채택된 답변

Steven Lord 2022년 7월 18일
initial=1000;
month = 1:1:120;
You don't want this to be a vector. Make it a scalar (one number) that represents the month whose balance you're currently computing.
interest= (0.05/12);
This is your interest rate, not the interest value. If you had a million dollars in the bank, you'd probably want to find a better bank if all the interest they were giving you each month was about 4/10th of a penny.
while month<=120
This is why you want month to be a scalar. When computing the first month (month = 1) this condition is satisfied. When computing the second month (month = 2) this condition is satisfied. ... When computing the 121st month (month = 121) this condition is not satisfied and the loop ends.
But if month were, say, [119 120 121] would you expect the loop to continue or end? Two of the elements of month satisfy the condition, but one does not. So MATLAB would end the loop.
Amount = initial + interest + 100;
This is close, but can you explain (using a comment) what each of those terms represents? I advise you to get in the habit of using comments to describe why your code is doing what it's doing if there's a chance it's not obvious. It really helps you or others who revisit this code a month, two months, six months, a year, or five years later. I have looked at test code, asked myself "Why in the world did they write it that way?" [though occasionally with saltier language] only to discover, through a little software archaeology, that "they" was me.
And why is usually the important question to answer, not what. What your code is doing (adding together three values) is often derivable from the code itself. But especially if you're doing something specific that a fellow developer may wonder "Why didn't you do it this easier way?" explaining the why can save them calling you up to ask for an explanation (or making the "obvious" change and potentially breaking the code.)
end
fprintf('month Amount\n%2.2f %2.2f', month,amount)
%I'm not sure how to contribute \$100 to the account each month
%Haven't plotted the bar graph yet since the code won't compute
You already did, sort of. That's the 100 inside your while loop. But you're not actually doing any accumulating. Each iteration through the while loop computes the variable Amount anew. But with interest, you accumulate into a pool of money you already have.
If you had a bank savings account with a balance of \$100 gaining 5% interest per month, at the end of the first month you'd have \$105 since 5% of \$100 is \$5. At the end of the second month, after the bank adds the interest to your account, would you expect it to have \$105 (= \$100 + \$5) or more than \$105 in it? Since the interest on \$105 is going to be more than \$5 I hope you'd expect the account to have more than \$105 + \$5 in it.
So based on what I wrote, I recommend adding a variable to your code named balance. [You might also find it useful to have two variables for interest, one called interestRate and one called interestAmount, and one named depositAmount.] The variables initial, interestRate, and depositAmount probably won't need to change in your loop (for this problem) but month, balance, and interestAmount will.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Torsten 2022년 7월 19일
편집: Torsten 2022년 7월 19일
Money Month 0: 1000
Money Month 1: Money Month 0 + 0.05/12*Money Month 0 + 100
Money Month 2: Money Month 1 + 0.05/12*Money Month 1 + 100
...
Money Month i: Money Month (i-1) + 0.05/12*Money Month (i-1) + 100
...

댓글을 달려면 로그인하십시오.

### 범주

Find more on Multirate Signal Processing 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!

Translated by