Interpolation and extrapolation functions



	1. Definitions

	Using tables or graphs, interpolation is  the method to get values
	knowing others located  between given points. Extrapolation is
	a similar method, but the point whom we solve for values extends beyond
	the given data.

	

	The data x1, y1 of the points P1 and x2, y2 of P2, located on the same line,
	represented by a linear equation y = a x + b are measured. We can write for :
	- an extrapolation: (ye - y2)/(x2 - xe) = (y2 - y1)/(x2 - x1), and for
	- an interpolation: (yi - y1)/(xi - x1) = (y2 - y1)/(x2 - x1)

	Or:
	Knowing xe, xi and solving for ye, yi:
	- extrapolation: ye  = y2 + (x2 - xe)(y2 - y1)/(x2 - x1),
	- interpolation: yi  = y1 + (xi - x1)(y2 - y1)/(x2 - x1)

	Or:
	Knowing ye, yi and solving for xe, xi:

	- extrapolation: xe  = x2 - (ye - y2)(x2 - x1)/(y2 - y1)
	- interpolation: xi = x1 + yi - y1)(x2 - x1)/(y2 - y1)


	2. Example in C language:
	
	#include <stdio.h>
	#include <math.h>

	int main()
	{

	float x1, y1;
	printf("\n Enter two coordinates of a point P1: x1 and y1:\n");
	scanf("%f %f", &x1,&y1);

	float x2, y2;
	printf("\n Enter two coordinates of a point P2: x2 and y2:\n");
	scanf("%f %f", &x2,&y2);

	float xi, yi, xe, ye;

	printf("\n Enter an x_coordinate xi of a point I(xi,yi) for interpolation:\n");
	scanf("%f", &xi);
		yi  = y1 + (xi - x1)*(y2 - y1)/(x2 - x1);
	printf("\n Its corresponding y_coordinate yi is %2.3f\n",yi);

	printf("\n Enter an x_coordinate xe of a point E(xe,ye) for extrapolation:\n");
	scanf("%f", &xe);
		ye  = y2 + (x2 - xe)*(y2 - y1)/(x2 - x1);
	printf("\n Its corresponding y_coordinate ye is %2.3f\n",ye);


	// ----------------------

		printf("\n Enter a y_coordinate yi of a point I(xi,yi) for interpolation:\n");
	scanf("%f", &yi);
		xi = x1 + (yi - y1)*(x2 - x1)/(y2 - y1);
	printf("\n Its corresponding x_coordinate xi is %2.3f\n",xi);

	printf("\n Enter a y_coordinate ye of a point E(xe,ye) for extrapolation:\n");
	scanf("%f", &ye);
		xe  = x2 - (ye - y2)*(x2 - x1)/(y2 - y1);
	printf("\n Its corresponding x_coordinate xe is %2.3f\n",xe);

	return 0;
	}