for loop - Error Attempted to access y(30); index out of bounds because numel(y)=2.
古いコメントを表示
Dear all
I have to compute a time path over t=1:100 periods. I have to assume a temporary shock in t=30:32 so I tried this and used different for loops:
y(1)=100; %(starting value of y) a=1.02;
for t=1:29;
y(t+1)=y(t)*(a+0.0001*randn(1,1));
for t=30:32 (here there is the temporary shock)
y(t+1)=y(t)*(a-0.2);
end;
for t=33:100
y(t+1)=y(t)*(a+0.0001*randn(1,1));
end;
end;
Unfortunately, it gives me the error: Attempted to access y(30); index out of bounds because numel(y)=2.
I really tried different things but couldn't get the loop fixed.
I already thank you for answering my question.
採用された回答
その他の回答 (1 件)
Torsten
2014 年 11 月 17 日
y=zeros(101);
b=randn(101,1);
y(1)=100;
for t=1:29
y(t+1)=y(t)*(a+0.0001*b(t,1));
end
for t=30:32 %(here there is the temporary shock)
y(t+1)=y(t)*(a-0.2);
end
for t=33:100
y(t+1)=y(t)*(a+0.0001*b(t,1));
end
Best wishes
Torsten.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!