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

4 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Steven Lord
Steven Lord il 18 Lug 2022
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 Commenti
Allison Sims
Allison Sims il 18 Lug 2022
Modificato: Allison Sims il 19 Lug 2022
I've made a few changes to my code, I'm a little confused on what the depositAmount will look like. I appreciate your help and feedback it's great to hear explanation instead of just direct answers. This new code seems to be working a little better but does not stay in the loop for up to 120 months.
initial=1000;
index=1:120;
Balance = initial + (index*(0.05/12));
for index=2;
Balance = Balance(index-1) + (Balance(index-1) * (0.05/12)) + 100;
end
fprintf('month Amount\n%2.2f %2.2f \n', index, Balance)
month Amount 2.00 1104.17
Torsten
Torsten il 19 Lug 2022
Modificato: Torsten il 19 Lug 2022
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
...

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Install Products in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by