Can I run dynare silently in a matlab function?


When running Dynare with simul, it generates parameters and paths of endogenous variables in the workspace, even if I call Dynare inside a Matlab function. Is there a way to run Dynare silently inside a Matlab function so that I can use only the information that I needed?

I can provide my code but I am not sure it’s necessary for my question.

Thank you.

Yes, that should be possible, but it’s easier with the codes.

Hi, Professor Pfeifer

Here is a minimal working example of my question. The ‘main.m’ calls a Matlab function ‘myfunction.m’, which then calls Dynare to solve a perfect foresight solution. Usually only the output of ‘myfunction.m’ will show up in the workspace. However, all Dynare’s output, which should be local inside the function, shows up in the upper-level workspace.

main.m (378 Bytes)
myfunction.m (672 Bytes)
rbc0.mod (1.6 KB)

You are approaching this way too complicated. Use
rbc0.mod (1.5 KB)
myfunction.m (382 Bytes)

Sorry, I don’t understand your solution. Maybe I did not explain my problem clearly. I want to loop over the initial value of the state variable K when solving perfect foresight solutions. Therefore I plan to use “main.m” to write my loop, which in each iteration calls Dynare inside “myfunction.m”. The problem is that Dynare returns all its output in the upper-level workspace (main.m) even if I call it inside a function.

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;

[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;

  k = K_ini;
  c = 0.76;
  n = 0.3;
  z = 0; 
  e = 0;


perfect_foresight_setup(periods = 200);

for ii=1:length(Knew_vec);
    output(ii,:) = myfunction(Knew_vec(ii),M_,oo_,options_);


function output = myfunction(Knew,M_,oo_,options_)


oo_ = perfect_foresight_solver_core(M_,options_,oo_);
if ~oo_.deterministic_simulation.status
    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];
1 Like

Thank you very much. This works for my purpose.

Do we always run Dynare directly, rather than call it inside a Matlab function?

It depends what you are trying to do. But often there is no reason for preprocessing the model again. Almost always, loops can be done at the level of Matlab functions.
Or does your question refer to my Matlab code being in the mod-file? That could be easily changed. You could have a main Matlab file that calls Dynare once and then does the rest. The important part is not to loop over Dynare.

I got your point that I should avoid looping over Dynare.

I am solving a system of equations (the steady state model) using fsolve, where some equations involve the dynamic paths deviating from the steady state. I plan to use Dynare to solve such dynamic paths and then return the results to fsolve. That’s why I want to call Dynare inside Matlab functions. Effectively I am looping over Dynare because of fsolve. I know it’s kind of slow but I think this is a natural way.

What concerns me is that even I call Dynare inside Matlab functions, somehow it returns all its output (parameters, paths of all endogenous variables, etc.) to the upper-level workspace. This means Dynare is not like a normal function that can be called inside other functions. I worry that this may mess up the whole thing.

This then is more complicated. What you provided was then not a MWE. So the inner loop involves Dynare, does the outer loop as well?


The code I uploaded was not an MWE for the whole project. It was only to address my concern: calling Dynare in a Matlab function returns a bunch of additional things in the workspace.

The whole project was going to be like this: 1) the “main.m” calls fsolve, which uses “myfunction”; 2) “myfunction” calls the Dynare mod (the original mod file without your modification for loops). I am effectively looping over Dynare because fsolve uses an iterative algorithm. And this is the only loop. The MWE I uploaded mimics a single iteration in fsolve.

There are two separate issues.

  1. Regarding the workspace, in dynare.m you would need to modify the line
    evalin('base',[fname '.driver']);


    evalin('caller',[fname '.driver']);

But this change will most probably break some Dynare functionality.
2. With that change, you still should not loop over Dynare. Rather, you can use
main.m (432 Bytes)
Dynare_caller.m (72 Bytes)
myfunction.m (439 Bytes)
which shows how to call fsolve in this context.

Thank you very much for the help. It works perfectly. Just want to confirm a few minor issues:

  1. I think I can call
    dynare rbc0 noclearall;
    directly in “main.m” to set up Dynare and I don’t need “Dynare_caller.m”. Otherwise, in “Dynare_caller.m” I need to add
    global M_ options_ oo_

  2. After running the program, the endogenous variables that remained in the workspace are Not those values that solved by fsolve. In your example code, the solution is k = 7.9185 such that c1 = 0.7. What remained in the workspace is the result of the very first call of Dynare (Dynare_caller).

Thanks again for your very patient help. I learned a lot from the conversation.

  1. I don’t really understand why you would need global variables. The Dynare_caller-function passes the three variables back. The reason I put Dynare into a function is that you did not want Dynare to put anything into the base workspace.
  2. Yes, that was the point.

In my code, Dynare does not put anything into the base workspace if you do the change in dynare.m to evalin('caller',[fname '.driver']);
3. If you are fine with some stuff in the base workspace, then do not do the change and simply call Dynare in main.m once. This will have a lot fewer side effects.
4. Potentially, the best solution is simply to have main.m be

dynare rbc0 noclearall;

clearvars -except M_ oo_ options_ 



That keeps the workspace clean, calls Dynare once, and requires no modifications to Dynare functions.

If I call “Dynare_caller.m”, it stops with an error:

Output argument “M_” (and maybe others) not assigned during call to
Error in main (line 12)

That’s why I may need global M_ options_ oo_

And I agree with you that with your suggested solution, leaving some stuff in the workspace is fine.