00001
00007 #include "../../Exceptions/error.h"
00008
00013 double polilinear( double *xa, double *ya, int n, double x, double lb, double ub)
00014 {
00015 int i = 0;
00016
00017 while (i <= n-1 && x > xa[i]) {
00018 i = i + 1;
00019 }
00020
00021
00022 if (i <= 0) {
00023 return lb;
00024 } else if (i <= 1) {
00025
00026 double a = (x - xa[i-1])/(xa[i] - xa[i-1]);
00027 return ya[i-1] + a*(ya[i] - ya[i-1]);
00028 } else if (i >= n-1) {
00029 return ub;
00030
00031 } else if (i >= n-2) {
00032
00033 double a = (x - xa[i-1])/(xa[i] - xa[i-1]);
00034 return ya[i-1] + a*(ya[i] - ya[i-1]);
00035
00036
00037
00038
00039
00040
00041
00042
00043 } else if (i >= 1 && i <= n-2) {
00044
00045 double A1 = (x - xa[i])*(x - xa[i+1]) / (xa[i-1] - xa[i]) / (xa[i-1] - xa[i+1]);
00046 double B1 = (xa[i-1] - x)*(x - xa[i+1]) / (xa[i-1] - xa[i]) / (xa[i] - xa[i+1]);
00047 double C1 = (xa[i-1] - x)*(xa[i] - x) / (xa[i] - xa[i+1]) / (xa[i-1] - xa[i+1]);
00048 double res = A1*ya[i-1] + B1*ya[i] + C1*ya[i+1];
00049
00050
00051
00052 return res;
00053 } else {
00054
00055
00056 throw error_msg("POLINOM_ERROR", "Polinomial interpolation error");
00057 }
00058 }