Constructing cubic spline interpolation polynomials

In summary, cubic spline interpolation involves constructing piecewise polynomial functions, specifically third-degree polynomials, to approximate a given set of data points. The process ensures that the spline is continuous and has continuous first and second derivatives at the data points, known as knots. Each segment of the spline is defined by a cubic polynomial, and the coefficients are determined by solving a system of equations that incorporates the conditions of continuity and smoothness. This method provides a smooth and accurate interpolation that minimizes oscillations, making it a preferred choice for numerical analysis and computer graphics applications.
  • #1
bremenfallturm
57
11
Homework Statement
Given the following points:
(-5,1.561), (-4,2) and (-3, 2.56).
Construct cubic spline interpolation polynomials between them.
Relevant Equations
General equations for a third-degree / cubic polynomial:
##y=ax^3+bx^2+cx+d##
##\frac{dy}{dx}=3ax^2+2bx+c##
##\frac{d^2y}{dx^2}=6ax+2b##
My attempt at a solution: We have three points, hence we will have two polynomials ##p_1(x)## for ##x\in [-5,-4]## and ##p_2(x)## for ##x\in [-4,-3]##. Define: ##p_1(x)=c_1x^3+c_2x^2+c_3x+c_4## and ##p_2(x)=c_5x^3+c_6x^2+c_7x+c_8## where the ##c_n## stuff are coefficients for the polynomial.

I came up with the following equations:
  1. For ##p_1##, it must pass through the first two points:
    1. ##1,5610=c_1(-5)^3+c_2(-5)^2+c_3(-5)+c_4##
    2. ##2=c_1(-4)^3+c_2(-4)^2+c_3(-4)+c_4##
  2. For ##p_2##, it must pass through the middle point and the last point:
    1. ##2=c_5(-4)^3+c_6(-4)^2+c_7(-4)+c_8##
    2. ##2,56=c_1(-3)^3+c_2(-3)^2+c_3(-3)+c_8##
  3. 1st and 2nd order derivatives of the polynomials should be equal at the middle point, i.e. ##(-4,2)##
    1. ##3\cdot (-4^2)c_1+2\dot(-4)c_2+c_3-3\cdot (-4^2)c_5-2\cdot (-4)c_6-c_7=0##
    2. ##6c_1\cdot (-4)+2c_2-6c_5\cdot (-4)-2c_6=0##
  4. Finally I make the decision that the 2nd derivatives should be equal to 0 at the endpoints.
    1. ##6c_1\cdot (-5)+2c_2=0##
    2. ##6c_5\cdot (-3)+2_6 =0##
I turned all equations into matrix form and plugged it into an online matrix calculator. The matrix form is this:
1721069081633.png

The matrix calculator gave me the following solutions:
1721069140654.png

If I try to plot it, it looks *almost* right but it's certainly not what I expected.
I assume it's a calculation problem, maybe it's even trival but I can not find it.
The plots look like below by the way, and you can find them using this link.
1721069233425.png

Many thanks for help!
 
Physics news on Phys.org
  • #2
bremenfallturm said:
Many thanks for help!
Check your arithmetic.
Here's the 8x8 matrix I get in Mathematica using your method of solution:
1721080804473.png

Using this to solve your system results in the cubic-spline plot:
1721081424709.png

This looks correct to me.
 
  • Like
Likes bremenfallturm and FactChecker
  • #3
Why not start with the acutal expression for a cubic bezier curve with control points [itex]\mathbf{x}_i[/itex], [tex]\mathbf{x}(t) = (1-t)^3\mathbf{x}_0 + 3(1-t)^2 \mathbf{x}_1 + 3(1-t)t\mathbf{x_2} + t^3\mathbf{x}_3[/tex] which has the properties that [itex]\mathbf{x}(0) = \mathbf{x}_0[/itex], [itex]\mathbf{x}(1) = \mathbf{x}_3[/itex], [itex]\mathbf{x}'(0) = 3(\mathbf{x}_1 - \mathbf{x}_0)[/itex] and [itex]\mathbf{x}'(1) = 3(\mathbf{x}_3 - \mathbf{x}_2).[/itex] Fitting two of these to the curve in question gives you eight unknowns; fixing the end points immediately determines four of them. Continuity of the derivative gives you a fifth equation. For the other three, we have only three points so we should fit them with a quadratic. The conditions therefore are that the coefficients of [itex]t^3[/itex] each vanish and the coefficients of [itex]t^2[/itex] are equal.
 
  • #4
