Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. from gekko import GEKKO
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. xm = np.array([1,2,3,5,6])
  6. ym = np.array([4.75,4.00,5.25,19.75,36.00])
  7.  
  8. #### Solution
  9. m = GEKKO()
  10. m.options.IMODE=2
  11. # coefficients
  12. c = [m.FV(value=0) for i in range(4)]
  13. x = m.Param(value=xm)
  14. y = m.CV(value=ym)
  15. y.FSTATUS = 1
  16. # polynomial model
  17. m.Equation(y==c[0]+c[1]*x+c[2]*x**2+c[3]*x**3)
  18.  
  19. # linear regression
  20. c[0].STATUS=1
  21. c[1].STATUS=1
  22. m.solve(disp=False)
  23. p1 = [c[1].value[0],c[0].value[0]]
  24.  
  25. # quadratic
  26. c[2].STATUS=1
  27. m.solve(disp=False)
  28. p2 = [c[2].value[0],c[1].value[0],c[0].value[0]]
  29.  
  30. # cubic
  31. c[3].STATUS=1
  32. m.solve(disp=False)
  33. p3 = [c[3].value[0],c[2].value[0],c[1].value[0],c[0].value[0]]
  34.  
  35. # plot fit
  36. plt.plot(xm,ym,'ko',markersize=10)
  37. xp = np.linspace(0,6,100)
  38. plt.plot(xp,np.polyval(p1,xp),'b--',linewidth=2)
  39. plt.plot(xp,np.polyval(p2,xp),'r--',linewidth=3)
  40. plt.plot(xp,np.polyval(p3,xp),'g:',linewidth=2)
  41. plt.legend(['Dane','1 rzedu','2 rzedu','3 rzedu'],loc='best')
  42. plt.xlabel('x')
  43. plt.ylabel('y')
  44. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement