The use of a model variable in an m-file

I’m trying to create an m-file to find two policy parameters that minimize my welfare loss function. To do so, I will use the fminsearch function and loop over these two parameters. However, when I call the welfare loss function in the m-file (defined as a variable outside the model block in my mod-file), I get this "Undefined function or variable ‘welfare_loss’ ". How can I fixe this problem?
Thank you in advance for your help.

I fixed it.

hello, I encounter the same problem about how to get the optimal policy paremerters, can you give me advice on how to solve it. Thank you.

Search the forum. There are various posts on this, e.g.

Thank you, professor.

I don’t know if you fixed it, but the solution for me was to define the welfare loss (the variable which was defined outside the model block in my mod-file) inside the m-file as well.

Thank you for your reply. I still don’t know how to solve it, can you give me some advice on how to deal with it?
Best wishes,

Do you have exactly the same problem? i.e. Matlab does not recognize the variable defined outside the model block.

No, I followed the others .mod code, defining the variable after stoch_simul to find the optimal parameters. But I don’t know if my code is right.

I can take a look at it if you want.

thank you very much, my model inlcudes one monetary policy (nominal rate) and two macroprudential instruments, I want to solve the optimai parameters in the views of cooperation and noncooperation, can you give me your email? thank you.