Hello,

I am using JuMP to solve dynamic programming problem. My constraints are `flow`

and `level`

, which are piecewise and depend on each other. `level[r, i] = f(flow[r, i], level[r, i-1])`

, `flow[r, i] = f(level[r, i], n)`

. The expression should be like as follows. In my case, the only variable is `n[r = 1:N, i = 1:numtime]`

:

```
for r in 1:N
for i in 1:numtime
if i == 1
# Discharge flow for the 1st time segment from upper reservoir
flow[r, 1] = (0.685 - 0.19 * n[r, 1] * 0.05 / (upstream[r].initial_level -
upstream[r].coef_level)) * 0.5 * n[r, 1] * sqrt(2 * 9.81 *
(upstream[r].initial_level - upstream[r].coef_level))
# Water lever of reservoir for the 1st time segment
level[r, 1] = upstream[r].initial_level +
(upstream[r].inflow * raintime[1] * 3600 - flow[r, 1] * 43200) / upstream[r].k
else
flow[r, i] == (0.685 - 0.19 * n[r, i] * 0.05 / (level[r, i-1] -
upstream[r].coef_level)) * 0.5 * n[r, i] * sqrt(2 * 9.81 *
(level[r, i-1] - upstream[r].coef_level))
level[r, i] == level[r, i-1] + (upstream[r].inflow * raintime[i] * 3600
- flow[r, i] * 43200) / upstream[r].k
end
end
end
```

with this written in expression, I want to define the constraints:

```
# constraint of discharge flow from upstream reservoir
@constraint(
model,
upstream[r].min_flow <= flow[r = 1:N, 1:numtime] <= upstream[r].max_flow
)
# constraint of water flow of upstream reservoir
@constraint(
model,
upstream[r].min_level <= level[r = 1:N, 1:numtime] <= upstream[r].max_level
)
```

Question: How can I write `flow`

and `level`

using `@expression`

so that the constraints can be defined? Could you please give me any suggestion?

Thanks!