#ipython -pylab import numpy as np import pylab n = 10; t = np.linspace(0,1,n) #y = np.array([1.1, 2.1, 3.2, 4.1, 6.4]) y = np.sin(np.pi*t) + 1.0*(np.random.random(n)-0.5) pylab.plot(t,y,'x') n = len(t) t1 = sum(t) t2 = sum(t**2) t3 = sum(t**3) t4 = sum(t**4) A = np.array([[n,t1,t2],[t1,t2,t3],[t2,t3,t4]]) #slide 35, Ch 5. y0 = sum(y) y1 = sum(y*t) y2 = sum(y*t**2) b = np.array([y0,y1,y2]) p = np.linalg.solve(A, b) x = np.linspace(0,1,101); f = p[2]*x**2 + p[1]*x + p[0] pylab.plot(x,f) pylab.show()