Linear Spline – Piecewise Interpolation [C PROGRAM]

Lagrange or Newton polynomial interpolations are useful interpolation techniques to have in your sleeves, but they don’t always give the best or desired result. As the degree of the polynomial increases, so do the wiggles.

Therefore, it is often advantageous to use piecewise interpolation, also known as spline interpolation.

A spline is simply a curve that connects two or more specific points.

Originally, spline was a term for elastic rulers that were bent to pass through a number of predefined points (“knots”). These were used to make technical drawings for shipbuilding and construction by hand.

In this post I will be sharing with you a C program that performs linear spline interpolation.
The user is asked to enter a set of x and y-axis data-points, and then each of these is joined by a straight line.
So the code would involve finding the equation of line connecting the two points.

You might have noticed this kind of interpolation in chart plotting softwares like Origin, Excel, Gnuplot where when you plot the data-set using the line chart, then the points are joined together by line segments as shown below.

So basically the code would need to generate a set of line segments for 2 adjacent points given by, (x_i,y_i) and (x_{i+1},y_{i+1}) .
The equation can be found by using the following formula:

So, here’s what we’re gonna do in the program:
1. We’re gonna ask the user to enter the no. of data points.
2. Then, we’re gonna let the user enter these data-points by running a simple for loop.
3. Next, we will calculate the equation of line between 2 adjacent points, and use it to interpolate the values.
4. We will store the interpolated values at a desired interval of say, 0.01, in arrays.
5. Then we will store them in a file and scatter plot them using a suitable software.


**********LINEAR SPLINE***********
Function to perform piecewise linear spline interpolation
n: no. of data-points
x[n]: x-axis points
y[n]: y-axis points
N: size of the array storing the interpolated x and y points
X[n]: array that stores the interpolated x-axis points
Y[n]: array that stores the interpolated y-axis points
void lSpline(int n, double x[n], double y[n], int N, double h, double X[N], double Y[N]){
		int i;
		int j=0;
			double yn,xn;
	int n;
	int N=0; //N is the no. of interpolated values
	int i;
	double h=0.01; //Space interval at which interpolated values are calculated
	printf("Enter the no. of data-points: (n)\n");
	double x[n];
	double y[n];
	printf("Enter the x-axis data points:\n");
	printf("Enter the y-axis data points:\n");
	//The following procedure calculates N
	//A little adjustment to get the correct value of N
	printf("\nThe no. of interpolated values N= %d\n",N);
	double Y[N];
	double X[N];
	//Perform piece-wise linear interpolation
	//Store the interpolated values in a File
	FILE *fp = "NULL";


Android App:

I’ve created a few data interpolation android apps, that you can check out.
Lagrange Interpolation:
Least-squares Curve Fitting:

References and Resources:

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT and Machine Learning related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *