Writing a function $f$ when $x$ and $f(x)$ are known

You can generalise the problem: suppose you know the value of $f(x)$ for a particular finite set of values of $x$. (Here, you know the value of $f(x)$ when $x=0,1,2,3,4,5,6$.) Then you can find a possible polynomial function $f$ which takes the given values using the following method.

Suppose you know the value of $f(x)$ when $x=x_1, x_2, \dots, x_n$, and that $f(x_j) = a_j$ for $1 \le j \le n$.

Let

$$P_i(x) = \lambda (x-x_1)(x-x_2) \cdots (x-x_{i-1})(x-x_{i+1}) \cdots (x-x_n)$$

Where $\lambda$ is some constant. That is, it's $\lambda$ times the product of all the $(x-x_k)$ terms with $x-x_i$ left out. Then $P_i(x_k) = 0$ whenever $k \ne i$.

We'd like $P_i(x_i) = 1$: then if we let

$$f(x) = a_1 P_1(x) + a_2 P_2(x) + \cdots + a_n P_n(x)$$

then setting $x=x_j$ sends all the $P_i(x)$ terms to zero except $P_j(x)$, leaving you with $f(x_j) = a_jP_j(x_j) = a_j$, which is exactly what we wanted.

Well we can set $\lambda$ to be equal to $1$ divided by what we get by setting $x=x_j$ in the product: this is never zero, so we can definitely divide by it. So we get

$$P_i(x) = \dfrac{(x-x_1)(x-x_2) \dots (x-x_{i-1})(x-x_{i+1}) \dots (x-x_n)}{(x_i-x_1)(x_i-x_2) \dots (x_i-x_{i-1})(x_i-x_{i+1}) \dots (x_i-x_n)}$$

Then $P_i(x_k) = 0$ if $k \ne i$ and $1$ if $k=i$, which is just dandy.

More concisely, if $f$ is to satisfy $f(x_j)=a_j$ for $1 \le j \le n$ then

$$f(x) = \sum_{j=1}^n \left[ a_j \prod_{i=1}_{i \ne j}^n \frac{x-x_i}{x_j-x_i} \right]$$

This method is called Lagrange interpolation.


So in this case, your $x_1, x_2, \dots, x_7$ are the numbers $0, 1, \dots, 6$ and your $a_1, a_2, \dots, a_7$ are, respectively, $2,0,0,0,0,0,1$. Substituting these into the above formula, we get:

$$\begin{align} f(x) &= 2 \times \dfrac{(x-1)(x-2) \dots (x-6)}{(0-1) (0-2) \dots (0-6)} + 0 \times (\text{stuff}) + 1 \times \dfrac{x(x-1) \dots (x-5)}{6(6-1)(6-2) \dots (6-5)}\\ &= 2 \dfrac{(x-1) (x-2) \dots (x-6)}{720} + \dfrac{x(x-1) \dots (x-5)}{720} \\ &= \dfrac{(x-1)(x-2)(x-3)(x-4)(x-5)}{720} \left[ 2(x-6) + x \right] \\ &= \boxed{\dfrac{(x-1)(x-2)(x-3)(x-4)^2(x-5)}{240}} \end{align}$$

You can check easily that this polynomial satisfies the values in your table.


In fact, in this particular case, all the above machinery wasn't necessary. It's plain that $f(x)=0$ when $x=1,2,3,4,5$, and so $x-j$ must divide $f(x)$ for $j=1,2,3,4,5$, and so $$f(x) = (x-1)(x-2)(x-3)(x-4)(x-5)g(x)$$ for some polynomial $g(x)$. Since we're only worried about $x=0,6$ beyond this, i.e. $2$ values of $x$, it suggests we have $2$ free parameters in $g(x)$ and hence $g(x)=ax+b$ is linear. That is, we have $$f(x) = (x-1)(x-2)(x-3)(x-4)(x-5)(ax+b)$$

Substituting $x=0$ and $x=6$, respectively, gives $$\begin{align}2 &= -5! \cdot b \\ 1 &= 5! \cdot (6a+b) \end{align}$$ and solving simultaneously gives $b=-\dfrac{2}{120}$ and $a = \dfrac{3}{720}$, which (after simplification) yields the desired result.


Use $f(x)=(x-1)(x-2)(x-3)(x-4)(x-5)(\frac{x}{240}-\frac{1}{60})$. The first five factors make the function $0$ at $1,2,3,4,5$. The last factor takes care of the rest.


Clearly a function that we are looking for is nonlinear. If we do not want to split it into separate cases and want to use only basic operations, at least one possibility is to find suitable coefficients for the following polynomial: $$f(x)=a_1x^6+a_2x^5+\dots+a_6x+a_7,$$ By solving the system of your 7 given equations: $$\dots \\f(1)=a_1+a_2+\dots+a_6+a_7=0\\ \dots$$ we obtain that $$f(x)=\frac{x^6}{240}-\frac{19x^5}{240}+\frac{29x^4}{48}-\frac{113x^3}{48}+\frac{587x^2}{120}-\frac{76x}{15}+2.$$