Show all scripts

Cubic Splines

Category: Fit/Interpolate/Extrapolate
Description: Functions to calculate the natural cubic spline and the constrained cubic spline of a data set. These draw smooth curves through a series of data points. The constrainedcubicspline() function has clamped ends. This avoids the oscillations that can sometimes appear near the ends of spline fitted curves or where data bunches. However, the constrainedcubicspline() function does this at the expense of smoothness In the NaturalCubicSpline() function, a series of x,y data points (with x increasing acroos the series) is passed into the function as an image. An x value passed into the function returns the corresponding y value (interpolated). The option to extrapolate the curve outside the data limits is also controlled by the boolean parameter extrapolate. The array of data points (x0, y0; x1 y1; etc) is passed into the function as an image. The image contains the point values stored in pixels. The image is two pixels high and as many pixels wide as the data set. The format of the the data is: x0, x1, x2, x3, . . . . xn y0, y1, y2, y3, . . . . yn If the image is anything other than 2 pixels high, or if the x values are not sorted into increasing order - left to right, then the function reports an error. The other function arguments are a value in x and a boolean called extrapolate. When the latter is set to zero, any x values which lie outside the range of x in the dataset result in the function returning a zero value for y. If extrapolate is set to 1, then the cubic function is extrapolated from the end members. This may work satisfactorily for small extrapolations on smooth curves but for large extrapolations it becomes meaningless due to inflections. The spline is calculated as y=yiA + y2B + y'1C + y'2D, where A, B, C and D are constants and y1 and y2 are the y values of the data points straddling the x value and y'1 and y'2 are the second derivatives at the neighbouring points. As these data are calcuated for each passed in value of x the calculation is slow. However, this function has the advantage that it is simple to use, because it is a single function. A much faster method of obtaining the spline has been created (fastcubicspline()). However, this requires using two functions - one to calculate the set of constants (calculatesplineconstants()) and another to use those constants from a lookup table to compute y values (quickly) in a loop (fastcubicspline()). It is more cumbersome to use but is about 10x faster than this method. This function seem very much slower than the fast spline function, but that is simply because the loop which plots the value of y over a range of x, updates the displayed image after every calculation. This is visually appealling, but slows things down lots. Removing the updateimage() command from the loops will speed it up. The example script creates a set of data then plots the natural cubic spline and the constrained cubic spline plots through it.
Comments: There is a limitation (or conflict) in User Defined Language with DM script. The operation "/" and "*" cannot be defined as operators in Notepad++ once we define "/*" and "*/" as comment block markers. So I have left "/" and "*" out from the operator list
Acknowledgements: The constrained cubic spline function is based on the CJC Kruger's web page
Script version: 1.0
DM/GMS version: 3.11
Script written as a function: Yes
Source: Show code
Download Source Code: Download
Download Zip File: Download