During my current project
astro patterns
I was looking for an algorithm in C or C + + for interpolation with periodic splines. Splines are piecewise cubic functions which interpolate a given set of points of a function. You have the property that they all sufficiently regular interpolating functions, the integral of the square minimize the second derivative. This clearly means that they are trying to do their job of interpolation with minimum curvature: any other interpolating function curves to some extent over the spline. This makes splines for the numerical analysis more attractive than, say, the classical interpolating N-th order for N +1 nodes, which assumes even at the nodes, the desired function values, but can oscillate at inappropriate choice of the nodes in the areas heavily between them. A spline is so to speak, the mathematical form of a ruler
curve (which is a commercially available flexible rod in the office of a special plastic, with which helps to interpolating functions can draw with a pencil). should have
Although I dealt myself already 23 years ago in studies with these entities and since then delivered thousands of students in a programming exercise for Applied Mathematics an algorithm for their calculation, I found no free C, Java or C + + - Code on the Internet, with periodic splines, as I needed it, could be calculated.
well there is the so-called "natural spline" and splines with second derivative prescribed at the edges of a 1989 Peter and Nigel wrote spline function with the mingw-Windows version of GCC 4.4.0 immediately isolated and run is generated (
http://www.mech.uq.edu.au/staff/jacobs/nm_lib/cmathsrc/
) and a nice slim binary of just 22.8 KB. Because it is pure C, I can also use the Tiny C Compiler
by Fabrice Bellard, and even come to only 5.6 KB Binary large including a calling small test program.
Unfortunately, the areas covered by Nigel and Peter splines with prescribed second derivatives at the boundary points is not what I needed for my purposes. The function would interpolate that I lived on the astrological houses
circle. It is therefore a periodic function, and for this you need the
periodic spline
that is in the Calculate little more complicated, because he no longer leads to a linear system of equations with a tridiagonal coefficient matrix: The corners top right and bottom left in the matrix can contain a non-zero value.
For people like me that their textbooks have kept on Numerical Mathematics somewhere in the attic in a box, it is enjoyable, a
excellent internship and seminar preparation
be found by an anonymous Regensburg students on the Web:
The paper leaves no questions about theory the splines open and derives the linear equations to be solved for the different types of cubic splines, whereby the periodic spline is discussed in detail.
remained the only task, the linear system of periodic splines in its general form to solve. Here, the wiki helped to Computational Fluid Dynamics, which contains some very clearly written article on tridiagonal matrices and its variants (for an introduction to
is the article about the Thomas algorithm
).
In C + +, one would consider the spline as an object that gets in the design or by calling a special method provided the node and then calculates the coefficients of cubic polynomials occurring and stored as a global member variables. For the actual Calculation of the function value at any intermediate value of the class provides a special method to then
eval ()
.
That's how my class
PeriodicSpline.cpp
I hereby to the public for the calculation of periodic splines provide. It is independent from the project
astro
patterns used in any context.
As an example of the use of a program should serve the data.txt the array of nodes from a file
reads, calculates the periodic spline, at 100 points and evaluates the results to a file
curveData.txt
writes.
# include <cstdlib>
#include <iostream>
#include <fstream>
#include "PeriodicSpline.hpp"
int main(int argc, char *argv[])
{
double xi, yi, t, period;
size_t i;
vector<double> x,y;
string msg;
// Parse the knots as pairs of doubles from an input file
ifstream dataFile( "data.txt" );
while ( dataFile >> xi >> yi ) { x.push_back(xi);
y.push_back(yi);
}
cout << "dimension: " << x.size() << endl;
period = x[x.size()-1] - x[0];
for (i = 0; i < x.size(); i++ ) { cout << x[i] << ":" << y[i] << endl; } cout << endl; try {
// Compute the spline
PeriodicSpline s(x,y);
// Evaluate the spline values for 100 points
curve ofstream data ("curveData.txt"); for (i = 0; i \u0026lt;100; i + +) { \u0026lt;\u0026lt;s.eval (t) \u0026lt;\u0026lt;endl;}
} catch (string msg) {
cerr \u0026lt;\u0026lt;msg;
exit (EXIT_FAILURE);}
}
Note .- The used input and output streams of this sample program make the code clear and readable, have However, at a price. By merely includi of
\u0026lt;iostream> and \u0026lt;fstream> increased the size of my executable of around 50 KB to over 500 KB! I do not know whether this is due to the mingw installation or on other platforms, including Linux occurs. The phenomenon is known and is regarded as unproblematic . It bothers me anyway. I can not imagine that the whole, actually used in the executable code is packaged and expect an intelligent linker that it packs only the truly necessary within the executable.
0 comments:
Post a Comment