# fitting curve

## 2285 days ago by bigdata2016

k=4 P=[[-1,1],[1,3],[2,4],[5,20],[6,-10],[8,-2],[10,5]] n=len(P) x=matrix(RDF,n,1) y=matrix(RDF,n,1) PP=matrix(RDF,n,2) for i in range(n): x[i]=P[i][0] y[i]=P[i][1] PP[i,0]=P[i][0] PP[i,1]=P[i][1] html('Given %s samples are $%s$ .'%(n,latex(PP))) if k > n-2 : html('Please, choose polynomial order to be considerably less than number of samples to avoid overfitting!') k=n-2 html('using polynomial of order %s for fitting<br><br>'%(k)) ################################################## fitting exp='a_0' for i in range(1,k+1): exp = exp + ' + a_{' + str(i) + '}x^{' + str(i) + '}' html('$y = %s$<br><br>'%(exp)) for j in range(n): exp='a_0' for i in range(1,k+1): exp = exp + ' + ' + str(x[j][0]^i) + 'a_{' + str(i) + '}' html('$%s = %s$<br>'%(exp,str(y[j][0]) )) html('<br>') A=matrix(RDF,n,k+1) for j in range(n): for i in range(k+1): A[j,i]=x[j][0]^i html('$Ax=b$<br><br>$A = %s$, $b=%s$<br><br>$A^{T}A = %s$<br><br>'%(latex(A),latex(y),latex(A.transpose()*A))) alpha =(A.transpose()*A).inverse() * A.transpose() * y html('$x = %s$<br><br>'%(latex(alpha))) for i in range(k+1): html('\$a_{' + str(i) + '} = ' + str(alpha[i][0])) ################################################## plotting def polynomial(x): ret=0 for i in range(k+1): ret = ret + alpha[i][0]*x^i return ret plotstart=min(x)[0]-5 plotend=max(x)[0]+5 curve=plot(polynomial,(plotstart, plotend)) show(list_plot(P,color='red') + curve)
 Given 7 samples are $\left(\begin{array}{rr} -1.0 & 1.0 \\ 1.0 & 3.0 \\ 2.0 & 4.0 \\ 5.0 & 20.0 \\ 6.0 & -10.0 \\ 8.0 & -2.0 \\ 10.0 & 5.0 \end{array}\right)$ . using polynomial of order 4 for fitting $y = a_0 + a_{1}x^{1} + a_{2}x^{2} + a_{3}x^{3} + a_{4}x^{4}$ $a_0 + -1.0a_{1} + 1.0a_{2} + -1.0a_{3} + 1.0a_{4} = 1.0$ $a_0 + 1.0a_{1} + 1.0a_{2} + 1.0a_{3} + 1.0a_{4} = 3.0$ $a_0 + 2.0a_{1} + 4.0a_{2} + 8.0a_{3} + 16.0a_{4} = 4.0$ $a_0 + 5.0a_{1} + 25.0a_{2} + 125.0a_{3} + 625.0a_{4} = 20.0$ $a_0 + 6.0a_{1} + 36.0a_{2} + 216.0a_{3} + 1296.0a_{4} = -10.0$ $a_0 + 8.0a_{1} + 64.0a_{2} + 512.0a_{3} + 4096.0a_{4} = -2.0$ $a_0 + 10.0a_{1} + 100.0a_{2} + 1000.0a_{3} + 10000.0a_{4} = 5.0$ $Ax=b$$A = \left(\begin{array}{rrrrr} 1.0 & -1.0 & 1.0 & -1.0 & 1.0 \\ 1.0 & 1.0 & 1.0 & 1.0 & 1.0 \\ 1.0 & 2.0 & 4.0 & 8.0 & 16.0 \\ 1.0 & 5.0 & 25.0 & 125.0 & 625.0 \\ 1.0 & 6.0 & 36.0 & 216.0 & 1296.0 \\ 1.0 & 8.0 & 64.0 & 512.0 & 4096.0 \\ 1.0 & 10.0 & 100.0 & 1000.0 & 10000.0 \end{array}\right)$, $b=\left(\begin{array}{r} 1.0 \\ 3.0 \\ 4.0 \\ 20.0 \\ -10.0 \\ -2.0 \\ 5.0 \end{array}\right)$$A^{T}A = \left(\begin{array}{rrrrr} 7.0 & 31.0 & 231.0 & 1861.0 & 16035.0 \\ 31.0 & 231.0 & 1861.0 & 16035.0 & 143701.0 \\ 231.0 & 1861.0 & 16035.0 & 143701.0 & 1324491.0 \\ 1861.0 & 16035.0 & 143701.0 & 1324491.0 & 12455341.0 \\ 16035.0 & 143701.0 & 1324491.0 & 12455341.0 & 118847715.0 \end{array}\right)$ $x = \left(\begin{array}{r} -0.928678993361 \\ 1.33736741458 \\ 2.4182780227 \\ -0.676378817549 \\ 0.0427914636306 \end{array}\right)$ $a_{0} = -0.928678993361$ $a_{1} = 1.33736741458$ $a_{2} = 2.4182780227$ $a_{3} = -0.676378817549$ $a_{4} = 0.0427914636306$  Given 7 samples are . using polynomial of order 4 for fitting ,