Cubic Spline what is the logic in constructing it?

  • Thread starter Thread starter Applejacks01
  • Start date Start date
  • Tags Tags
    Cubic Logic
Click For Summary
SUMMARY

The discussion focuses on the construction of cubic splines in VBA, specifically addressing issues with logic that led to incorrect outputs. The user initially produced linear splines instead of cubic ones due to incorrect constraints on derivatives at the endpoints. After revising the logic, they established that the first and second derivatives at the endpoints should equal zero, while ensuring continuity and equality of derivatives at the nodes between splines. The final solution involved removing unnecessary constraints and correctly applying the cubic spline equations.

PREREQUISITES
  • Understanding of cubic spline interpolation
  • Familiarity with VBA programming
  • Knowledge of piecewise functions
  • Basic calculus concepts, particularly derivatives
NEXT STEPS
  • Study the mathematical foundations of cubic splines
  • Learn about implementing cubic splines in VBA
  • Explore numerical methods for spline interpolation
  • Investigate the use of Maple for mathematical computations
USEFUL FOR

Mathematicians, software developers working with numerical methods, and anyone involved in data interpolation and curve fitting using cubic splines.

Applejacks01
Messages
26
Reaction score
0
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:
Physics news on Phys.org


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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 21 ·
Replies
21
Views
2K