Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import matplotlib.pyplot as plt
- import numpy as np
- import sys
- def read_inputs(filename):
- """
- Given a filename containing abscissaes and ordinates, returns the actual
- values.
- """
- with open(filename) as f:
- x, y = [], []
- for line in f.readlines():
- coord = [float(n) for n in line.strip().split()]
- x.append(coord[0])
- y.append(coord[1])
- return np.array(x), np.array(y)
- def newton_differences(x, y):
- """
- Returns the newton difference of the given abscissaes and ordinates.
- """
- n = len(x)
- a = []
- for i in range(n):
- a.append(y[i])
- for j in range(1, n):
- for i in range(n - 1, j - 1, -1):
- a[i] = float(a[i] - a[i - 1]) / float(x[i] - x[i - j])
- return np.array(a)
- def Horner(abscissaes, newton_diff, x):
- # Reverse iteration, starting from the end makes it easier.
- value = newton_diff[-1]
- for i, abscissae in reversed(list(enumerate(abscissaes[:-1]))):
- term = newton_diff[i]
- value = term + (x - abscissae) * value
- return value
- def write_to_files(newton_diff_filename, graph_points_filename, npoints,
- abscissaes, newton_diff, a, b):
- """
- Writes the newton difference in the first file, and the points of the
- graph in the second file.
- """
- with open(newton_diff_filename, "w") as f:
- f.writelines(["%.20f\t" % item for item in newton_diff])
- # we create a mesh of npoints abscissae equidistant from each other by distance step
- x = a
- step = (b - a) / float(npoints - 1)
- with open(graph_points_filename, "w") as f:
- for i in range(npoints):
- y = Horner(abscissaes, newton_diff, x)
- f.write("{:.20f}\t{:.20f}\n".format(x, y))
- x = x + step
- abscissaes, ordinates = read_inputs("table.txt")
- newton_diff = newton_differences(abscissaes, ordinates)
- write_to_files(
- "divded_differences.txt",
- "function_to_plot.txt",
- 100000, # npoints
- abscissaes,
- newton_diff,
- 0, # a
- 10, # b
- )
- def main():
- try:
- name = sys.argv[1]
- numits = int(sys.argv[2])
- except:
- print('Usage: python %s <filename> <nb points>' % sys.argv[0])
- print('Example: python %s table.txt 1000' % sys.argv[0])
- # we read from the table and ensure that abscissae are at distances larger than 1.e-10 from each other
- x, y = read_from_file(name)
- print("Your abscissae ", x)
- print("Your ordinates ", y)
- print("This is a representation of the points in the Cartesian plane:")
- a = min(x) - 0.005 # you need to modify this based on how
- b = max(x) + 0.005 # wide you want your graph to be
- diff = newton_differences(x, y)
- write_to_files("divided_differences.txt", "function_to_plot.txt", numits, x, diff, a, b )
- x, y = read_from_file("table.txt")
- plt.plot([x], [y], 'ro')
- plt.axes().set_aspect('equal', 'datalim')
- plt.xlabel('abscissae')
- plt.ylabel('ordinates')
- plt.title('Graph of the original points')
- plt.show()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement