CodeCogs - An iteractive open source Numerical library Welcome... Login
CodeCogs
shopping cart
OSXWindowsLinux
Search CodeCogs
Numerical Components

Valid RSS

MathsInterpolation

Cubic

Only available under a commercial licence
COST (GBP)
this unit 8.32
sub units 0.00
add a commercial licence to your cart
0
viewed 12404 times and licensed 84 times

Interpolates a given set of points using cubic spline fitting.

Controller: CodeCogs  Contact Controller
+View other versions (2)
Contents hide toc
buy now     add cart

Interface

#include <codecogs/maths/interpolation/cubic.h>

using namespace Maths::Interpolation;

class Cubic
Interpolates a given set of points using cubic spline fitting.
[constructor]Cubic (int n, double *x, double *y)
Class constructor
[destructor]~Cubic ()
m_class destructor
doublegetValue (double x)
Returns an interpolated value.
intm_n
double*m_x
double Cubic_once (int N, double *x, double *y, double a)
A static function implementing the Cubic Class for one off calculations
ExcelReal cc_interpolationCubic_once (Integer N, Range x, Range y, Real a)
This function is available as a Microsoft Excel add-in.

Class Documentation

 
Cubic
A cubic spline is a piecewise cubic polynomial such that the function, its derivative and its second derivative are continuous at the interpolation nodes. The natural cubic spline has zero second derivatives at the endpoints. It is the smoothest of all possible interpolating curves in the sense that it minimizes the integral of the square of the second derivative. For more information on this algorithm, please see references.

An important detail when using this class is that the abscissas array (the x-axis) given as argument to the constructor needs to be sorted in ascending order. For an optimal fit, the elements should also be equally spaced, though this is by no mean essential.

Below you will find the interpolation graphs for a set of points obtained by evaluating the function f(x) = \sin(2x) / x, displayed in light blue, at particular abscissas. The spline fitting curve, displayed in red, has been calculated using this class. In the first graph there had been chosen a number of 12 points, while in the second 36 points were considered. You may notice the root mean squared error in each of the cases.

1/cubic-378.png
+

References:

Example 1:
The following example displays 20 interpolated values (you may change this amount through the N_out variable) for the given function f(x) with abscissas equally spaced in the [ \pi, 3\pi] interval. The X and Y coordinate arrays are initialized by evaluating this function for N = 12 points equally spaced in the domain from \pi to 5 \pi.
#include <codecogs/maths/interpolation/cubic.h>
 
#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;
 
#define PI  3.1415
#define N   12
 
int main() 
{
    // Declare and initialize two arrays to hold the coordinates of the initial data points
    double x[N], y[N];
 
    // Generate the points
    double xx = PI, step = 4 * PI / (N - 1);
    for (int i = 0; i < N; ++i, xx += step) {
        x[i] = xx;
        y[i] = sin(2 * xx) / xx;
    }
 
    // Initialize the Cubic spline interpolation routine with known data points
    Maths::Interpolation::Cubic A(N, x, y);
 
    // Interrogate spline curve to find interpolated values
    int N_out = 20;
    xx = PI, step = (3 * PI) / (N_out - 1);
    for (int i = 0; i < N_out; ++i, xx += step) {
        cout << "x = " << setw(7) << xx << "  y = ";
        cout << setw(13) << A.getValue(xx) << endl;
  }
  return 0;
}
Output:
x =  3.1415  y = -5.89868e-005
x = 3.63753  y =      0.233061
x = 4.13355  y =      0.216427
x = 4.62958  y =     0.0486148
x = 5.12561  y =     -0.133157
x = 5.62163  y =     -0.172031
x = 6.11766  y =    -0.0456079
x = 6.61368  y =     0.0906686
x = 7.10971  y =      0.116462
x = 7.60574  y =     0.0557287
x = 8.10176  y =      -0.03875
x = 8.59779  y =      -0.10346
x = 9.09382  y =    -0.0734111
x = 9.58984  y =     0.0298435
x = 10.0859  y =      0.094886
x = 10.5819  y =     0.0588743
x = 11.0779  y =    -0.0171021
x = 11.5739  y =    -0.0630512
x =   12.07  y =    -0.0601684
x =  12.566  y =   -0.00994154

See Also

Also consider the regression methods: Regression/Discrete, Regression/Forsythe, Regression/Orthogonal, Regression/Stiefel
Authors:
Lucian Bentea (August 2005)
Source Code:

To view or download source code you need to buy a Commercial licence.

buy now     add cart

Not a member, then Register with CodeCogs. Already a Member, then Login.

Members of Cubic

 
Cubicintn
double*x
double*y )[constructor]
Initializes the necessary data for following evaluations of the spline.
Parameters:
nThe number of initial points
xThe x-coordinates for the initial points
yThe y-coordinates for the initial points

 
doublegetValuedoublex )
Returns the approximated ordinate at the given abscissa.
Note:
The value of the x parameter needs to be in the X[1]...X[N - 1] interval (including endpoints).
Parameters:
xThe abscissa of the interpolation point


Function Documentation

Cubic Once Calculator
  
Add calculator to website or email
 
doubleCubic_onceintN
double*x
double*y
doublea )
This function implements the Cubic class for one off calculations, thereby avoid the need to instantiate the Cubic class yourself.
Example 2:
The following graph is constructed from interpolating the following values:
x = 1  y = 0.22
x = 2  y = 0.04
x = 3  y = -0.13
x = 4  y = -0.17
x = 5  y = -0.04
x = 6  y = 0.09
x = 7  y = 0.11
\graph  N=7, x="1 2 3 4 5 6 7", y="0.22 0.04 -0.13 -0.17 -0.04 0.09 0.11", a=1:7
Parameters:
NThe number of initial points
xThe x-coordinates for the initial points
yThe y-coordinates for the initial points
aThe x-coordinate for the output point
Returns:
the interpolated y-coordinate that corresponds to a.
Source Code:

To view or download source code you need to buy a Commercial licence.

buy now     add cart

Not a member, then Register with CodeCogs. Already a Member, then Login.


Page Comments

john@versalab.com\′s Photo
15 Feb 08, 12:01AM
(3 replies)
Overshoot
I would like some advice on this

N=10 1 2 3 4 5 6 7 8 9 10 0 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 1:10

Odd numbers perhaps, but it illustrates an overshoot after the first 8.75.
will\′s Photo
15 Feb 08, 12:08AM
With the number you have given then any interpolation scheme will do the same, these systems don't like sharp corners. Even a very higher order polynomial, or Fourier approach (I've yet to write these but will), will have local overshot.

Therefore what you have to do is locally force conformity, it'll require a little bit of experimentation, but try this:

N=12 1 2 2.1 2.2 3 4 5 6 7 8 9 10 0 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 8.75 1:10

I've bolded the two extra terms (I hope you see these at 2.1 and 2.2).
Need_Cubic\′s Photo
5 Mar 08, 9:12PM
Does anyone know of any algorithm that will evaluate a "closeness of fit"?

For example, I have two stored cubic splines that I want to compare to a new spline and pick the one that is closer.

Any thoughts?
will\′s Photo
5 Mar 08, 11:15PM
Typical approach is to look at the Root Mean Squares (RMS), something like: where
  • f(x) is say your fitted function
  • g(x) are your original points

Obviously you select values of x that are at know points in g(x) and apply to f(x).

Consider this example
double f[4]={1,2,3,4};      // approx values (say)
  double g[4]={1.1,2,2.9,4};  // real values
 
  double RMS=0;
  for(int i=0;i<4;i++) RMS+=pow(f[i]-g[i],2);
  RMS=sqrt(RMS/4.0);
Format Excel Equations

  You must login to leave a messge


Last Modified: 15 Feb 08 @ 00:10     Page Rendered: 2010-03-14 22:47:44

Valid CSS!   Valid XHTML 1.0 Transitional