-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpolationGeneral.py
More file actions
47 lines (34 loc) · 895 Bytes
/
InterpolationGeneral.py
File metadata and controls
47 lines (34 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import matplotlib.pyplot as plt
import numpy as np
xRaw = [1, 5, 10, -1, 7, 8]
yRaw = [2, 6, 4, 1, 3, 9]
POLYNOMIAL = 5
def f(x, X):
#Function to graph that fits the data
sum = 0;
for n in range(0, POLYNOMIAL + 1):
sum += X[n, 0] * (x ** n)
return sum
#System equations
#y(n) = a + b * x(n) + c * x(n)^2 + d * x(n)^3
#y(n) = a + b * x(n) + c * x(n)^2 + d * x(n)^3
#y(n) = a + b * x(n) + c * x(n)^2 + d * x(n)^3
#y(n) = a + b * x(n) + c * x(n)^2 + d * x(n)^3
# XA = B
A = np.zeros((POLYNOMIAL + 1, POLYNOMIAL + 1))
for row in range(0, POLYNOMIAL + 1):
for n in range(0, POLYNOMIAL + 1):
A[row, n] = (xRaw[row] ** n)
B = np.matrix(yRaw)
# X = B * A^-1
X = np.linalg.inv(A)
# print X, "\n\n"
# print B, "\n\n"
X = np.dot(X, np.transpose(B))
print X, "\n\n"
plt.figure(1)
plt.scatter(xRaw, yRaw)
plt.ylabel('Data')
x = np.arange(-2.0, 12.0, 0.1)
plt.plot(x, f(x, X))
plt.show()