Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def read_inputs(filename):
  4. with open(filename) as f:
  5. x, y = [], []
  6. for line in f.readlines():
  7. coord = [float(n) for n in line.strip().split()]
  8. x.append(coord[0])
  9. y.append(coord[1])
  10. return x, y
  11.  
  12.  
  13. def read_from_file(file_name):
  14. f=open("table.txt","r")
  15. lines=f.readlines()
  16. x=[]
  17. y=[]
  18.  
  19. for q in lines:
  20. temp = q.strip().split()
  21. x.append(float(temp[0]))
  22. y.append(float(temp[1]))
  23.  
  24. x = np.array(x)
  25. y = np.array(y)
  26. return x,y
  27. x, y = read_from_file("table.txt")
  28.  
  29. def diff(x, y):
  30. '''x : array of data points
  31. y : array of f(x) '''
  32. n = len(x)
  33. a = []
  34. for i in range(n):
  35. a.append(y[i])
  36.  
  37. for j in range(1, n):
  38.  
  39. for i in range(n-1, j-1, -1):
  40. a[i] = float(a[i]-a[i-1])/float(x[i]-x[i-j])
  41.  
  42. return np.array(a)
  43.  
  44. the_values = diff(x, y)
  45. def write_to_file(the_array, file_name):
  46. my_list = the_array.tolist()
  47. with open(file_name, 'w') as f:
  48. for item in my_list:
  49. f.write("%s\n" % item)
  50. write_to_file(diff(x,y), "divided_differences.txt")
  51.  
  52.  
  53. def Horner(abscissaes, newton_diff, x):
  54. if len(abscissaes) == 1:
  55. # Exit condition.
  56. return newton_diff[0]
  57.  
  58. term, newton_diff = newton_diff[0], newton_diff[1:]
  59. abscissae, abscissaes = abscissaes[0], abscissaes[1:]
  60. next = Horner(abscissaes, newton_diff, x)
  61. return term + (x - abscissae) * next
  62.  
  63.  
  64. def Horner_not_recusrive(abscissaes, newton_diff, x):
  65. # Reverse iteration, starting from the end makes it easier.
  66. value = newton_diff[-1]
  67. for i, abscissae in reversed(list(enumerate(abscissaes[:-1]))):
  68. term = newton_diff[i]
  69. value = term + (x - abscissae) * value
  70. return value
  71.  
  72.  
  73. abscissaes, ordinates = read_inputs("table.txt")
  74. diff = newton_diff(abscissaes, ordinates)
  75. x = 20
  76. print(Horner_not_recusrive(np.array(abscissaes), diff, x))
  77. print(Horner(np.array(abscissaes), diff, x))
  78. print(-0.5 + (x - 1) * (0.9 + (x - 2) * (-0.216667 + (x - 4) * (0.0285714 + (x - 8) * (-0.00199562)))))
  79.  
  80. def write_to_files( shortfilename, longfilename, npoints, listx, diff, a, b ):
  81. shortfile = open(shortfilename, "w")
  82. longfile = open(longfilename, "w")
  83. shortfile.writelines(["%.20f\t" % item for item in diff])
  84. shortfile.close()
  85.  
  86. # we create a mesh of npoints abscissae equidistant from each other by distance step
  87. x = a
  88. step = (b-a)/float(npoints-1)
  89. for i in range(npoints):
  90. y = Horner(listx,diff,x)
  91. longfile.write("{:.20f} \t {:.20f} \n".format(x,y))
  92. x = x+step
  93. longfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement