Lagrange 's Interpolation | To represent a data point corresponding to x and y = f ( x ) ; function to interpolate the given data points using Lagrange 's formula xi corresponds to the new data point whose value is to be obtained n represents the number of known data points ; let result = 0 ; Initialize result ; Compute individual terms of above formula ; Add current term to result ; creating an array of 4 known data points ; Using the interpolate function to obtain a data point corresponding to x = 3
class Data { constructor ( x , y ) { this . x = x ; this . y = y ; } } function interpolate ( f , xi , n ) { for ( let i = 0 ; i < n ; i ++ ) { let term = f [ i ] . y ; for ( let j = 0 ; j < n ; j ++ ) { if ( j != i ) term = term * ( xi - f [ j ] . x ) / ( f [ i ] . x - f [ j ] . x ) ; } result += term ; } return result ; } let f = [ new Data ( 0 , 2 ) , new Data ( 1 , 3 ) , new Data ( 2 , 12 ) , new Data ( 5 , 147 ) ] ; document . write ( " " + interpolate ( f , 3 , 4 ) ) ;