Simpler way to write this scrip.

16 views (last 30 days)
Kristian Torres
Kristian Torres on 3 Dec 2019
Answered: David Hill on 3 Dec 2019
I wrote this script but its overcomplicated could someone give some advice how to simplify the scrip.
2. The question: Use a while loop in MATLAB to determine how long it will take to accumulate at least 10,000$ in a bank account if you deposit $1000 initially and $500 at the end of each year, assuming the APR is 5%.
clear;
clc;
balance = 1000;%initial deposit
target_balance = 10000;%target balance to reach
deposit = 500;% deposit at the end of each year
interest_rate = 0.05; % APR
years =0;%number of years
while balance < target_balance;
years = years + 1%increment the years
principal = balance+(balance*interest_rate)%calculate the interest accumulated at the end of the year
balance = balance+deposit%adddeposit at the end of the year
fprintf('After year %d, the balance in your account is $%.2f\n',years,balance);
end
fprintf('Final balance after %d years :$%.2f\n',years,balance);

Answers (1)

David Hill
David Hill on 3 Dec 2019
Time=0;
balance=1000;
while balance<10000
balance=500 + 1.05*balance;
Time=Time+1;
end
display(Time);

Categories

Find more on Get Started with MATLAB 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!