Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # Solutions to a system of polynomial coefficients Xa = f
- # the x-coordinates of the points we know
- x = np.array([0, 1, 2, 3, 4, 5, 6, 6.7, 7.5])
- # plugs x into f(x) = Σa_i*x^i and represents it as a vector for each value of x, creating a matrix
- # when multiplied by the appropriate coefficient vector a, we get a vector of f(x)
- to_poly = lambda x : np.array([x ** (i) for i in range(len(x))]).T
- X = to_poly(x)
- # the f(x) values corresponding to the x-coordinates
- f = np.array([0, 1.1, 1.5, 1.6, 1.55, 1.4, 1, 0.5, 0])
- # solves the system
- a = (np.linalg.solve(X, f))
- # formatting mumbo-jumbo; the '10' specifies 10 decimal places
- print('[%s]' % ', '.join(map(str, ['%.10f' % r for r in a])))
- # creates the polynomial in Python (numpy)
- p = np.poly1d(a[::-1])
- # check if (x, f(x)) is correct
- for r in x:
- print('p(', r, ')', '=', p(r), end= ', ', sep='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement