Simpler way to write this scrip.

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);

답변 (1개)

David Hill
David Hill 2019년 12월 3일

0 개 추천

Time=0;
balance=1000;
while balance<10000
balance=500 + 1.05*balance;
Time=Time+1;
end
display(Time);

카테고리

도움말 센터File Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

질문:

2019년 12월 3일

답변:

2019년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by