In: Computer Science
In MatLab, Determine how many years it will take to accumulate at least $10,000 in
your bank account if you deposit $1000 initially and $500 at the end of
each year. The account pays 2.5% interest annually. At the end of each
year your loop should write a message to the user, for example: "After
year 1, the balance in your account is $xx,xxx.xx". Your script should
also write out the final balance after $10,000 is reached.
% Matlab script to determine how many years it will take to
accumulate at least $10,000
balance = 1000 ; % initial deposit
target_balance = 10000; % target balance to reach
deposit = 500; % deposit at the end of each year
interest_rate = 0.025; % interest rate annually
years = 0;% number of years
% loop that continues till the user has accumulated at least target
balance
while balance < target_balance
years = years + 1; % increment the years
balance = balance+(balance*interest_rate); % calculate the interest
accumulated at the end of the year
balance = balance + deposit; % add deposit at the end of the
year
% display the balance at the end of the year
fprintf('After year %d, the balance in your account is
$%.2f\n',years,balance);
end
% display the final balance
fprintf('Final balance after %d years :
$%.2f\n',years,balance);
%end of script
Output: