Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. px = py = []
  5. with open("data8//data15.txt") as file:
  6. for line in file.readlines():
  7. lines = line.split()
  8. px = np.append(px, float(lines[0].strip()))
  9. py = np.append(py, float(lines[1].strip()))
  10.  
  11. n = len(px)
  12. f = {}
  13. for i in range(0, n):
  14. f[px[i]] = py[i]
  15.  
  16.  
  17. def fun1(x2, x1):
  18. return (f.get(x2) - f.get(x1)) / (x2 - x1)
  19.  
  20.  
  21. def fun2(x3, x2, x1):
  22. return (fun1(x3, x2) - fun1(x2, x1)) / (x3 - x1)
  23.  
  24.  
  25. def fun3(x4, x3, x2, x1):
  26. return (fun2(x4, x3, x2) - fun2(x3, x2, x1)) / (x4 - x1)
  27.  
  28.  
  29. def fun4(x5, x4, x3, x2, x1):
  30. return (fun3(x5, x4, x3, x2) - fun3(x4, x3, x2, x1)) / (x5 - x1)
  31.  
  32.  
  33. def fun5(x6, x5, x4, x3, x2, x1):
  34. return (fun4(x6, x5, x4, x3, x2) - fun4(x5, x4, x3, x2, x1)) / (x6 - x1)
  35.  
  36.  
  37. def fun6(x7, x6, x5, x4, x3, x2, x1):
  38. return (fun5(x7, x6, x5, x4, x3, x2) - fun5(x6, x5, x4, x3, x2, x1)) / (x7 - x1)
  39.  
  40.  
  41. def fun7(x8, x7, x6, x5, x4, x3, x2, x1):
  42. return (fun6(x8, x7, x6, x5, x4, x3, x2) - fun6(x7, x6, x5, x4, x3, x2, x1)) / (x8 - x1)
  43.  
  44.  
  45. def fun8(x9, x8, x7, x6, x5, x4, x3, x2, x1):
  46. return (fun7(x9, x8, x7, x6, x5, x4, x3, x2) - fun7(x8, x7, x6, x5, x4, x3, x2, x1)) / (x9 - x1)
  47.  
  48.  
  49. def fun9(x10, x9, x8, x7, x6, x5, x4, x3, x2, x1):
  50. return (fun8(x10, x9, x8, x7, x6, x5, x4, x3, x2) - fun8(x9, x8, x7, x6, x5, x4, x3, x2, x1)) / (x10 - x1)
  51.  
  52.  
  53. def fun10(x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1):
  54. return (fun9(x11, x10, x9, x8, x7, x6, x5, x4, x3, x2) - fun9(x10, x9, x8, x7, x6, x5, x4, x3, x2, x1)) / (x11 - x1)
  55.  
  56. b = [1]
  57. b.append(f.get(px[0]))
  58. b.append(fun1(px[1], px[0]))
  59. b.append(fun2(px[2], px[1], px[0]))
  60. b.append(fun3(px[3], px[2], px[1], px[0]))
  61. b.append(fun4(px[4], px[3], px[2], px[1], px[0]))
  62. b.append(fun5(px[5], px[4], px[3], px[2], px[1], px[0]))
  63. b.append(fun6(px[6], px[5], px[4], px[3], px[2], px[1], px[0]))
  64. b.append(fun7(px[7], px[6], px[5], px[4], px[3], px[2], px[1], px[0]))
  65. b.append(fun8(px[8], px[7], px[6], px[5], px[4], px[3], px[2], px[1], px[0]))
  66. b.append(fun9(px[9], px[8], px[7], px[6], px[5], px[4], px[3], px[2], px[1], px[0]))
  67. b.append(fun10(px[10], px[9], px[8], px[7], px[6], px[5], px[4], px[3], px[2], px[1], px[0]))
  68.  
  69. def newton(x):
  70. factor = b[1]
  71. out = factor
  72. for i in range(2, n+1):
  73. factor = factor*(b[i]/b[i-1])*(x-px[i-2])
  74. out += factor
  75. return out
  76.  
  77. i = np.linspace(min(px), max(px), 200)
  78. plt.plot(px, py, 'r.', i, newton(i))
  79. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement