# How to define JuMP expressions using a loop

**URL:** <https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [January 27, 2021, 8:51pm UTC](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079 "2021-01-27T20:51:15Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 27, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079/2 "2021-01-27T22:03:48Z")

</div>

This is a reasonable way to initialize this. The only thing to change is that you should initialize `BatteryCap = Array{AffExpr,2}(undef, nHours, n)`. (Linear JuMP expressions are type `AffExpr`, not `Float64`.)

The other option is to write it as something like

```nohighlight
@expression(
    model,
    BatteryCap[k = 1:nHours, j = 1:n],
    initialCapacity[j] + sum(Charging[i, j] - Discharging[i, j] for i = 2:k)
)

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079)._
