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

Réponses (2)

madhan ravi
madhan ravi le 29 Déc 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 commentaire

heidi pham
heidi pham le 29 Déc 2018
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

Connectez-vous pour commenter.

John D'Errico
John D'Errico le 29 Déc 2018
Modifié(e) : John D'Errico le 29 Déc 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.

Catégories

En savoir plus sur Just for fun dans Centre d'aide et File Exchange

Question posée :

le 29 Déc 2018

Commenté :

le 29 Déc 2018

Community Treasure Hunt

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

Start Hunting!

Translated by