2
$\begingroup$

I am currently trying to create a while-loop which iterates the evolution of the capital stock until it converges on the n-th digit. As I am still quite new to Matlab I face a lot of problems. I would be very thankful for any kind of help.

Here is my code:

% Model parameters
alpha = 1/3;    %  
s     = 0.2;    % Investment rate
delta = 0.2;    % Depreciation rate
n     = 0.02;   % Growth of labor force
g     = 0.01;   % technological progress


eps  = 1;
k(1,1) = 0.5;
i=0;


while  eps >= 1.0e-5        
    t = t+1;
    k(1,t) = (1/((1+n)*(1+g)))*((s*k(1,t-1)^alpha+(1-delta)*k(1,t-1)));
     z = k(1,t);       
    z1 = k(1,t+1);
         kdif = z1-z ;
          eps = max(abs(kdif));
    i = i + 1;
  if i >= 300
    break;
  end
end
$\endgroup$
7
  • 2
    $\begingroup$ I have several remarks concerning your code. First of all, write for simplification $k(1)=0.5$, and within the formula of while-loop replace $k(1,t-1)$ by $k(t-1)$ that should be assigned to the variable $k(t)$ and not to $k(1,t)$. In addition, replace $z=k(1,t)$ by $k(t).$. Moreover, you must set the time period to $t=1$ before the while-loop. Finally, $z1=k(1,t+1)$ makes no sense, this value is still not calculated. Thus, replace it by $z1=k(k-1)$. Then rewrite $kdif$ to $kdif=z-z1$. After these corrections, you should get after $55$ iterations the value $0.8097$. Hope this helps. $\endgroup$ Nov 23 '20 at 11:59
  • $\begingroup$ @HolgerI.Meinhardt please post answers as answers - note answer can be in forms of hints if you think the user’s question is simple enough that from didactic perspective it is better to provide hint rather than straightforward answer $\endgroup$
    – 1muflon1
    Nov 23 '20 at 12:07
  • 1
    $\begingroup$ Finally, delete the iterator $i$ everywhere it has no meaning, you have already $t$. Hence, within the if-construct, use if $ t>=300$ instead. $\endgroup$ Nov 23 '20 at 12:09
  • $\begingroup$ @HolgerI.Meinhardt Thanks, this helps a lot. Actually I thought I have to type row and column whenever I am working with vectors. Therefore I typed k(1,t) all the time. So thanks for this hint as well. Obviously, I am still in the very beginnings of learning Matlab. $\endgroup$
    – Hokkaido21
    Nov 23 '20 at 12:12
  • $\begingroup$ @HolgerI.Meinhardt Thank you. What would change if I would use k(t+1)=(1/((1+n)*(1+g)))*((s*k(t)^alpha+(1-delta)*k(t))) instead? So is it important for Matlab whether I write something like k(t+1)=k(t) or k(t)=k(t-1)? $\endgroup$
    – Hokkaido21
    Nov 23 '20 at 12:16
4
$\begingroup$

Since, I was urged to present an answer for didactic reasons to which I totally agree, I will provide the full set of corrections to avoid any ambiguity.

% Model parameters
alpha = 1/3;    %  
s     = 0.2;    % Investment rate
delta = 0.2;    % Depreciation rate
n     = 0.02;   % Growth of labor force
g     = 0.01;   % technological progress
eps  = 1;
k(1) = 0.5;
t=1;

while  eps >= 1.0e-5        
    t = t+1;
    k(t) = (1/((1+n)*(1+g)))*((s*k(t-1)^alpha+(1-delta)*k(t-1)));
    z = k(t);         
    z1 = k(t-1);
    kdif = z-z1 ;
    eps = max(abs(kdif));
   if t >= 300
     break;
   end
 end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.