Hello,
In the following code, I try to exclude the case when there is any element of the vector "kseq" in my code. So, I expect the "kseq" achived should have all positive element.
But I dont know why in the result, I still got negative value for some elements of that vector "kseq".
If anyone can see what I did wrong with my code, plz kindly let me know. Thank you!
alpha = 1/3;
beta = 0.9999;
sigma = 0.001;
delta = 0.1;
% Horizon
T=200;
kseq=zeros(T+1,1);
cseq=zeros(T+1,1);
kseq(1)=((alpha*beta)/(1-beta+delta*beta))^(1/(1-alpha));
cguessl=1;
cguessh=6;
iter=0;
tol=0.01;
gapk=1;
while gapk > 0.01 && iter < 1000
cguess=0.5*(cguessl+cguessh);
cseq(1)=cguess;
for i = 2:T+1
kseq(i) = (kseq(i-1))^alpha+(1-delta)*kseq(i-1)-cseq(i-1);
if kseq(i)<0; flag=1; break; end;
cseq(i)=(beta*(1+alpha*(kseq(i))^(alpha-1)-delta))^sigma*cseq(i-1);
end
if flag==0 & kseq(T+1) >0
cguessl=cguess;
else
cguessh=cguess;
end
gapk = abs(kseq(T+1));
iter=iter+1;
end

답변 (2개)

madhan ravi
madhan ravi 2018년 12월 29일

0 개 추천

kseq(i) = (kseq(i-1))^alpha+(1-delta)*kseq(i-1)-cseq(i-1);
^^^^^^^^^---- if greater than the other values the result would be negative

댓글 수: 1

heidi pham
heidi pham 2018년 12월 29일
Hi,
Could you kindly say a bit clearer? I do not understand fully.
In the code, I have if kseq(i)<0; flag=1; break; end; so I should have negative values?
Thanks

댓글을 달려면 로그인하십시오.

John D'Errico
John D'Errico 2018년 12월 29일
편집: John D'Errico 2018년 12월 29일

0 개 추천

LEARN TO USE THE DEBUGGER!
If I step through your code, when i == 4, I get to this point:
i
i =
4
K>> [kseq(1:i),cseq(1:i)]
ans =
6.0767 3.5
3.7938 3.5001
1.474 3.5006
0 0
That is just before kseq(4) is assigned. What will it be?
(kseq(i-1))^alpha+(1-delta)*kseq(i-1)-cseq(i-1)
ans =
-1.036
USE THE DEBUGGER! It was put there for you to use. What you are doing and why, I have no clue, because it is impossible to understand uncommented code, to know if it should be written differently.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 12월 29일

댓글:

2018년 12월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by