MATLAB: Interest and displaying balance until double the investment

balancecompound interestinterestinvestmentwhile

So I am trying to write a function that will show me a balance after every year with a compound interest of 10% until the balance has reached double the initial investment.
what I have so far is
x=input('initial investment ');
b=1;
for y=x*((1.1)^b)
if y< 2*y
b=b+1;
elseif y >= 2*y
disp(y);
end
I'm pretty sure that my issue is trying to get 'b' to increase each year as so far it just stops. I also think I might have to use a while loop but I don't know how.

Best Answer

I don't know what the badly-named "b" is. Try it like this:
initialInvestment = input('Enter initial investment ')
interestRate = input('Enter annual interest rate (0-1)')
currentBalance = initialInvestment;
maxYears = 1000;
for y = 1 : maxYears
currentBalance = currentBalance * (1 + interestRate);
fprintf('At the end of year %d, your balance is $%.2f.\n', y, currentBalance);
if currentBalance >= 2 * initialInvestment
fprintf('After year %d, your balance has more than doubled!\n', y);
break;
end
end
Related Question