<p>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.</p>
<pre><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) = 0.5;
t=1;

while  eps &gt;= 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 &gt;= 300
     break;
   end
 end
</code></pre>

CC BY-SA 4.0
2020-11-23T12:14:45.363Z
2020-11-23T12:14:45.363Z
Holger I. Meinhardt
<p>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.</p>
<p>Here is my code:</p>
<pre><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 &gt;= 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 &gt;= 300
    break;
  end
end
</code></pre>

CC BY-SA 4.0
2020-11-23T11:59:56.430Z
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.
Holger I. Meinhardt
CC BY-SA 4.0
2020-11-23T12:07:34.210Z
@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
1muflon1
CC BY-SA 4.0
2020-11-23T12:09:34.973Z
Finally, delete the iterator $i$ everywhere it has no meaning, you have already $t$. Hence, within the if-construct, use if $ t>=300$ instead.
Holger I. Meinhardt
CC BY-SA 4.0
2020-11-23T12:12:30.090Z
@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.
Hokkaido21
CC BY-SA 4.0
2020-11-23T12:16:43.270Z
@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)?
Hokkaido21
CC BY-SA 4.0
2020-11-23T12:21:16.353Z
I suppose that this is better, since if one does not pay attention $t-1$ can give $0$ for which the iterator is not defined.
Holger I. Meinhardt
<macroeconomics><solow><matlab>
Matlab: Solow Model Convergence of capital
Hokkaido21
# Matlab: Solow Model Convergence of capital

(Asked by: username_0 on 2020-11-23)

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

**Tags:** macroeconomics, solow, matlab

> **Comments:**
> **username_1**: 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.  
> **username_2**: @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  
> **username_1**: Finally, delete the iterator $i$ everywhere it has no meaning, you have already $t$. Hence, within the if-construct, use if $ t>=300$ instead.  
> **username_0**: @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.  
> **username_0**: @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)?  
> **username_1**: I suppose that this is better, since if one does not pay attention $t-1$ can give $0$ for which the iterator is not defined.  

---

## 1 answers

### Answer by username_1 on 2020-11-23. Score: 4 | Accepted answer

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


---

