Guest User

Untitled

a guest
Dec 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. %matplotlib inline
  4.  
  5. np.random.seed(2)
  6. X=np.arange(0,1.05,0.05)
  7. y=np.random.randn(21)
  8. cnum=2
  9. constr_X = np.array([0.0,1])
  10. constr_Y = np.array([0,0])
  11.  
  12. d_cnum=2
  13. dconstr_X = np.array([0.0,1])
  14. dconstr_Y = np.array([-3,3])
  15.  
  16. deg = 6
  17. poly = np.zeros((X.shape[0],deg))
  18. constr = np.zeros((constr_X.shape[0], deg))
  19. dconstr = np.zeros((dconstr_X.shape[0], deg))
  20. for i in range(deg):
  21. poly[:,i] = X**(i)
  22. for i in range(deg):
  23. constr[:,i] = constr_X**(i)
  24. for i in range(1,deg):
  25. dconstr[:,i] = i*dconstr_X**(i-1)
  26.  
  27. XX = np.dot(poly.T, poly)
  28. Xy = np.dot(poly.T, y)
  29. constr
  30. total_A = np.zeros((deg+cnum+d_cnum,deg +cnum +d_cnum))
  31. total_A[:deg,:deg] = XX
  32. total_A[deg:deg+cnum,:deg ] = constr
  33. total_A[:deg ,deg:deg+cnum] = constr.T
  34. total_A[deg+cnum:,:deg ] = dconstr
  35. total_A[:deg,deg+cnum:] = dconstr.T
  36.  
  37. total_B = np.zeros(deg+cnum + d_cnum)
  38. total_B[:deg] = Xy
  39. total_B[deg:deg+cnum] = constr_Y
  40. total_B[deg+cnum:deg+cnum+d_cnum] = dconstr_Y
  41.  
  42. solved = np.linalg.solve(total_A, total_B)[:deg]
  43. plt.scatter(X,y)
  44. plt.plot(X, np.dot(poly,solved))
Add Comment
Please, Sign In to add comment