# Interpolation over an unequally spaced grid

my aim is to interpolate a function like this: I have

      |<--- h -->|<-------- k ----------->|
o----------o------------------------o
x(i-1)      x(i)                   x(i+1)


with respective values $f(x_i-h), f(x_i), f(x_i+k)$, now I want to approximate $f(x+k/2)$ using centered formula.

I know for example that $f(x+k/2)\approx \frac{f(x_i)+f(x_i+k)}{2}$ but what if I want to use also $f(x-h)?$

thanks!

With three points, you can either do a linear interpolation which is $\mathcal{O}(h)$ or a quadratic interpolation which is $\mathcal{O}(h^2)$.

The linear interpolation is simple. Let $\theta \in [0,1]$ (the percentage along the interval from $x$ to $x+h$). Then,

$$f(x+\theta h) = \theta f(x) + (1-\theta)f(x+h) + \mathcal{O}(h)$$

It's just the weighted average between the two points, putting more weight on the end you're closer to. Notice that this only uses two points. Also notice that if $\theta = \frac{1}{2}$, we recover the midpoint formula you had before.

The quadratic interpolation is exactly how it sounds: you interpolate by making a polynomial. Recall that in the general form we have that a quadratic is

$$f(x) = ax^2 + bx + c$$

Thus we have 3 unknowns and 3 equations: $f(x) = y_1$, $f(x+h) = y_2$, and $f(x+k) = y_3$. Given that $k$, $h$, and the $y_i$ are known, this uniquely defines the quadratic through these points, and thus gives the coefficients $a$, $b$, and $c$. In this interval, we can then define $\theta \in [0,1]$ (the percentage to $k$) and approximate via

$$f(x+\theta k) = a (x + \theta k)^2 + b (x + \theta k) + c + \mathcal{O}(k^2)$$

Notice the order here is 2, so this will be a slightly better approximation than the linear interpolation.

Let's do the math out for completeness. We get the equations:

$$y_1 = a x^2 + bx + c$$ $$y_2 = a x^2 + (b + 2ah)x + (c + bh + a h^2)$$ $$y_3 = a x^2 + (b + 2ak)x + (c + bk + a k^2)$$

The easier way to do this is write everything in terms of the coefficent we wish to find:

$$y_1 = x^2 a + x b + c$$ $$y_2 = (x^2 + 2hx + h^2) a + (x + h) b + c$$ $$y_3 = (x^2 + 2kx + k^2) a + (x + k) b + c$$

So this is a linear system:

$$\left[\begin{array}{c} y_{1}\\ y_{2}\\ y_{3} \end{array}\right]=\left[\begin{array}{ccc} x^{2} & x & 1\\ x^{2}+2hx+h^{2} & x+h & 1\\ x^{2}+2kx+k^{2} & x+k & 1 \end{array}\right]\left[\begin{array}{c} a\\ b\\ c \end{array}\right]$$

The solution is then

$$\left[\begin{array}{c} a\\ b\\ c \end{array}\right]=\left[\begin{array}{ccc} x^{2} & x & 1\\ x^{2}+2hx+h^{2} & x+h & 1\\ x^{2}+2kx+k^{2} & x+k & 1 \end{array}\right]^{-1}\left[\begin{array}{c} y_{1}\\ y_{2}\\ y_{3} \end{array}\right]$$

I threw this into Mathematica to get:

$$\left[\begin{array}{c} a\\ b\\ c \end{array}\right]= \left( \begin{array}{c} \frac{y_1 (h-k)-h y_3+k y_2}{h k (h-k)} \\ \frac{y_1 (-(h-k)) (h+k+2 x)+h y_3 (h+2 x)-k y_2 (k+2 x)}{h k (h-k)} \\ \frac{(k+x) \left(y_1 (h-k) (h+x)+k x y_2\right)-h x y_3 (h+x)}{h k (h-k)} \\ \end{array} \right)$$

as the explicit equations for the coefficients.