Why have negative value in result
Show older comments
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
Answers (2)
madhan ravi
on 29 Dec 2018
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 Comment
heidi pham
on 29 Dec 2018
John D'Errico
on 29 Dec 2018
Edited: John D'Errico
on 29 Dec 2018
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.
1 Comment
per isakson
on 29 Dec 2018
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!