How to loop equations and expressions used by Dynare in matlab context?

5 views (last 30 days)
Hi all,
Could anybody help me with the problem code below?
I want to loop over several equations in matlab, and the file is to be written into one equation each time.Then the file is added in another software platform (i.e., dynare) which is run in matlab. The problem is one of the equations is not straightforward but contains macro variables and loops in dynare format. So it seems such kind of equations or expressions could not be simply in the curley brace.To be concrete, I want to construct the loopover as the example shows below:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rule ={
'a=x;'
'b=y;'
'c=rho+(
@ # for i in number
+eta_nu(-@{i})
@ # endfor
);'
'd=u;'
'e=r;'
};
for ii=1:size(rule,1)
%write equation to file
fid=fopen('policy.mod','w');
fprintf(fid,'%s \n',rule{ii,1});
fclose(fid);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Then the file policy.mod is added into a dynare file to run recursively. The prolem is the equation c can not be in the curley brace. Is there any other way to solve the problem? Could anybody help me ? Thank you very much!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Oct 2021
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rule ={
'a=x;'
'b=y;'
"c=rho+(" + newline + ...
"@ # for i in number" + newline + ...
"+eta_nu(-@{i})" + newline + ...
"@ # endfor " + newline + ...
");"
'd=u;'
'e=r;'
};
fid=fopen('policy.mod','w');
for ii=1:size(rule,1)
%write equation to file
fprintf(fid,'%s \n',rule{ii,1});
end
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
type policy.mod
a=x; b=y; c=rho+( @ # for i in number +eta_nu(-@{i}) @ # endfor ); d=u; e=r;
If there is a particular reason why you need to fopen() inside the loop in your actual code, then you need to switch from 'w' to 'a' so that you append each time instead of overwriting each time.
  1 Comment
JANE TIAN
JANE TIAN on 16 Oct 2021
Thank you so much for quick response, Walter Roberson.That's great help for me. Indeed I need to fopen() inside the loop, andonly one equation is used each time so that the policy.mod file here needs to be overwritten.Many many thanks once again!

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!