[Math] The quadratic spline

interpolationquadraticsspline

I'd like to fit the data in table as blow

x      f(x)
3.0     2.5
4.5     1.0
7.0     2.5
9.0     0.5

when $x=5$, I want to find value of $f(x)$ by using Quadratic Spline.

this is my text book example. but there are some confused point which is I can't understand equation.
question

How to set the equation?

  • $20.25a_1 + 4.5b_1 + c_1 = 1.0$
  • $20.25a_2 + 4.5b_2 + c_2 = 1.0$
  • $49a_2 + 7b_2 + c_2 = 2.5$
  • $49a_3 + 7b_3 + c_3 = 2.5$

I meant that my question is that why does equation to be

  • $a_1 + b_1 + c_1$
  • $a_2 + b_2 + c_2$
  • $a_2 + b_2 + c_2$
  • $a_3 + b_3 + c_3$

instead of

  • $a_1 + b_1 + c_1$
  • $a_2 + b_2 + c_2$
  • $a_3 + b_3 + c_3$
  • $a_4 + b_4 + c_4$

Also

  • $9a_1 + 3b_1 + c_1 =2.5$
  • $81a_3+9b_3+c3 = 0.5 $

?

update2

as you can see from the picture, there is one equation 18.33
But I'm not understand it. how to that condition can be represented to 18.33 equation ? would you please give any hint?

update3.

finally, I understand almost but except the matrix.
In the text, they are use the matrix for find to each coefficients.
So My question is how to set this matrix. I can't find quite a bit relationships.

Would you please give me any hint?

Best Answer

Here is a cubic-spline interpolation for the $4$ points given in your question:

$ f(x)= \begin{cases} +0.1 \overline3(x-3.0)^3 -1.3 (x-3.0)+2.5 & \text{$3.0 \leq x \leq 4.5$}\\ -0.15\overline1(x-4.5)^3+0.6 (x-4.5)^2+0.0\overline4(x-4.5)+1.0 & \text{$4.5 \leq x \leq 7.0$}\\ +0.0 \overline8(x-7.0)^3-0.5\overline3(x-7.0)^2-0.2\overline8(x-7.0)+2.5 & \text{$7.0 \leq x \leq 9.0$}\\ \end{cases} $

Here is a piece of C code for any given number of points $(x_0,y_0),(x_1,y_1),\ldots,(x_N,y_N)$:

void Spline(double x[N+1],double y[N+1], // input
            double A[N],double B[N],     // output
            double C[N],double D[N])     // output
{
    double w[N];
    double h[N];
    double ftt[N+1];

    for (int i=0; i<N; i++)
    {
        w[i] = (x[i+1]-x[i]);
        h[i] = (y[i+1]-y[i])/w[i];
    }

    ftt[0] = 0;
    for (int i=0; i<N-1; i++)
        ftt[i+1] = 3*(h[i+1]-h[i])/(w[i+1]+w[i]);
    ftt[N] = 0;

    for (int i=0; i<N; i++)
    {
        A[i] = (ftt[i+1]-ftt[i])/(6*w[i]);
        B[i] = ftt[i]/2;
        C[i] = h[i]-w[i]*(ftt[i+1]+2*ftt[i])/6;
        D[i] = y[i];
    }
}

void PrintSpline(double x[N+1],           // input
                 double A[N],double B[N], // input
                 double C[N],double D[N]) // input
{
    for (int i=0; i<N; i++)
    {
        printf("%f <= x <= %f : f(x) = ",x[i],x[i+1]);
        printf("%f(x-%f)^3 + ",A[i],x[i]);
        printf("%f(x-%f)^2 + ",B[i],x[i]);
        printf("%f(x-%f) + "  ,C[i],x[i]);
        printf("%f\n"         ,D[i]);
    }
}

Here is a piece of Python code for any given number of points $(x_0,y_0),(x_1,y_1),\ldots,(x_N,y_N)$:

class Point:
    def __init__(self,x,y):
        self.x = 1.0*x
        self.y = 1.0*y

def Spline(points):
    N   = len(points)-1
    w   =     [(points[i+1].x-points[i].x)      for i in range(0,N)]
    h   =     [(points[i+1].y-points[i].y)/w[i] for i in range(0,N)]
    ftt = [0]+[3*(h[i+1]-h[i])/(w[i+1]+w[i])    for i in range(0,N-1)]+[0]
    A   =     [(ftt[i+1]-ftt[i])/(6*w[i])       for i in range(0,N)]
    B   =     [ftt[i]/2                         for i in range(0,N)]
    C   =     [h[i]-w[i]*(ftt[i+1]+2*ftt[i])/6  for i in range(0,N)]
    D   =     [points[i].y                      for i in range(0,N)]
    return A,B,C,D

def PrintSpline(points,A,B,C,D):
    for i in range(0,len(points)-1):
        func = str(points[i].x)+' <= x <= '+str(points[i+1].x)+' : f(x) = '
        components = []
        if A[i]:
            components.append(str(A[i])+'(x-'+str(points[i].x)+')^3')
        if B[i]:
            components.append(str(B[i])+'(x-'+str(points[i].x)+')^2')
        if C[i]:
            components.append(str(C[i])+'(x-'+str(points[i].x)+')')
        if D[i]:
            components.append(str(D[i]))
        if components:
            func += components[0]
            for i in range (1,len(components)):
                if components[i][0] == '-':
                    func += ' - '+components[i][1:]
                else:
                    func += ' + '+components[i]
            print func
        else:
            print func+'0'

def Example():
    points = [Point(3.0,2.5),Point(4.5,1.0),Point(7.0,2.5),Point(9.0,0.5)]
    A,B,C,D = Spline(points)
    PrintSpline(points,A,B,C,D)

Please note that the two pieces of code above assume $x_0 < x_1 < \ldots < x_N$.

Related Question