nested loop shows only the result of last iteration

1 次查看(过去 30 天)
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?

采纳的回答

Torsten
Torsten 2023-10-4
...
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
...

更多回答(1 个)

David Hill
David Hill 2023-10-4
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by