how to solve ini equation problem using gekko?

Viewed 19

If I want use V1_0 represent the initiate value of V1 and V2_0 represent the initiate value of V2,and has the initiate equation V2_0-V1_0=5,how should I to use gekko to express this relation?

1 Answers

For that level of control, I recommend writing out the dynamic problem as arrays. You'll need to include your own collocation equations if you have differential equations.

from gekko import GEKKO
m = GEKKO()
n  = 5
V1 = m.Array(m.Var,n)
V2 = m.Array(m.Var,n)
# rename initial conditions
V1_0 = V1[0]; V2_0 = V2[0]
m.Equation(V1_0==3)
m.Equation(V2_0-V1_0==5)
for i in range(1,n):
    m.Equation(V1[i]==V1[i-1]+1)
    m.Equation(V2[i]==V2[i-1]+0.5)
m.solve(disp=False)
print(V1)
print(V2)

This produces the solution for V1 and V2:

[[3.0] [4.0] [5.0] [6.0] [7.0]]
[[8.0] [8.5] [9.0] [9.5] [10.0]]

If you are using a dynamic mode (IMODE=5 or IMODE=6), you can also try the m.Connection() function to connect the initial conditions with V1_0=m.FV() and V2_0=m.FV() with V2_0.STATUS=1. Then you can write the Equation m.Equation(V2_0-V1_0==5).

Related