• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jan 27th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2.
4.     with open(filename) as f:
5.         x, y = [], []
7.             coord = [float(n) for n in line.strip().split()]
8.             x.append(coord)
9.             y.append(coord)
10.     return x, y
11.
12.
14.     f=open("table.txt","r")
16.     x=[]
17.     y=[]
18.
19.     for q in lines:
20.         temp = q.strip().split()
21.         x.append(float(temp))
22.         y.append(float(temp))
23.
24.     x = np.array(x)
25.     y = np.array(y)
26.     return x,y
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
57.
58.     term, newton_diff = newton_diff, newton_diff[1:]
59.     abscissae, abscissaes = abscissaes, 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.
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()
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.
Top