Summation of array variable in gekko

Viewed 353

I tried this code but it seems that something is wrong. Normaly print (z.value) must return a single number but in my case it returns an array. could someone help please

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
xm = np.array([0,1,2,3,4,5])
ym = np.array([0.1,0.2,0.3,0.5,0.8,2.0])
m = GEKKO()
x = m.Param(value=xm)
a = m.FV()
a.STATUS=1
y = m.CV(value=ym)
y.FSTATUS=1

z = None

m.Equation(y==0.1*m.exp(a*x))

z = m.Intermediate(sum([y[i] for i in range(len(y))]))


m.options.IMODE = 2

m.solve(disp=False)

print(z.value)

print(y.value)

1 Answers

Use z=m.vsum(y) to create a summation of the variable across the data sets with IMODE=2.

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
xm = np.array([0,1,2,3,4,5])
ym = np.array([0.1,0.2,0.3,0.5,0.8,2.0])
m = GEKKO()
x = m.Param(value=xm)
a = m.FV()
a.STATUS=1
y = m.CV(value=ym)
y.FSTATUS=1
m.Equation(y==0.1*m.exp(a*x))

z = m.vsum(y)

m.options.IMODE = 2
m.solve(disp=False)
print(z.value)
print(y.value)

Your method to define z creates a summation of the initial values ym. When defining a model in Gekko, it is important to realize that the model is defined once for each data set. Gekko then takes the model and solves it for each data set. This is also the same approach when solving over multiple time points. Even though multiple values are reported for z, there is only one unique value (4.3146).

Related