import numpy as np
import matplotlib.pyplot as plt
def rbf_tut1_q3(xx, kk, hh):
"""Evaluate RBF kk with bandwidth hh on points xx (shape N,)"""
center = ((kk - 51) * hh) / np.sqrt(2)
phi = np.exp((-(xx - center) ** 2) / hh ** 2)
return phi
N = 10
N_real= 1000
K = 101
kks = np.arange(1, K+1, 1)
hh = 0.2
xx = np.arange(-1, 1, 2 / N)
yy = xx**7+xx**5+xx*3+xx
xxx = np.arange(-1, 1, 2 / N_real)
yyy = xxx**7+xxx**5+xxx*3+xxx
for kk in kks:
inputs = rbf_tut1_q3(xx, kk, hh)
X = []
for i in range(K):
X.append(rbf_tut1_q3(xx, kks[i], hh))
X = np.array(X)
X = X.T
print(X.shape)
W = np.linalg.lstsq(X, yy, rcond=None)[0]
print(np.matmul(X, W).shape)
plt.plot(xxx, yyy, c = 'r')
plt.plot(xx, np.matmul(X, W), c='y')
plt.scatter(xx, yy, marker = 'o')
plt.show()
