You are not supposed to run Dynare again. This is inefficient. Also note that you were reading out the first entry of `oo_.endo_simul`

. That is the initial value set by you. It will never change.

All you need for the desired loop is run the mod-file

```
var c k n z;
varexo e;
parameters beta chi delta alpha rho K_ini;
K_ini = 8.5;
alpha = 0.33;
beta = 0.99;
delta = 0.025;
chi = 1.75;
sigma = 0.01;
rho = 0.95;
model;
[name='Euler equation']
(1/c) = beta*(1/c(+1))*(1+alpha*exp(z(+1))*k^(alpha-1)*n(+1)^(1-alpha)-delta);
[name='Consumption/leisure choice ']
chi*c/(1-n) = exp(z)*(1-alpha)*k(-1)^alpha*n^(-alpha);
[name='Resource constraint']
c +k - (1-delta)*k(-1) = exp(z)*k(-1)^alpha*n^(1-alpha);
[name='Technology shock']
z = rho*z(-1)+e;
end;
initval;
k = K_ini;
c = 0.76;
n = 0.3;
z = 0;
e = 0;
end;
check;
perfect_foresight_setup(periods = 200);
Knew_vec=7:0.1:10;
output_vec=NaN(length(Knew_vec),2)
for ii=1:length(Knew_vec);
output(ii,:) = myfunction(Knew_vec(ii),M_,oo_,options_);
end
```

with

```
function output = myfunction(Knew,M_,oo_,options_)
oo_.endo_simul(strmatch('k',M_.endo_names,'exact'),1)=Knew;
oo_ = perfect_foresight_solver_core(M_,options_,oo_);
if ~oo_.deterministic_simulation.status
output=NaN(1,2);
else
C1 = oo_.endo_simul(strmatch('c',M_.endo_names,'exact'),M_.maximum_lag+1);
N1 = oo_.endo_simul(strmatch('n',M_.endo_names,'exact'),M_.maximum_lag+1);
output = [C1, N1];
end
```