/* Bilinear Interpolation (15 points) MATH BACKGROUND (Linear Interpolation): ======================================= ^ f(x2) |..........* | /. | / . p=f(x)|.......? | /. . | / . . | / . . f(x1) |...* . . | . . . --------------------> x1 x x2 Linear interpolation is a common method for estimating the value of a function at a point x for which no data is available, but data exists for points located on either side of x on the horizontal axis. Given two points x1 and x2 and the value of the function evaluated at these points f(x1) and f(x2), using linear interpolation one can estimate the value of the function p=f(x) at another point x that lies between x1 and x2. The linear interpolation formula is as follows: (x - x1) (x2 - x) p = f(x) = ---------- f(x2) + ---------- f(x1) (x2 - x1) (x2 - x1) In other words, the estimation is performed by fitting a straight line between the points (x1, f(x1)) and (x2, f(x2)) and using this line to estimate the values of the function in the interval (x1, x2). Note that we don't even need to know the analytical form of the function; we only need to know its values at two points. MATH BACKGROUND (Bilinear Interpolation): ========================================= ^ | Q12 R2 Q22 y2 |...*.......o....* | . . . | . .P . y |...........?..... | . . . | . . . | .Q11 .R1 .Q21 y1 |...*.......o....* | . . . | . . . ----------------------> x1 x x2 Bilinear interpolation is a natural extension of linear interpolation that can be used with functions of two variables, e.g., f(x,y). Once again, we don't need to know the analytical form of the function. We only need to know its value at four points that form a rectangle, which contains the point of interest (x,y). To find an estimate for the value of the function at the point (x,y), where x1 #include #include double linearInterpolation(double x1, double f_x1, double x2, double f_x2, double x) { double result = (x - x1)/(x2-x1)*f_x2 + (x2-x)/(x2-x1)*f_x1; return result; } int main(void) { double x1, x2; double y1, y2; double x, y; double Q11, Q12, Q21, Q22; printf("Please enter x1: "); scanf("%lf", &x1); printf("Please enter x2: "); scanf("%lf", &x2); printf("Please enter y1: "); scanf("%lf", &y1); printf("Please enter y2: "); scanf("%lf", &y2); printf("Please enter x: "); scanf("%lf", &x); printf("Please enter y: "); scanf("%lf", &y); printf("Please enter Q11=f(x1,y1): "); scanf("%lf", &Q11); printf("Please enter Q12=f(x1,y2): "); scanf("%lf", &Q12); printf("Please enter Q21=f(x2,y1): "); scanf("%lf", &Q21); printf("Please enter Q22=f(x2,y2): "); scanf("%lf", &Q22); double R1 = linearInterpolation(x1, Q11, x2, Q21, x); double R2 = linearInterpolation(x1, Q12, x2, Q22, x); printf("The nterpolated value of the function at R1=(x,y1) is %g\n", R1); printf("The nterpolated value of the function at R2=(x,y2) is %g\n", R2); double P = linearInterpolation(y1, R1, y2, R2, y); printf("\nThe interpolated value of the function at (x=%g, y=%g) is equal to %g\n\n", x, y, P); system("pause"); }