## Intermediate Variables and Equations

Intermediate variables are useful to decrease the complexity of the model. These variables store temporary calculations with results that are not reported in the final solution reports. In many models, the temporary variables outnumber the regular variables by many factors. This model reduction often aides the solver in finding a solution by reducing the problem size.

Intermediate variables are declared in the *Intermediates ... End Intermediates* section of the model file. The intermediate variables may be defined in one section or in multiple declarations throughout the model. Intermediate variables are parsed sequentially, from top to bottom. To avoid inadvertent overwrites, intermediate variable can be defined once. In the case of intermediate variables, the order of declaration is critical. If a variable is used before it's definition, it will contain a default value of 1.

### Explicit calculation

The intermediate variables are processed before the implicit equation residuals, every time the solver requests model information. As opposed to implicitly calculated variables, the explicit variables are calculated once and substituted into other explicit or implicit equations.

### Clipping

When the intermediate variable is solely a function of parameters (not variables), the value may be clipped. This is accomplished by adding inequalities to the expression, separated by a comma. The inequalities may also be a function of other intermediate or regular variables.

### Limitations

APMonitor is designed to encourage the user to construct well-posed models for numerical solution. One limitation that may be encountered is the 100 variable limit in each equation. Excessive use of intermediate variables may lead to the violation of this limit. If this limit is encountered, the user can remediate this problem by converting an intermediate variable to a regular implicit variable.

## Example

! Original model Model example Parameters p = 2 End Parameters Variables x y z End Variables Equations exp(x*p)=y z = p*$x + x (y+2/x)^(x*z) * (log(tanh(sqrt(y-x+x^2))+3))^2 = 2+sinh(y)+acos(x+y)+asin(x/y) End Equations End Model ! Model simplified with use of intermediate variables Model example Parameters p = 2 End Parameters Variables x y z End Variables Intermediates exp_result = exp(x*p) left = (y+2/x)^(x*z) * (log(tanh(sqrt(y-x+x^2))+3))^2 right = 2+sinh(y)+acos(x+y)+asin(x/y) End Intermediates Equations exp_result=y z = p*$x + x left = right End Equations End Model |

! Example with intermediate variable clipping Model example Parameters p = 1 End Parameters Variables x End Variables Intermediates pi = p*3.1415, <1.5 End Intermediates Equations x = 0.5 * pi End Equations End Model |