renormalize said:
Check your arithmetic.
Here's the 8x8 matrix I get in Mathematica using your method of solution:
View attachment 348354
Using this to solve your system results in the cubic-spline plot:
View attachment 348355
This looks correct to me.
Haha what I silly mistake I made, I forgot a parenthesis when feeding the equations into the matrix calculator. All worked out now, thank you!
 
  • Like
Likes renormalize
  • #5
pasmith said:
Why not start with the acutal expression for a cubic bezier curve with control points [itex]\mathbf{x}_i[/itex], [tex]\mathbf{x}(t) = (1-t)^3\mathbf{x}_0 + 3(1-t)^2 \mathbf{x}_1 + 3(1-t)t\mathbf{x_2} + t^3\mathbf{x}_3[/tex] which has the properties that [itex]\mathbf{x}(0) = \mathbf{x}_0[/itex], [itex]\mathbf{x}(1) = \mathbf{x}_3[/itex], [itex]\mathbf{x}'(0) = 3(\mathbf{x}_1 - \mathbf{x}_0)[/itex] and [itex]\mathbf{x}'(1) = 3(\mathbf{x}_3 - \mathbf{x}_2).[/itex] Fitting two of these to the curve in question gives you eight unknowns; fixing the end points immediately determines four of them. Continuity of the derivative gives you a fifth equation. For the other three, we have only three points so we should fit them with a quadratic. The conditions therefore are that the coefficients of [itex]t^3[/itex] each vanish and the coefficients of [itex]t^2[/itex] are equal.
The reason I didn't start with that is because I didn't learn about it yet 😁 I am currently looking into more math regarding curve fitting so hopefully I will get there soon:)
 

FAQ: Constructing cubic spline interpolation polynomials

What is cubic spline interpolation?

Cubic spline interpolation is a mathematical method used to construct a smooth curve that passes through a set of given data points. It uses piecewise cubic polynomials to ensure that both the function and its first and second derivatives are continuous at the data points, providing a smooth transition between segments.

How do you determine the coefficients of cubic spline polynomials?

The coefficients of the cubic spline polynomials are determined by setting up a system of equations based on the conditions of continuity and smoothness at each data point. This involves solving for the coefficients such that the first and second derivatives of the polynomials match at each interior point, along with specifying boundary conditions, which can be either natural, clamped, or not-a-knot conditions.

What are the boundary conditions in cubic spline interpolation?

Boundary conditions are constraints that are applied to the cubic spline at the endpoints of the data range. The most common types are natural boundary conditions, which set the second derivative to zero at the endpoints, clamped conditions, which specify the first derivative values at the endpoints, and not-a-knot conditions, which impose continuity conditions on the first derivative at the second and second-to-last points.

What are the advantages of using cubic spline interpolation over other interpolation methods?

Cubic spline interpolation offers several advantages, including greater smoothness and a more accurate representation of the data compared to linear interpolation. It avoids the oscillations that can occur with higher-degree polynomial interpolations (Runge's phenomenon) and provides a piecewise-defined function that can adapt well to the local behavior of the data.

Can cubic spline interpolation be used for extrapolation?

While cubic spline interpolation is primarily designed for interpolation within the range of the given data points, it can be used for extrapolation. However, this practice is generally discouraged because the behavior of the spline outside the data range may not accurately represent the underlying function, leading to unreliable results.

Similar threads

Replies
1
Views
861
Replies
11
Views
2K
Replies
4
Views
1K
Replies
5
Views
1K
Replies
6
Views
1K
Replies
4
Views
2K
Back
Top