# For loop error

**URL:** <https://discourse.julialang.org/t/for-loop-error/26210>\
**Category:** Optimization (Mathematical)\
**Created:** [July 10, 2019, 6:58am UTC](https://discourse.julialang.org/t/for-loop-error/26210 "2019-07-10T06:58:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 6:58am UTC](https://discourse.julialang.org/t/for-loop-error/26210/1 "2019-07-10T06:58:14Z")

</div>

Hi,  
I’m still new to Julia, and am having problems with a for loop. I’m trying to create an array where each value is the former value minus another value. This is the code:

```julia
for i = 1:T 
     bal[i] = bal[i-1] - A[i]
end

```

where bal and A are arrays of the same size. The error I get is “MethodError: no method matching -(::VariableRef, ::Array{VariableRef,1})”

Please help, thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 10, 2019, 7:06am UTC](https://discourse.julialang.org/t/for-loop-error/26210/2 "2019-07-10T07:06:24Z")

</div>

It would be great if you could provide an [MWE](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) - the error is telling you that for the type of variables you’re using, a subtraction operation is not defined, but without more context it’s hard to say where these types come from and why (if at all) you’d need them.

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 7:12am UTC](https://discourse.julialang.org/t/for-loop-error/26210/3 "2019-07-10T07:12:28Z")

</div>

Oh alright, thanks for the link to the page as well. Here’s an MWE, I hope this would be enough.

```julia
using JuMP

m = Model()
T = 300

@variables(m, begin
    delta0 >= 0
    amort[i=1:T] >= 0
	bal[0:T] >= 0
end)

amort = delta0/T
bal[0] = delta0

for i = 1:T
	bal[i] = bal[i-1] - amort
end

```

If there is a better way to do this please let me know.

Thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 10, 2019, 8:05am UTC](https://discourse.julialang.org/t/for-loop-error/26210/4 "2019-07-10T08:05:36Z")

</div>

Running your code seems to fail with a different error for me

```julia
for i = 1:T 
    bal[i] = bal[i-1] - amort
end

ERROR: MethodError: Cannot `convert` an object of type GenericAffExpr{Float64,VariableRef} to an object of type VariableRef
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:154
  VariableRef(::Any, ::Any) at C:\Users\####\.julia\packages\JuMP\ibcEh\src\variables.jl:99

```

So what’s happening here is:

```julia
julia> typeof(bal)
JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{UnitRange{Int64}},Tuple{Dict{Int64,Int64}}}

julia> typeof(bal[0])
VariableRef

julia> typeof(amort)
GenericAffExpr{Float64,VariableRef}

julia> typeof(bal[0] - amort)
GenericAffExpr{Float64,VariableRef}

```

Subtracting `amort` from an element in your `bal` array produces an object of type `GenericAffExpr{Float64, VariableRef}`, which then can’t be assigned to the JuMP `DenseAxisArray` which is expecting an object of type `VariableRef`

Unfortunately I’ve never really used JuMP so I’m not sure what a workaround might be. Maybe you could explain what you’re trying to do?

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 8:12am UTC](https://discourse.julialang.org/t/for-loop-error/26210/5 "2019-07-10T08:12:57Z")

</div>

Oh alright, yeah I’m getting the same error too actually, and I sort of understand why it gives an error, but I’m not sure what else to do. I don’t think this particular issue is related to JuMP tho. So here’s basically what I want.

Let’s say I have an amount of $10 at time period t = 0, and at the beginning of each period I want to deduct $1. So the formula would be at time t, amount owned would be the amount at period (t-1) minus the $1, correct? How would I do this in Julia? In my code, bal is the amount and amort is the amount to be subtracted.

Would really appreciate any advice, thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 10, 2019, 8:18am UTC](https://discourse.julialang.org/t/for-loop-error/26210/6 "2019-07-10T08:18:27Z")

</div>

That’s simple enough, although I’m not sure how it relates to what you’re trying to do in JuMP - what you’re trying to do works if you do it with regular numbers (e.g. `Float64`) rather than references:

```julia
balance = zeros(10)
balance[1] = 10 # initial amount owed
amort = 1 # repayment per period

julia> for i in 2:length(balance)
           balance[i] = balance[i-1] - amort
       end

julia> balance
10-element Array{Float64,1}:
 10.0
  9.0
  8.0
  7.0
  6.0
  5.0
  4.0
  3.0
  2.0
  1.0

```

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 9:28am UTC](https://discourse.julialang.org/t/for-loop-error/26210/7 "2019-07-10T09:28:55Z")

</div>

Thank you! I completely forgot about trying regular numbers instead of JuMP variables.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 10, 2019, 9:46am UTC](https://discourse.julialang.org/t/for-loop-error/26210/8 "2019-07-10T09:46:56Z")

</div>

That is an impressive commitment to optimization - glad it works now!
