Linear Interpolation Method Using C Programming
Earlier in Linear Interpolation Method Algorithm article we discussed about interpolation and we developed an algorithm for interpolation using Linear interpolation Method. And in another article Linear Interpolation Method Pseudocode, we developed pseudocode for this method. In this tutorial we are going to implement Linear Interpolation Method using C Programming Language.
C Program for Linear Interpolation Method
#include<stdio.h>
#include<conio.h>
int main()
{
float x0,y0,x1,y1,xp,yp;
clrscr();
/* Inputs */
printf("Enter first point (x0,y0):\n");
scanf("%f%f",&x0,&y0);
printf("Enter second point (x1,y1):\n");
scanf("%f%f",&x1,&y1);
printf("Enter interpolation point: ");
scanf("%f", &xp);
/* Calculation */
yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0);
printf("Interpolated value at %0.3f is %0.3f", xp, yp);
getch();
return 0;
}
Output: Linear Interpolation Using C Programming Language
Consider we have interpolation problem stated as: "From some observation it is found that pressure recorded at temperature 35°C is 5.6KPa and at 40°C is 7.4 KPa. Later it is required to use pressure at 37°C which is not in observation table. So pressure value at 37°C need to be Interpolated and this can be calculated using above program as:"
Enter first point (x0,y0): 35 ↲ 5.6 ↲ Enter Second point (x1,y1): 40 ↲ 7.4 ↲ Enter interpolation point: 37 ↲ Interpolated value at 37.000 is 6.320 Note: ↲ indicates ENTER is pressed.
Recommended Readings