 Calculating Expected Value Function in Dynare: issues with endogenous variables and external functions

I have a Bellman Equation and I am trying to estimate its expected value, EV, by iteration. My first guess for this value is EV=0, and from that I am supposed to estimate another guess as follows:

And iterate until convergence. Here, x and i are endogenous variables (with x being continuous and i a dummy with value either 0 or 1), y is the future value of x and j the future value of i. θ is the set of parameters, which is known. Function u is also known and is linear. P is a four by four transition matrix, composed of some of the parameters included in θ. I am trying to code this in Dynare:

var EV;

parameters beta RC theta_11 theta_30 theta_31 theta_32 theta_33;

beta = 0.9999;

RC = 9.7687;

theta_11 = 1.3428;

model;

P = [1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1];

EV = integral(log(exp(-theta_11x + betaEV(+1))+exp(-RC + betaEV(+1))))P;

end;

initval;

EV = 0;

end;

However, I run into a number of problems with this:

1. Dynare won’t create matrix P so I think I will need to add P in matlab and then multiplicate the function. This is fine by me, but if someone has a simpler approach, I would love to hear it.
2. As you can see from the intitial equation, EV is a function of i and x and I don’t think the simplified version in my code is capturing this correctly, but it’s the best I knew how to do. Does anyone know how to make the value function depend on two values?
3. MOST IMPORTANTLY: when I run this code in Dynare, an error occurs reading “To use an external function (integral) within the model block, you must first declare it via the external_function() statement.” I tried to solve this by using

ThemeCopy

external_function (integral)

And putting it at different places throughout the code. However, the error message or a similar one always shows up. Does anyone know what to do?