import numpy as np from gekko import GEKKO # load data x1 = np.array([1,2,5,3,2,5,2]) x2 = np.array([5,6,7,2,1,3,2]) ym = np.array([3,2,3,5,6,7,8]) # model m = GEKKO() c = m.Array(m.FV,3) for ci in c: ci.STATUS=1 x1 = m.Param(value=x1) x2 = m.Param(value=x2) ymeas = m.Param(value=ym) ypred = m.Var() m.Equation(ypred == c[0] + c[1]*x1 + c[2]*x2) # add constraint on sum(c[1]*x1) with vsum v1 = m.Var(); m.Equation(v1==c[1]*x1) con = m.Var(lb=0,ub=10); m.Equation(con==m.vsum(v1)) m.Minimize((ypred-ymeas)**2) m.options.IMODE = 2 m.solve() print('Final SSE Objective: ' + str(m.options.objfcnval)) print('Solution') for i,ci in enumerate(c): print(i,ci.value[0]) # plot solution import matplotlib.pyplot as plt plt.figure(figsize=(8,4)) plt.plot(ymeas,ypred,'ro') plt.plot([0,10],[0,10],'k-') plt.xlabel('Meas') plt.ylabel('Pred') plt.savefig('results.png',dpi=300) plt.show()