Write a function that has three input arguments deposit, interest, and years. It
ID: 3536762 • Letter: W
Question
Write a function that has three input arguments deposit, interest, and years. It has one output variable balance. At the end of each year, the balance in the account is (1+interest rate) times the amount at the beginning of the year(i.e. if your interest rate is 5% the balance is 1.05 times the amount at the beginning of the year.) Now you want to write a script that is able to call the function with different amount of deposit, interest rate, and years of duration. For example, you want to see how much your balance will be with $1000 of deposit and 5% interest rate after 10 years.Explanation / Answer
ANSWER
THIS WILL HELP YOU
balance = 1000;
for year = 1:10
for month = 1:12
balance = balance * 1.01;
end
disp(balance)
end
%----
Copy and paste the whole of the above into the main window and it will pump out 10 numbers. Those are the year-end balances. However, the answer isn't vectorised - it's just a single figure. If you want the vectorised version, swap the appropriate line with:
balance = [balance balance(end)*1.01];
and place the "disp (balance)" at the very end.
But that will now give you all 120 months' worth! Finally, use the following:
balance = 1000;
for year = 1:10
temp_balance = balance(end);
for month = 1:12
temp_balance = temp_balance*1.01;
end
balance = [balance temp_balance];
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.