In [1]:
    
from matplotlib import pylab as plt
import numpy as np
import scipy as sp
    
In [2]:
    
def f(x):
    return np.sin(x / 5.0) * np.exp(x / 10.0) + 5.0 * np.exp(-x / 2.0)
def interpolate(fn, x):
    A = np.ndarray(shape=(x.size, x.size), dtype=float)
    for row in range(0, x.size):
        for col in range(0, x.size):
            A[row, col] = x[row] ** col
    return np.linalg.solve(A, map(fn, x))
def approximated_fn(w, x):
    y = np.zeros(shape=(x.size, 1), dtype=float)
    for row in range(0, y.size):
        for col in range(0, w.size):
            y[row] += w[col] * x[row]**col
    return y
    
In [3]:
    
x2 = np.array([1.0, 15.0])
w2 = interpolate(f, x2)
x3 = np.array([1.0, 8.0, 15.0])
w3 = interpolate(f, x3)
x4 = np.array([1.0, 4.0, 10.0, 15.0])
w4 = interpolate(f, x4)
    
In [4]:
    
with open("submission-2.txt", 'w') as file_obj:
    res = map(str, w4)
    file_obj.write(res[0] + " " + res[1] + " " + res[2] + " " + res[3])
    
In [5]:
    
test_x = plt.frange(1.0, 15.0, 0.1)
test_y = map(f, test_x)
    
In [6]:
    
y2 = approximated_fn(w2, test_x)
plt.plot(test_x, test_y)
plt.plot(test_x, y2)
plt.show()
    
    
In [7]:
    
y3 = approximated_fn(w3, test_x)
plt.plot(test_x, test_y)
plt.plot(test_x, y3)
plt.show()
    
    
In [8]:
    
y4 = approximated_fn(w4, test_x)
plt.plot(test_x, test_y)
plt.plot(test_x, y4)
plt.show()