For a general polynomial interpolating function f of degree D, you have
[tex]f(x) = \sum_{k=0}^D a_k x^k[/tex]
Now, for your N data points [itex](x_i, y_i)[/itex], you want to minimize the squares of the difference between your interpolating function and the true [itex]y_i[/itex] values; i.e., you want to minimize the following:
[tex]S = \sum_{i=1}^N(f(x_i) - y_i)^2[/tex]
Now, f(x) can be considered to be a function of the parameters [itex]a_k[/itex], and thus S can also be considered to be a function of [itex]a_k[/itex]. We want to find the [itex]a_k[/itex] that minimize S. We note that S is positive and quadratic in each of the [itex]a_k[/itex], so we know that we can minimize S by setting all of its first partial derivatives equal to zero:
[tex]{\partial S \over \partial a_n} = 0 \text{ for all n}[/tex]
Expanding the partial derivative:
[tex]{\partial S \over \partial a_n} = {\partial \over \partial a_n} \left[ \sum_{i=1}^N(f(x_i) - y_i)^2 \right] = \sum_{i=1}^N {\partial \over \partial a_n} \left[(f(x_i) - y_i)^2\right] = \sum_{i=1}^N\left[2(f(x_i) - y_i)\left {\partial f \over \partial a_n}\right|_{x_i}\right] = 0[/tex]
We note that for a particular n,
[tex]{\partial f \over \partial a_n} = x^n[/tex]
And so,
[tex]{\partial S \over \partial a_n} = \sum_{i=1}^N\left[2(f(x_i) - y_i)\left {\partial f \over \partial a_n}\right|_{x_i}\right] = \sum_{i=1}^N\left[ 2\left(\sum_{k=0}^D a_k x_i^k - y_i\right)x_i^n \right] = 0[/tex]
If you expand out this sum, you will obtain D+1 linear equations for each of the D+1 [itex]a_k[/itex] (i.e., if D=3 for a 3rd-degree polynomial, then you'll get 4 equations for your 4 unknown polynomial coefficients). Then you just have to solve this system of linear equations.