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]) n = len(ym) # model m = GEKKO() c = m.Array(m.FV,3) for ci in c: ci.STATUS=1 yp = m.Array(m.Var,n) for i in range(n): m.Equation(yp[i]==c[0]+c[1]*x1[i]+c[2]*x2[i]) m.Minimize((yp[i]-ym[i])**2) # add constraint on sum(c[1]*x1) s = m.Var(lb=0,ub=10); m.Equation(s==c[1]*sum(x1)) m.options.IMODE = 3 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)) ypv = [yp[i].value[0] for i in range(n)] plt.plot(ym,ypv,'ro') plt.plot([0,10],[0,10],'k-') plt.xlabel('Meas') plt.ylabel('Pred') plt.savefig('results.png',dpi=300) plt.show()