Construction
Let us now try to construct the cubic spline interpolant:
> restart:
> S[j]:=xx->a[j]+b[j]*(xx-x[j])+c[j]*(xx-x[j])^2+d[j]*(xx-x[j])^3;
> eq1:=subs(xx=x[j],S[j](xx))=f(x[j]);
> eq2:=a[j+1]=S[j](x[j+1]);
> eq2a:=subs(x[j+1]=h[j]+x[j],eq2);
> D(S[j])(xx);
> subs(xx=x[j],D(S[j])(xx));
> eq4:=b[j+1]=D(S[j])(x[j+1]);
> eq4a:=subs(x[j+1]=h[j]+x[j],eq4);
> (D@@2)(S[j])(xx);
> subs(xx=x[j],(D@@2)(S[j])(xx));
> eq5:=2*c[j+1]=(D@@2)(S[j])(x[j+1]);
> eq5a:=subs(x[j+1]=h[j]+x[j],eq5/2);
> dj:=solve(eq5a,d[j]);
> eq2b:=simplify(subs(d[j]=dj,eq2a));
> eq4b:=simplify(subs(d[j]=dj,eq4a));
> bj:=simplify(solve(eq2b,b[j]));
> bjmin:=simplify(subs(j=j-1,bj));
> eq4c:=subs(j=j-1,eq4b);
> eqf:=simplify(subs(b[j]=bj,b[j-1]=bjmin,eq4c));
> eqf1:=simplify(lhs(eqf)+(1/3)*(-3*a[j+1]+3*a[j])/h[j]=rhs(eqf)+(1/3)*(-3*a[j+1]+3*a[j])/h[j]);
> eqf2:=simplify(lhs(eqf1)-(1/3)*(c[j-1]*h[j-1]^2+2*h[j-1]^2*c[j])/h[j-1]=rhs(eqf1)-(1/3)*(c[j-1]*h[j-1]^2+2*h[j-1]^2*c[j])/h[j-1]);
All
's are known so we should be able to solve this system for the coefficients
. Let us consider this system in a practical case:
> for i from 1 to 4 do eqq[i]:=subs(j=i,eqf2): od;
add the two boundary conditions:
> eqq[0]:=c[0]=0;eqq[5]:=c[5]=0;
Which consitutes a system of linear equations, with coefficient matrix
> with(linalg):AA:=genmatrix([eqq[0],eqq[1],eqq[2],eqq[3],eqq[4],eqq[5]],[c[0],c[1],c[2],c[3],c[4],c[5]]);
One can show that a matrix of this form will lead to a unique solution of the system, so that a unique spline exists.
>
>