Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cubic Spline what is the logic in constructing it?

  1. Jan 21, 2012 #1
    Cubic Spline....what is the logic in constructing it?

    Hey guys,
    I am not trying to directly code a cubic spline computation, but I am writing a sub routine in VBA that takes input data, and outputs it to a text file in Maple syntax.

    Anywho, my cubic spline actually produces LINEAR splines!!! I can't figure out what is wrong with my logic?!

    Here is my logic:
    Suppose I have n data points

    Then I will have n-1 splines, defined for interval (x_i, x_(i+1)) where i = 1 to (n-1)
    I am modeling the spline according to the equation ax^3 + bx^2 + cx + d,
    and therefore IN TOTAL I will have 4*(n-1) different coefficients. The reasoning behind that is simple. The first spline has 4 coefficients, the second has 4, etc, and there are (n-1) splines in total.

    I am also setting the first and second derivatives equal to zero (and hence equal to each other) at (x_i) and (x_(i+1))

    So, here is what I am constraining each spline to:
    a(x_i)^3 + b(x_i)^2 + c(x_i) + d = y(x_i)
    a(x_(i+1))^3 + b(x_(i+1))^2 + c(x_(i+1)) + d = y(x_(i+1))
    3a(x_i)^2 + 2b(x_i) + c = 3a(x_(i+1))^2 + 2b(x_(i+1)) + c
    6a(x_i) + 2b = 6a(x_(i+1)) + 2b

    So for example, suppose My points are: (-1,.5), (0,0) , (3,3)
    I get the following function:

    S(x) = (-.5x, -1<x<0
    = ( x, 0<x<3

    Where is my logic flawed? I would really appreciate any help.
    Last edited: Jan 21, 2012
  2. jcsd
  3. Jan 21, 2012 #2
    Re: Cubic Spline....what is the logic in constructing it?

    EDIT: In my code I was forcing the derivatives to be equal to zero at the end points of S(x), i.e x_0 and x_n. I loosened that constraint up by just making them equal each other, and now I produce pretty nice solutions.

    EDIT 2: Consider this solved. I set the 2nd derivatives of the end points to 0, and removed the constraint that the first derivatives be 0 or equal to each other at the end points. Not sure why I thought that was right.

    I fixed my logic up...and now I'm getting empty solutions.
    Here are my new constraints, which I figured would be correct

    At the end points of S(x), my piecewise cubic spline function, S'(x) and S''(x) = 0.
    I.e Once again, for n data points:
    First Derivative at x_0: 3a(x_0)^2+2b(x_0)+c(x_0)+d = 0
    First Derivative at x_n: 3a(x_n)^2+2b(x_n)+c(x_n)+d = 0
    Second Derivative at x_0: 6a(x_0) + 2b = 0
    Second Derivative at x_n: 6e(x_n) + 2f = 0

    At each node of S(x), i.e, at the endpoint of each intersecting spline, the derivative and second derivative of each spline is equal.
    The following shows what I mean:
    If we have any node, (x_i,y_i),where 0<i<n(strictly) and two splines, az^3+bz^2+cz+d, ez^3+fz^2+gz+h, then we have:
    First Derivative at x-value x_i: 3a(x_i)^2+2b(x_i)+c(x_i)+d = 3e(x_i)^2+2f(x_i)+g(x_i)+h
    Second Derivative at x_i: 6a(x_i) + 2b = 6e(x_i) + 2f

    The final constraint is that the Spline be continuous, and therefore for all x_i, 0<=i<=n,
    S(x_i) = a(x_i)^3 + b(x_i)^2 + c(x_i) + d and S(x_(i+1)) = a(x_(i+1))^3 + b(x_(i+1))^2 + c(x_(i+1)) + d must hold to ensure both points in each sub interval are contained in the spline,
    additionally S(x_(i+1)) = e(x_(i+1))^3 + f(x_(i+1))^2 + g(x_(i+1)) + h must hold to ensure continuity from spline to spline.
    Last edited: Jan 22, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Cubic Spline what is the logic in constructing it?
  1. Cubic Splines (Replies: 0)