Best way to approximate a curve?
So, you have a bunch of datapoints of the form (x,y), and considering all of those datapoints together, you have the vectors $x$ and $y$.

To do a curve fit, you would like to solve the equation $Aw=y$ for the column vector $w$, which holds the coefficients of your curve fit polynomial.  These coefficients are also the weights of the basis vectors in the matrix $A$.

The matrix $A$, for an $n^{th}$ order approximation, will hold $n+1$ columns and have a number of rows equal to the length of $x$ and/or $y$ (the number of datapoints).

The first column of $A$ is the vector $x$ raised element-wise to the power of 0. So, it's just a vector of 1's.  This is the basis associated with the offset or y-intercept.  The second column of $A$ is the vector $x$ raised element-wise to the power of 1. So, it's just the vector $x$.  The third column of $A$ is the vector $x$ raised element-wise to the power of 2.  You see the pattern?

To solve for the weight vector, $w=pinv(A)y$, the pseudoinverse of $A$ times $y$. Note, this doesn't actually solve the equation $Aw=y$, but performs an optimal projection of your data onto the vector space described by $A$.

EDIT:  $pinv(A)=(A^TA)^{-1}A^T$