Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gekko import GEKKO
- import numpy as np
- import matplotlib.pyplot as plt
- xm = np.array([1,2,3,5,6])
- ym = np.array([4.75,4.00,5.25,19.75,36.00])
- #### Solution
- m = GEKKO()
- m.options.IMODE=2
- # coefficients
- c = [m.FV(value=0) for i in range(4)]
- x = m.Param(value=xm)
- y = m.CV(value=ym)
- y.FSTATUS = 1
- # polynomial model
- m.Equation(y==c[0]+c[1]*x+c[2]*x**2+c[3]*x**3)
- # linear regression
- c[0].STATUS=1
- c[1].STATUS=1
- m.solve(disp=False)
- p1 = [c[1].value[0],c[0].value[0]]
- # quadratic
- c[2].STATUS=1
- m.solve(disp=False)
- p2 = [c[2].value[0],c[1].value[0],c[0].value[0]]
- # cubic
- c[3].STATUS=1
- m.solve(disp=False)
- p3 = [c[3].value[0],c[2].value[0],c[1].value[0],c[0].value[0]]
- # plot fit
- plt.plot(xm,ym,'ko',markersize=10)
- xp = np.linspace(0,6,100)
- plt.plot(xp,np.polyval(p1,xp),'b--',linewidth=2)
- plt.plot(xp,np.polyval(p2,xp),'r--',linewidth=3)
- plt.plot(xp,np.polyval(p3,xp),'g:',linewidth=2)
- plt.legend(['Dane','1 rzedu','2 rzedu','3 rzedu'],loc='best')
- plt.xlabel('x')
- plt.ylabel('y')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement