SHARE
TWEET

Untitled

a guest Apr 18th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import math
  4.  
  5. x = np.array([0.173, 0.518, 0.864, 1.209, 1.555, 1.901, 2.246, 2.592, 2.937], dtype=float)
  6. y = np.array([-1.547, -0.852, 0.063, -0.470, -0.750, -1.142, -1.052, -1.188, -0.962], dtype=float)
  7.  
  8. '''(y - (a * sqrt(x) + b * sin(x) + c)) ^ 2
  9.  
  10. 2 * (y - (a * sqrt(x) + b * sin(x) + c)) * sqrt(x) = 0
  11. 2 * (y - (a * sqrt(x) + b * sin(x) + c)) * sin(x) = 0
  12. 2 * (y - (a * sqrt(x) + b * sin(x) + c)) = 0
  13.  
  14. 2*y*sqrt(x) - 2*x*a - 2*sin(x)*sqrt(x)*b - 2*sqrt(x)*c = 0
  15. 2*y*sin(x) - 2*sqrt(x)*sin(x)*a - 2*sin^2(x)*b - 2*sin(x)*c = 0
  16. 2*y - 2*sqrt(x)*a - 2*sin(x)*b - 2*c = 0
  17.  
  18. 2*x*a + 2*sin(x)*sqrt(x)*b + 2*sqrt(x)*c = 2*y*sqrt(x)
  19. 2*sqrt(x)*sin(x)*a + 2*sin^2(x)*b + 2*sin(x)*c = 2*y*sin(x)
  20. 2*sqrt(x)*a + 2*sin(x)*b + 2*c = 2*y'''
  21.  
  22. sqrtX = np.sqrt(x)
  23. sinX = np.sin(x)
  24.  
  25. sumX = np.sum(x)
  26. sumSinSqrtX = np.sum(sqrtX * sinX)
  27. sumSqrtX = np.sum(sqrtX)
  28. sumYSqrtX = np.sum(y * sqrtX)
  29. sumSinSecondDegreeX = np.sum(sinX * sinX)
  30. sumSinX = np.sum(sinX)
  31. sumYSinX = np.sum(y * sinX)
  32. sumY = np.sum(y)
  33.  
  34. matrixA = np.array([[2*sumX, 2*sumSinSqrtX, 2*sumSqrtX],
  35.                     [2*sumSinSqrtX, 2*sumSinSecondDegreeX, sumSinX],
  36.                     [2*sumSqrtX, 2*sumSinX, 2.0]], dtype=float)
  37. matrixB = np.array([2*sumYSqrtX, 2*sumYSinX, 2*sumY], dtype=float)
  38.  
  39.  
  40. answer = np.linalg.solve(matrixA, matrixB)
  41. print(answer)
  42.  
  43. xnew = np.linspace(np.min(x), np.max(x), 100)
  44. ynew = [-1.0706749 * math.sqrt(x) + 0.56973596 * math.sin(x) + 0.14997273 for x in xnew]
  45.  
  46. plt.plot(x, y, 'o', xnew, ynew, color='red')
  47. plt.grid(True)
  48. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top