Borrar filtros
Borrar filtros

nested loop shows only the result of last iteration

1 visualización (últimos 30 días)
Raushan
Raushan el 4 de Oct. de 2023
Comentada: Raushan el 4 de Oct. de 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?

Respuesta aceptada

Torsten
Torsten el 4 de Oct. de 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
...

Más respuestas (1)

David Hill
David Hill el 4 de Oct. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by