Newton Forward And Backward Interpolation | Calculation of u mentioned in formula ; Calculating factorial of given n ; number of values given ; y is used for difference table and y [ 0 ] used for input ; Calculating the backward difference table ; Displaying the backward difference table ; Value to interpolate at ; Initializing u and sum
function u_cal ( u , n ) { var temp = u ; for ( var i = 1 ; i < n ; i ++ ) temp = temp * ( u + i ) ; return temp ; } function fact ( n ) { var f = 1 ; for ( var i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } var n = 5 ; var x = [ 1891 , 1901 , 1911 , 1921 , 1931 ] ; var y = Array ( n ) . fill ( 0.0 ) . map ( x => Array ( n ) . fill ( 0.0 ) ) ; y [ 0 ] [ 0 ] = 46 ; y [ 1 ] [ 0 ] = 66 ; y [ 2 ] [ 0 ] = 81 ; y [ 3 ] [ 0 ] = 93 ; y [ 4 ] [ 0 ] = 101 ; for ( var i = 1 ; i < n ; i ++ ) { for ( var j = n - 1 ; j >= i ; j -- ) y [ j ] [ i ] = y [ j ] [ i - 1 ] - y [ j - 1 ] [ i - 1 ] ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) document . write ( y [ i ] [ j ] + " " ) ; document . write ( ' ' ) ; ; } var value = 1925 ; var sum = y [ n - 1 ] [ 0 ] ; var u = ( value - x [ n - 1 ] ) / ( x [ 1 ] - x [ 0 ] ) ; for ( var i = 1 ; i < n ; i ++ ) { sum = sum + ( u_cal ( u , i ) * y [ n - 1 ] [ i ] ) / fact ( i ) ; } document . write ( " " + value + " " + sum ) ;