Polynomial regression interpolation? 
Possible Duplicate:
Writing a function $f$ when $x$ and $f(x)$ are known 

I'm not versed in mathematics, so you'll have to speak slowly...
If I want to fit a curve to the points,
X  Y
1  0.5
2  5.0
3  0.5
4  2.5
5  5.0
6  0.5

Where would I begin? For my purposes, this needs to be a sixth-order fit...
 A: First of all, since you have 6 points, you can only get a 5th order polynomial to fit.
You need two points to define a line, 3 to define a quadratic, 4 for a cubic... n+1 for an nth-order.
The naive solution is simply to set up:
Y = A5 x^5 + A4 x^4 + A3 x^3 + A2 x^2 + A1 x + A0
For each data pair, plug in x, and generate an equation for the coefficients.
You'll now have six equations in six unknowns, which is solvable, but computationally annoying if you're doing it by hand.
Example: for the second point, x=2, y=5
5 = A5 (2)^5 + A4 (2)^4 + A3 (2)^3 + A2 (2)^2 + A1 (2) + A0
and you end  up with:
5 = 32 A5 + 16 A4 + 8 A3 + 4 A2 + 2 A1 + A0
Repeat for each data point, and then solve the system of equations for A0, A1, ..., A5
A: Here is a step by step way of finding a polynomial $f(x)$ such that for $1 \leq i \leq 6$, $f(i)$ has the values you stated. We will "build up" $f(x)$ slowly, calling the intermediate stages $f^{(1)}(x), f^{(2)}(x), \ldots $ and so on.


*

*Set $y = f^{(1)}(x) = 0.5$ where the right side doesn't really depend on $x$ and the $0.5$ was chosen from the given data: when we set $x=1$ in $f^{(1)}(x)$, this (zero-degree) polynomial evaluates to $0.5$, and thus fits the point $(1,0.5)$ that was given to you.

*If we set $x=2$ in $f^{(1)}(x)$, we will get $0.5$ but we really need
to get $5.0$. So, let us test $$y = f^{(2)}(x) = f^{(1)}(x) + c(x-1)
= 0.5 + c(x-1)$$
where $c$ is just some number (for now) to see what happens. If we set $x=1$
in $f^{(2)}(x)$, we get $0.5$ as before (yay!) but
if we set $x=2$, we get $0.5 + c$ and so by choosing $c = 4.5$, we can get
$f^{(2)}(x)$ to give the desired values for both $x = 1$ and $x = 2$.

$f^{(2)}(x) = 0.5 + 4.5(x-1)$  is a polynomial such that $f^{(2)}(1) = 0.5$ and
  $f^{(2)}(2) = 5.0$.\
  Notice that $4.5 = 5.0 - f^{(1)}(2)$ which I will write for future reference
  as $\displaystyle\frac{5.0 - f^{(1)}(2)}{2-1}$.


*$f^{(2)}(3) = 0.5 + 4.5(3-1) = 9.5$ which is not what we want. So,
let us try
$$y = f^{(3)}(x) = f^{(2)}(x) + c(x-1)(x-2)$$
where, as before, we will choose the value of $c$ in just a bit.  Note
that if we set $x$ to either $1$ or $2$ in $f^{(3)}(x)$, that last term
evaluates to $0$ and so $f^{(3)}(1) = f^{(2)}(1) = 0.5$, 
$f^{(3)}(2) = f^{(2)}(2) = 5.0$ and so we are OK there. But, 
$$f^{(3)}(3) = f^{(2)}(3) + c(2)(1) = 9.5 + 2c$$
and so choosing $c = -4.5$ will make $f^{(3)}(3)$ evaluate to 
the desired $0.5$.

$f^{(3)}(x) = 0.5 + 4.5(x-1) - 4.5(x-1)(x-2)$ is a polynomial such that
  $f^{(3)}(1) = 0.5$, $f^{(3)}(2) = 5.0$, and $f^{(3)}(3) = 0.5$.
  Note that
  $-4.5 = \displaystyle\frac{0.5 - f^{(2)}(3)}{(3-1)(3-2)}$.

Since I am getting tired of typing, let me briefly explain how the
rest of the thing will work.  We set 
$$f^{(4)}(x) = f^{(3)}(x) 
+ \frac{2.5 - f^{(3)}(4)}{(4-1)(4-2)(4-3)}(x-1)(x-2)(x-3)$$
where the second term vanishes when we set $x = 1, 2, 3$ and so
we get the desired values of $y$, and when we set $x = 4$,
we get
$$f^{(4)}(4) = f^{(3)}(4) + \frac{2.5 - f^{(3)}(4)}{(4-1)(4-2)(4-3)}(4-1)(4-2)(4-3) = 2.5$$
which is exactly what we want. Lather, rinse, and repeat for
$f^{(5)}(x)$ and $f^{(6)}(x)$.
If you feel inclined to learn more about this idea, look for
Newtonian interpolation on the Internet.
