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
< ? php function u_cal ( $ u , $ n ) { $ temp = $ u ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ temp = $ temp * ( $ u + $ i ) ; return $ temp ; } function fact ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f *= $ i ; return $ f ; } $ n = 5 ; $ x = array ( 1891 , 1901 , 1911 , 1921 , 1931 ) ; $ y = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; $ y [ 0 ] [ 0 ] = 46 ; $ y [ 1 ] [ 0 ] = 66 ; $ y [ 2 ] [ 0 ] = 81 ; $ y [ 3 ] [ 0 ] = 93 ; $ y [ 4 ] [ 0 ] = 101 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ n - 1 ; $ j >= $ i ; $ j -- ) $ y [ $ j ] [ $ i ] = $ y [ $ j ] [ $ i - 1 ] - $ y [ $ j - 1 ] [ $ i - 1 ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) print ( $ y [ $ i ] [ $ j ] . " TABSYMBOL " ) ; print ( " STRNEWLINE " ) ; } $ value = 1925 ; $ sum = $ y [ $ n - 1 ] [ 0 ] ; $ u = ( $ value - $ x [ $ n - 1 ] ) / ( $ x [ 1 ] - $ x [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( u_cal ( $ u , $ i ) * $ y [ $ n - 1 ] [ $ i ] ) / fact ( $ i ) ; } print ( " Value at " ▁ . ▁ $ value ▁ . STRNEWLINE TABSYMBOL " is " ? >