Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
- ys = [5, 6, 8, 10, 12, 13, 12, 10, 8, 10, 8, 11, 7, 9, 11, 10, 9, 12, 11, 6]
- gx = []
- gy = []
- t = []
- deg = 6
- for k in range(deg):
- t.append(0)
- for i in range(len(xs)):
- t[k] += ys[i] * xs[i] ** k
- t = np.array(t)
- def get_matrix():
- res = np.zeros((deg, deg))
- for k in range(deg):
- for i in range(len(xs)):
- for d in range(deg):
- res[k][d] += xs[i] ** (d + k)
- return res
- a = np.linalg.solve(get_matrix(), t)
- def f(x):
- res = 0
- for i in range(len(a)):
- res += a[i] * x ** i
- return res
- for i in range(len(xs)):
- print(ys[i], f(xs[i]))
- gx = []
- gy = []
- x = xs[0]
- deltaX = 0.01
- while x <= xs[-1]:
- gx.append(x)
- gy.append(f(x))
- x += deltaX
- fig, ax = plt.subplots()
- ax.plot(gx, gy, color='green')
- ax.scatter(np.array(xs), np.array(ys), color='red')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement