## You are here

Homecubic spline interpolation

## Primary tabs

# cubic spline interpolation

Suppose we are given $N+1$ data points $\{(x_{{k}},y_{{k}})\}$ such that

$a=x_{{0}}<\dots<x_{{N}}.$ | (1) |

Then the function $S(x)$ is called a *cubic spline interpolation* if
there exists $N$ cubic polynomials $S_{{k}}(x)$ with coefficients
$s_{{k,i}}\,\,0\leq i\leq 3$ such that the following hold.

1. $S(x)=S_{{k}}(x)=\sum_{{i=0}}^{{3}}s_{{k,i}}(x-x_{{k}})^{{i}}\;\;\;\forall x\in% [x_{{k}},x_{{k+1}}]\;\;\;0\leq k\leq N-1$

2. $S(x_{{k}})=y_{{k}}\;\;\;0\leq k\leq N$

3. $S_{{k}}(x_{{k+1}})=S_{{k+1}}(x_{{k+1}})\;\;\;0\leq k\leq N-2$

4. $S^{{\prime}}_{{k}}(x_{{k+1}})=S^{{\prime}}_{{k+1}}(x_{{k+1}})\;\;\;0\leq k\leq N-2$

5. $S^{{\prime\prime}}_{{k}}(x_{{k+1}})=S^{{\prime\prime}}_{{k+1}}(x_{{k+1}})\;\;% \;0\leq k\leq N-2$

The set of points $\eqref{knot}$ are called the knots. The set of cubic splines on a fixed set of knots, forms a vector space for cubic spline addition and scalar multiplication.

So we see that the cubic spline not only interpolates the data $\{(x_{{k}},y_{{k}})\}$ but matches the first and second derivatives at the knots. Notice, from the above definition, one is free to specify constraints on the endpoints. One common end point constraint is $S^{{\prime\prime}}(a)=0\;\;S^{{\prime\prime}}(b)=0$, which is called the natural spline. Other popular choices are the clamped cubic spline, parabolically terminated spline and curvature-adjusted spline. Cubic splines are frequently used in numerical analysis to fit data. Matlab uses the command spline to find cubic spline interpolations with not-a-knot end point conditions. For example, the following commands would find the cubic spline interpolation of the curve $4\cos(x)+1$ and plot the curve and the interpolation marked with o’s.

x = 0:2*pi; y = 4*cos(x)+1; xx = 0:.001:2*pi; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)

## Mathematics Subject Classification

65-01*no label found*

- Forums
- Planetary Bugs
- HS/Secondary
- University/Tertiary
- Graduate/Advanced
- Industry/Practice
- Research Topics
- LaTeX help
- Math Comptetitions
- Math History
- Math Humor
- PlanetMath Comments
- PlanetMath System Updates and News
- PlanetMath help
- PlanetMath.ORG
- Strategic Communications Development
- The Math Pub
- Testing messages (ignore)

- Other useful stuff
- Corrections

## Comments

## Matlab?

I'm a little concerned with the use of Matlab as an illustrating example.

First, it does not show an algorithm that computes a cubic spline interpolation, it just shows an example of how to use an already implemented routine in Matlab. I don't think this should be PlanetMath's job, the place for this is in Matlab documentation.

Second, if there's a piceof code with a 'plot' command, why not include the plot output in the article?

Third, Matlab is proprietary software, and I think it's a little against the spirit of this site to advertise it. If you look at GNU Octave, together with the Octave-forge package, it can execute the exact same piece of code.

I'm sure this issue will come up again in the future, so I would like to see some discussion. On these questions in general:

* Should PlanetMath try to be language neutral?

* If not, should Free software be pefered to proprietary software?

* If yes, should there be some "standard" syntax for the pseudocode

used to demonstrate algorithms?

## Re: Matlab?

> First, it does not show an algorithm that computes a cubic

> spline interpolation, it just shows an example of how to use

> an already implemented routine in Matlab. I don't think this

> should be PlanetMath's job, the place for this is in Matlab

> documentation.

I agree, there is no mathematical insight to this "demonstration". The algorithm should be re-coded in a simple manner if code is to be presented.

> Third, Matlab is proprietary software, and I think it's a

> little against the spirit of this site to advertise it. If

> you look at GNU Octave, together with the Octave-forge

> package, it can execute the exact same piece of code.

Personally I would tend to go with Octave first, but I think it is a bad idea to exclude mathlab/mathematica/whatever from the field. Octave is really obscure compared to them (and it is really only a clone of *old* matlab). Also there is a good argument for giving people experience with commercially-utilized packiages. Suggesting alternatives cannot hurt, but the issue is not so simple that we can just pick one of {free, commercial} for all of PlanetMath.

> * Should PlanetMath try to be language neutral?

> * If not, should Free software be pefered to proprietary

> software?

I see no reason to standardize on answers to any of these questions. The demonstration should be invariant of language and software; these are just tools that make doing work convenient for the author. What I am saying is not to expunge matlab code in favor of some neutral pseudocode, but to avoid doing anything obfuscated and provide plain-English explanations of what is complex.

> * If yes, should there be some "standard" syntax for the

> pseudocode used to demonstrate algorithms?

This doesn't seem to have anything to do with whether the languages used for workable code are free or proprietary, so I'm not sure why you qualified it.

I lean towards preferring, but not enforcing (certain) standards. Perhaps a good model for this would be w3c "recommendations": they become ad hoc standards because of (1) first complete and widely disseminated formulation (they fill a vacuum), and (2) voluntary merit-based support in the community. In this case, I think authors are actually yearning for some sort of standard. If we give them one, they will probably use it.

Logan wrote (I believe-- adapting an existing style) an algorithms environment, which he uses in his entries. We could use this. It is just up to us to document it, and provide some recommendations for pseudocode and "real" code.

apk

## Re: Matlab?

I personally also use octave and gp-pari, coding most needed functions myself. I also don't think it is wise to exclude any package just because it's commercial. Although any code used should make minimal use of unnecessary features. Any attempts to standardize could backfire, any code published should have been run and tested. If you have to change it to some pseudocode then you are guaranteeing lots of bugs.

The real reason I'm posting is that the example given seems largely replicated from the Matlab help page for spline

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/spline.shtml

## Re: Matlab?

I tend to agree with Igor that the use of non-free software

is somehow against the spirit of the site. It is not so

much commercial vs non commercial as free vs non-free.

I think that the community of Math people (amateurs or professionals)

should really support free computer algebra systems because any restriction

in the use and distribution of algorithms impedes the further progress

of math and science in general.

Besides the software already mentioned there is Maxima, whose

later versions are almost complete. Even if some of the free programs

are not as shiny as the non-free ones or they are missing features

they should be preferred over non free ones. In my humble opinion use

of non free systems should be only the last resource.

## Re: Matlab?

My position is that PlanetMath is not a proper place for examples of using computer algebra systems (CAS). This is not a PlanetCAS or PlanetProgramming. Implementation of cerain mathematical ideas in specific programming product (free or non-free, commercial or freeware) falls outside the scope of PlanetMath.

On the other hand, inclusion of mathematical algorithms with illustrations in any reasonable language should be welcome. The pseudo-code is more preferable because it is language-independent and relieves the reader of burden of unnecessary technical details associated with specific a language.

Boris

## Re: Matlab?

> My position is that PlanetMath is not a proper place for

> examples of using computer algebra systems (CAS). This is

> not a PlanetCAS or PlanetProgramming. Implementation of

> cerain mathematical ideas in specific programming product

> (free or non-free, commercial or freeware) falls outside the

> scope of PlanetMath.

>

> On the other hand, inclusion of mathematical algorithms with

> illustrations in any reasonable language should be welcome.

> The pseudo-code is more preferable because it is

> language-independent and relieves the reader of burden of

> unnecessary technical details associated with specific a

> language.

I strongly agree with this. I think there's little place on PlanetMath for complete implementations in any language. But including descriptions of algorithms and some pseudocode is ok.

I played with the idea of standardizing pseudocode, but from the ensuing discussion is seems that it would be conterproductive. Perhaps some style guidelines would be good.

Last, I think that _references_ to specific languages should be cut down to a minumum. I think the references in AlgebraicComputation are ok, but the one in CubicSplinInterpolation is out of line.

## Re: Matlab?

Take note of a few things:

1. matlab is not a programming language!

2. the commands given show how to compute a spline using matlab'b built in function "spline". This example was given to illustrate how computers may aid in learning.

3. the commands given, are not code, pseudocode or in the format of any programming language. They are commands issued directly to the matlab workspace.

4. The popular (and most widely used) programs for mathematics are the 3 M's, Mathematica, Matlab, and Maple. I am not in anyway advertising these programs. However, Matlab is the main program used by NASA, JPL and commercially in research. It is the preferred program in engineering.

5. The purpose of planetmath is to disseminate mathematical knowledge. Showing ways to apply knowledge learned from planetmath in computing is conducive to education and will aid people both in academia and their professional lives.

6. Personally, I wish more people would show the commands to apply their planetmath work to computing.

7. Cubic Spline Interpolation is what is defined in the subbmission and hence the name is more than appropriate.

## Re: Matlab?

> 1. matlab is not a programming language!

It is.

> 2. the commands given show how to compute a spline using

> matlab'b built in function "spline". This example was given

> to illustrate how computers may aid in learning.

That is really more pure applications than learning, as it did not yield any insight to the algorithm.

> 3. the commands given, are not code, pseudocode or in the

> format of any programming language. They are commands

> issued directly to the matlab workspace.

Matlab's commands *are* its programming language (not that this is particularly relevant to the issue at hand).

> 4. The popular (and most widely used) programs for

> mathematics are the 3 M's, Mathematica, Matlab, and Maple.

> I am not in anyway advertising these programs. However,

> Matlab is the main program used by NASA, JPL and

> commercially in research. It is the preferred program in

> engineering.

Yes, this is why I think it would be foolish to standardize on free alternatives.

> 5. The purpose of planetmath is to disseminate mathematical

> knowledge. Showing ways to apply knowledge learned from

> planetmath in computing is conducive to education and will

> aid people both in academia and their professional lives.

> 6. Personally, I wish more people would show the commands to

> apply their planetmath work to computing.

I think it would be more more elegant and pedagogical to put pseudocode (or the key portions of a matlab implementation) *within* the entry, and an a file attachment for mundane things like calling the built-in spline generator.

apk

## Re: Matlab?

> Take note of a few things:

I will address only the following:

> 2. the commands given show how to compute a spline using

> matlab'b built in function "spline". This example was given

> to illustrate how computers may aid in learning.

> 5. The purpose of planetmath is to disseminate mathematical

> knowledge. Showing ways to apply knowledge learned from

> planetmath in computing is conducive to education and will

> aid people both in academia and their professional lives.

> 6. Personally, I wish more people would show the commands to

> apply their planetmath work to computing.

My main point is that the piece of code that you gave

does not really help learn or apply what the entry talks

about. If you consider it an "application" of some knowledge

then the knowledge is that a cubic spline interpolation

routing exists in Matlab. Which is useful if you're trying

to find a program to compute splines, but not very useful

if you're trying to learn how to produce cubic splines on

a computer by yourself. Then, a description of the algorithm

itself (whatever the format, as long as its clear and

understandable) would be more useful.

I don't think anyone can argue about the popularity of Matlab,

but I don't think you can also argue that it's accesible to

everyone. For example a highschool student might be interested

in cubic splines, but may not have access to Matlab at school

and may not be able to afford it for home use. But just because

a program is free, doesn't meen that it's universally accessible,

either. There could be many reasons why someone cannot use it.

Since PlanetMath's goal is to disseminate mathematical knowledge

that is independent of which computer program it is applied to

or with, I think it should stay as independent as possible

specific computer programs, non matter what they are.

In the same spirit as my first paragraph, you could say that

cubic spline interpolation implementations exist in existing

software packages, such as Matlab, GNU Octave, Maple, Mathematica,

and others. If someone is interested in using any one of these

programs, they can lookup the relevant documentation for

themselves. This kind of statement gives more information

than you already provide, since it names more software packages

where cubic splines are available.

## classification

There is an "Interpolation" section (41A05) and a "Spline approximation"

section (41A15) in MSC. When you browse the encyclopedia by subjects,

having only one classification for each entry may get one miss many

relevant entries. So I suggest to add these two if possible.

Other point: the "natural" and "not-a-knot" end point conditions are the

same, which should be stated.

## Re: classification

well.. when you ran across an entry and it doesn't shows up on the corresponding MSC section

it means the author forgot to add it, please file a meta/minor correction suggesting the proper code to be added to the entry

f

G -----> H G

p \ /_ ----- ~ f(G)

\ / f ker f

G/ker f

## my mistake

The "natural" and "not-a-knot" end conditions are not the same:

"not-a-knot" means x_1 and x_{N-1} have no discontinuity of

S'''. (It improves the fit. See Carl deBoor's "A practical

guide to splines", it should be there).

## cubic spline

This is my entry and I wrote every word of it. I never orphaned it and you should be ashamed of yourself for putting you name on something that you had absolutly to contribution in.

## Re: cubic spline

Why then in the author history it is stated that:

User: tensorking

Why: Orphan

When: 2004-07-08

## Continuous but not matched.

"but matches the first and second derivatives at the knots."

The derivatives are continuous across boundaries but do not in general match the TRUE derivatives anywhere. I assume what you are implying is that the derivatives of the spline on either side of a knot are MATCHED to one another. The original statement seemed misleading on first read.

## Re: Matlab?

I support completely the opinion of Igor.

Greetings for all.