Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4.  
  5. def f(x):
  6.     return x - math.sin(x) - 0.25
  7.  
  8.  
  9. a = -1
  10. b = 1
  11.  
  12. n = 6
  13. xr = np.linspace(a, b, n);
  14. yr = np.array(list(map(f, xr)))
  15.  
  16. xch = np.zeros(n)
  17.  
  18. for i in range(n):
  19.     xch[i] = 0.5*((b- a)*math.cos((2*i + 1) / (2*n ) * math.pi) + (b + a))
  20.  
  21. ych = np.array(list(map(f, xch)))
  22.  
  23. xx = xr
  24.  
  25.  
  26. yy = yr
  27.  
  28. def lagrange(x):
  29.     def l(k):
  30.         a = 1
  31.         for i in range(n):
  32.             if i != k:
  33.                 a *= (x - xx[i])
  34.         b = 1
  35.         for i in range(n):
  36.             if i != k:
  37.                 b *= (xx[k] - xx[i])
  38.         return a / b;
  39.     res = 0
  40.     for i in range(n):
  41.         res += yy[i]*l(i)
  42.     return res
  43.  
  44.  
  45.  
  46.  
  47.  
  48. xg = np.arange(a, b + 0.1, 0.1)
  49.  
  50. maxr = 0
  51.  
  52. for cur in xg:
  53.     maxr = max(maxr, abs(f(cur) - lagrange(cur)))
  54.  
  55. print(maxr)
  56.  
  57.  
  58.  
  59. def f1(x):
  60.     return 1 - math.cos(x)
  61.  
  62. def f2(x):
  63.     return math.sin(x)
  64.  
  65. p = 1
  66. k = 3
  67.  
  68. xe = np.linspace(a, b, k)
  69.  
  70.  
  71. ye0 = np.array(list(map(f, xe)))
  72. ye1 = np.array(list(map(f1, xe)))
  73. ye2 = np.array(list(map(f2, xe)))
  74.  
  75. m = k*(p + 1)
  76.  
  77. X = np.zeros((m, m))
  78. for i in range(k):
  79.     for j in range(p + 1):
  80.         for l in range(0, m - j):
  81.             X[i*(p+ 1) + j][l] = (xe[i]**(m -  1 - l - j)) * math.factorial(m - 1- l)/math.factorial(m - 1- j - l)
  82.  
  83. Y = np.zeros(m)
  84.  
  85. Y[0] = ye0[0]
  86. Y[1] = ye1[0]
  87.  
  88. Y[2] = ye0[1]
  89. Y[3] = ye1[1]
  90.  
  91. Y[4] = ye0[2]
  92. Y[5] = ye1[2]
  93.  
  94.  
  95. h = np.linalg.solve(X, Y)
  96.  
  97. print(X)
  98. print(Y)
  99.  
  100. def e(x):
  101.     res = 0
  102.     for i in range(m):
  103.         res += (x**i) * (h[m - 1- i])
  104.     return res
  105.  
  106. def dedx(x):
  107.     res = 0
  108.     for i in range(m):
  109.         res += i*(x**(i - 1)) * (h[m - 1- i])
  110.     return res
  111.  
  112. xg = np.arange(a, b + 0.1, 0.1)
  113.  
  114. maxr = 0;
  115. for curx in xg:
  116.     maxr = max(maxr, abs(f(curx) - e(curx)))
  117.  
  118.  
  119. yg = np.array(list(map(f1, xg)))
  120. plt.plot(xg, yg)
  121.  
  122.  
  123. yg = np.array(list(map(dedx, xg)))
  124. plt.plot(xg, yg)
  125.  
  126. p = 0
  127. k = 6
  128.  
  129. xe = np.linspace(a, b, k)
  130.  
  131. m = k*(p + 1)
  132.  
  133. X = np.zeros((m, m))
  134. for i in range(k):
  135.     for j in range(p + 1):
  136.         for l in range(0, m - j):
  137.             X[i*(p+ 1) + j][l] = (xe[i]**(m -  1 - l - j)) * math.factorial(m - 1- l)/math.factorial(m - 1- j - l)
  138.  
  139. Y = np.zeros(m)
  140.  
  141. ye0 = np.array(list(map(f, xe)))
  142.  
  143. Y[0] = ye0[0]
  144. Y[1] = ye0[1]
  145.  
  146. Y[2] = ye0[2]
  147. Y[3] = ye0[3]
  148.  
  149. Y[4] = ye0[4]
  150. Y[5] = ye0[5]
  151.  
  152.  
  153. h = np.linalg.solve(X, Y)
  154.  
  155.  
  156.  
  157. yg = np.array(list(map(dedx, xg)))
  158. plt.plot(xg, yg)
  159.  
  160.  
  161.  
  162.  
  163. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement