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:

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})”

It would be great if you could provide an MWE - 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.

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

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

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?

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.

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:

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