MATLAB: Linking an element in the for loop with the result from the previous iteration

for loopindexingvectorization

I'm working on a retirement portfolio code which involves inflation adjusted withdrawals. What I'm trying to achieve here is a series of these to subtract from the portfolio later. 1:20 are essentially years 1:20 the portfolio. Each year, the withdrawal amount is = previous year's withdrawal amount * (1+inflation rate). Trying to formulate this but not getting the output. Any help would be appreciated!
Inipot = 200000; %The initial amount invested
Withd = 0.04; %The withdrawal rate
Infrate = 0.03;%Inflation rate
A= Inipot*Withd ; %Withdrawal amount
for idx = 1:20,
if idx ==1
out = A;
else
out(idx) =[(1+Infrate)* A(idx-1)]
end
end

Best Answer

You need to put the withdrawal rate in as well as the inflation rate.
I’m not certain whether you want to add inflation before or after you factor in the withdrawal, so see if this works for you:
Inipot = 200000; %The initial amount invested
Withd = 0.04; %The withdrawal rate
Infrate = 0.03;%Inflation rate
A= Inipot*Withd ; %Withdrawal amount
for idx = 1:20,
if idx ==1
out = A;
else
out(idx) =[(1+Infrate)* A]; % Add Inflation
A = A*Withd; % Withdrawal
end
end
figure(1)
semilogy(out)
grid
I added the plot as a check on the result.