MATLAB: Loop starting from a specific time

countdifferece equationsloopstime path

I first had to calculate two time paths y_permanent and y_temporary, the codes are the following ones:
y_temporary=zeros(101,1);
psi=0.0001*randn(101,1);
y_temporary(1)=100;
for t=1:29
y_temporary(t+1)=y_temporary(t)*(a+0.0001*psi(t,1));
end
for t=30:32
y_temporary(t+1)=y_temporary(t)*(a-0.2);
end
for t=33:100
y_temporary(t+1)=y_temporary(t)*(a+0.0001*psi(t,1));
end
The path for the permanent change was:
y_permanent=zeros(100,1);
y_permanent(1)=100;
for t=1:100;
y_permanent(t+1)=y_permanent(t)*(1.01+0.0001*psi(t,1)); %alpha is now 1.01
end;
Now, my problem is, that I have to figure out a loop wich starts at time t=33 and stops, when the temporary shocks value is twice as high as the value of the permanent shock, counting the number of periods it needs to do so.
I tried several for loops, while loops and combination of a while and if loop but with no success.
If someone could give me a hint how to easily create such a loop, I would be so glad! I'm desperately looking for an answer.
Best Rico

Best Answer

Rico - if a period is just one iteration of your for loop (so since your arrays have 101 elements, then there are 101 periods), then you could try the following
numPeriods = 0;
criteriaMet = false;
for k=33:length(y_temporary)
if y_temporary(k)>=2*y_permanent(k)
% the temporary shock value is at least twice that of the
% permanent shock, so we are done
criteriaMet = true;
break;
end
numPeriods = numPeriods + 1;
end
Note how we use break to exit the for loop once the criteria has been met. We use criteriaMet to determine if we ever did find a period where the temporary shock was at least twice that for the permanent.