Filter löschen
Filter löschen

nested loop shows only the result of last iteration

1 Ansicht (letzte 30 Tage)
Raushan
Raushan am 4 Okt. 2023
Kommentiert: Raushan am 4 Okt. 2023
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?

Akzeptierte Antwort

Torsten
Torsten am 4 Okt. 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
...

Weitere Antworten (1)

David Hill
David Hill am 4 Okt. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by