# Call matlab function within model block

Hi, I have two questions.

2. how to call a matlab function within the model block?

My purpose is to have a loop over parameters to see how IRF and other endogenous variables behave. Because of changing exogenously specified deep parameters, the utility function form and thus the marginal utility form in the Euler equation will change, as well as all steady state values.

The scenario is like this: first, I have defined a matlab function “solveSS” that with solve for steady state values of all endogenous variables according to exogenously given deep parameters (all deep parameters are exogenously specified); second, I have defined a matlab function call “unmu” which return utility and marginal utility when two variables and some exogenously specified parameters are input, so

```function =unmu(c,h,paras) if paras==1 u=....% equation that calculate utility level muc=... % calculate marginal utility of c muh=... % calculate marginal utility of h else u=....% equation that calculate utility level muc=... % calculate marginal utility of c muh=... % calculate marginal utility of h end ```

Obviously, the way to calculate marginal utility depends on the parameters input.

Could anyone provide some hints about how to do such loops?

Ideally, I would think it is feasible to code like this:

``````//**************************************
// dynare mod file
//**************************************

//**************************************
// Declare variables and parameters
//**************************************
var ...
varexo ...
parameters ...

// provide parameter values

for npara=1:length(M_.params)
deep_parameter_name = M_.param_names(npara,:);
eval('M_.params(npara)  = ' deep_parameter_name ' ;'])
end

....

//**************************************
model;
//**************************************
**please, how to call matlab function 'unmu' I defined that return marginal utility to me such that I can use it in my Euler equation?**
// Function 'unmu' will give me =unmu(c,h,parameters), where  are utility level, marginal utility of c and h, respectively,
//but I only need muc and muh
// so could I write like this:

=unmu(c,h,parameters);
muc=beta*mucXXXXXXXXXXXXXXXXXXXXXXXXXXXX // No, wrong! Do not know how to do it, I need c(+1) and unmu(c(+1),h(+1), parameters) but this cannot be handle in unmu function

end;``````

Could someone provide samples that have similar scenarios?

I am sure you can call a matlab function outside the model block (I do it myself) however I am not sure if this is feasible within it. Btw, why does your MU change so dramatically with parametres? I mean, I would understand if you changed the functional form.

Kyriacos

[quote=“kyri82”]I am sure you can call a matlab function outside the model block (I do it myself) however I am not sure if this is feasible within it. Btw, why does your MU change so dramatically with parametres? I mean, I would understand if you changed the functional form.

Kyriacos[/quote]

Yes the utility function form changes, e.g., from CES to Cobb-Douglas, and from power utility to log utility, according to parameters input.

From my understanding, you want to determine at the beginning which parameter and thus utility function to use, but having only one mod-file instead of 2. Your best shot seems to be the Dynare macro language that would paste the required utility function depending on your specification. Using an external file seems not sensible to me as you lose Dynare’s ability to take (higher order) derivatives for you.

Thank you very much.
Instead of trying a loop over the mod file, I may turn to specify separate mod file according to the utility function and thus the Euler equation now because I have not mastered Dynare macro language yet. Learn it later.