public static double width_interpolate ( double [ ] [ ] data , double x , int nx , int ny ) { int rows = data . length ; double xuno = 0 , xdue = 0 , yuno = 0 , ydue = 0 , y = 0 ; // if 0, interpolate between 0 and the first value of data if ( x >= 0 && x < data [ 0 ] [ nx ] ) { xuno = 0 ; xdue = data [ 0 ] [ nx ] ; yuno = 0 ; ydue = data [ 0 ] [ ny ] ; y = ( ( ydue - yuno ) / ( xdue - xuno ) ) * ( x - xuno ) + yuno ; } // if it is less than 0 and bigger than the maximum, throw error if ( x > data [ ( rows - 1 ) ] [ nx ] || x < 0 ) { throw new RuntimeException ( MessageFormat . format ( "Error in the interpolation algorithm: entering with x = {0} (min = 0.0 max = {1}" , x , data [ ( rows - 1 ) ] [ nx ] ) ) ; } /* trovo i valori limite entro i quali effettuo l'interpolazione lineare */ for ( int i = 0 ; i < rows - 1 ; i ++ ) { if ( x > data [ i ] [ nx ] && x <= data [ ( i + 1 ) ] [ nx ] ) { xuno = data [ i ] [ nx ] ; xdue = data [ ( i + 1 ) ] [ nx ] ; yuno = data [ i ] [ ny ] ; ydue = data [ ( i + 1 ) ] [ ny ] ; y = ( ( ydue - yuno ) / ( xdue - xuno ) ) * ( x - xuno ) + yuno ; } } return y ; }
Linear interpolation between two values