Clear Filters
Clear Filters

nested loop shows only the result of last iteration

1 view (last 30 days)
Dear All,
T=10
for n=(0:1:(T-1))
W_t=(1+r*(1-tau)).^(-(T-n)).*C_0*epsilon.^T.*K^(1/gamma);
for m=(n+1:1:T-1)
W_t=W_t+((C_0.*epsilon.^m-L_0.*(1-tau).*g.^m).*(1+r.*(1-tau)).^(-(m-n)));
end
disp(W_t)
end
My inside loop depends on index of outside loop
This stores only W(t) at 9.
I would like to get from 0 to 9.
How can I fix this?

Accepted Answer

Torsten
Torsten on 4 Oct 2023
...
W_t(n+1)=(1+r*(1-tau)).^(-(T-n)).*C_0*epsilon.^T.*K^(1/gamma);
for m=(n+1:1:T-1)
W_t(n+1)=W_t(n+1)+((C_0.*epsilon.^m-L_0.*(1-tau).*g.^m).*(1+r.*(1-tau)).^(-(m-n)));
end
...

More Answers (1)

David Hill
David Hill on 4 Oct 2023
Index W_T
T=10
W_T=zeros(T,T-1);
for n=0:T-1
W_t=(1+r*(1-tau)).^(-(T-n)).*C_0*epsilon.^T.*K^(1/gamma);
for m=n+1:T-1
W_T(n+1,m)=W_t+((C_0.*epsilon.^m-L_0.*(1-tau).*g.^m).*(1+r.*(1-tau)).^(-(m-n)));
end
end
W_T

Categories

Find more on Loops and Conditional Statements 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!