# Template:Tutorial:TestPage2

For a function $y = x^3 - 2 x^2 -x + 4 \,$, it is known the values for 3 points:
• $x_1 = 0, ~ x_2=1, ~ x_3= 2 \,$
• $y_1 = 4, ~ y_2=2, ~ y_3= 2 \,$
For a three nodes quadratic element, the interpolation of such function is:
$x = \sum_{i=1}^3 N_i^{(e)}(\xi) x_i = \frac{1}{2} \xi (\xi -1) x_1 + (1- \xi^2) x_2 + \frac{1}{2} \xi (1+\xi) x_3$

$y = \sum_{i=1}^3 N_i^{(e)}(\xi) y_i = \frac{1}{2} \xi (\xi -1) y_1 + (1- \xi^2) y_2 + \frac{1}{2} \xi (1+\xi) y_3$
For this quadratic approach, the resulting expression is:

$x = (1- \xi^2) + \xi (1+\xi) = 1 + \xi \,$
$y = 2 \xi (\xi -1) + 2(1- \xi^2) + \xi (1+\xi) = \xi (\xi - 1) + 2 \,$

and then:
$y = f(x) = (x - 1)(x - 2) + 2 = x^2 - 3 x + 4 \,$

The following picture shows the error, much bigger outside of the interval.

If the set of points is:
• $x_1 = 0, ~ x_2=\frac{2}{3}, ~ x_3= \frac{4}{3}, ~ x_4= 2\,$
• $y_1 = 4, ~ y_2=\frac{74}{27}, ~ y_3= \frac{40}{27}, y_4= 2 \,$
that means:
• $\xi_1 = -1, ~ \xi_2=-\frac{1}{3}, ~ \xi_3= \frac{1}{3}, \xi_4= 1 \,$

For the four nodes cubic element, the interpolation of such function is:
$x = \sum_{i=1}^4 N_i^{(e)}(\xi)x_i$
$y = \sum_{i=1}^4 N_i^{(e)}(\xi)y_i$

Remember the expression to obtain each $N_i^{(e)}(\xi)$:
$N_i^{(e)}(\xi) = \prod_{j=1 (j \ne i)}^n \frac{\xi - \xi_j}{\xi_i - \xi_j}$

• $N_1^{(e)}(\xi) = \frac{ (\xi+\frac{1}{3}) (\xi-\frac{1}{3}) (\xi-1) }{ (-1 +\frac{1}{3}) (-1-\frac{1}{3}) (-1-1) } = - \frac{9}{16} (\xi+\frac{1}{3}) (\xi-\frac{1}{3}) (\xi-1)$
• $N_2^{(e)}(\xi) = \frac{ (\xi+1) (\xi-\frac{1}{3}) (\xi-1) }{ (-\frac{1}{3} + 1) (-\frac{1}{3}-\frac{1}{3}) (-\frac{1}{3}-1) } = \frac{27}{16} (\xi+1) (\xi-\frac{1}{3}) (\xi-1)$
• $N_3^{(e)}(\xi) = \frac{ (\xi + 1) (\xi + \frac{1}{3}) (\xi-1) }{ (\frac{1}{3} + 1) (\frac{1}{3} + \frac{1}{3}) (\frac{1}{3}-1) } = - \frac{27}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi-1)$
• $N_4^{(e)}(\xi) = \frac{ (\xi + 1) (\xi + \frac{1}{3}) (\xi - \frac{1}{3}) }{ (1 + 1) (1 + \frac{1}{3}) (1 - \frac{1}{3}) } = \frac{9}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi - \frac{1}{3})$

therefore:
$x = \frac{2}{3} \frac{27}{16} (\xi+1) (\xi-\frac{1}{3}) (\xi-1) - \frac{4}{3} \frac{27}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi-1) + 2 \frac{9}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi - \frac{1}{3}) = (\xi + 1)$

\begin{align} y & = - 4 \frac{9}{16} (\xi+\frac{1}{3}) (\xi-\frac{1}{3}) (\xi-1) + \frac{74}{27} \frac{27}{16} (\xi+1) (\xi-\frac{1}{3}) (\xi-1) \\ & - \frac{40}{27} \frac{27}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi-1) + 2 \frac{9}{16} (\xi + 1) (\xi + \frac{1}{3}) (\xi - \frac{1}{3}) \\ & = \xi^3 + \xi^2 - 2 \xi + 2 \\ & = (\xi + 1)^3 - 2 (\xi + 1)^2 - (\xi + 1) + 4 \end{align}

The resulting expression is exactly the same to the original one:
$y(x) = x^3 - 2 x^2 -x + 4 \,$