# How to implement a difference equation in Pyomo

I would like to define a difference equation in pyomo for a temperature variable. The equation (simplified) looks like: $$Temperature(t) = Temperature (t-1) + x(t)$$ , for t>0

$$Temperature (0) = 20$$

So this is what I tried in Pyomo

model.set_timeslots = pyo.RangeSet(0,95)

model.variable_x = pyo.Var(model.set_timeslots, within=pyo.Binary)
model.variable_temperature[0].fix(20)

def temperatureConstraintRule(model):
model.variable_temperature(t) = model.variable_temperature(t-1) + model.variable_x(t)
model.constraint_temperature = pyo.Constraint (model.set_timeslots, rule=temperatureConstraintRule)


However, I get the error message "SyntaxError: can't assign to function call"

Can anybody tell me how I can implement this difference equation in Pyomo?

try:

model.set_timeslots = pyo.RangeSet(0,95)

model.variable_x = pyo.Var(model.set_timeslots, within=pyo.Binary)
i0 = 20

def temperatureConstraintRule(model, t):
if t == model.set_timeslots.first():
return model.variable_x[t] == i0 + model.variable_x[t]
return model.variable_x[t] == model.variable_x[t-1] + model.variable_x[t]
model.constraint_temperature = Constraint(model.set_timeslots, rule=temperatureConstraintRule)

• Cool. Thank you Steven. Your suggested code seems to work. I upvoted and accepted your answer. – PeterBe Feb 22 at 17:37
• Glad it helped @PeterBe – Steven01123581321 Feb 22 at 17:50