restart:# NEWTON'S INTERPOLATORY DIVIDED-DIFFERENCE FORMULA ALGORITHM 3.2 ## To obtain the divided-difference coefficients of the # interpolatory polynomial P on the (n+1) distinct numbers x(0), # x(1), ..., x(n) for the function f: ## INPUT: numbers x(0), x(1), ..., x(n); values f(x(0)), f(x(1)), # ..., f(x(n)) as the first column Q(0,0), Q(1,0), ..., # Q(N,0) of Q, or may be computed if function f is supplied.# # OUTPUT: the numbers Q(0,0), Q(1,1), ..., Q(N,N) where# P(x) = Q(0,0) + Q(1,1)*(x-x(0)) + Q(2,2)*(x-x(0))* # (x-x(1)) +... + Q(N,N)*(x-x(0))*(x-x(1))*...*(x-x(N-1)).print(`Newtons form of the interpolation polynomial\134n`):OK := FALSE:while OK = FALSE doprint(`Choice of input method: `):print(`1. Input entry by entry from keyboard `):print(`2. Input data from a text file `):print(`3. Generate data using a function F `):print(`Choose 1, 2, or 3 please `):FLAG := scanf(`%d`)[1]: print(`Entry = `): print(FLAG):if FLAG = 1 or FLAG = 2 or FLAG = 3 thenOK := TRUE:fi:od:if FLAG = 1 thenOK := FALSE:while OK = FALSE doprint(`Input n `):N := scanf(`%d`)[1]:print(`n = `):print(N):if N > 0 thenOK := TRUE:for I1 from 0 to N doprint(`Input X(i) and F(X(i)) for i = `):print(I1):print(`separated by space `):X[I1] := scanf(`%f`)[1]:Q[I1,0] := scanf(`%f`)[1]: print(`Entries are `): print(X[I1],Q[I1,0]):od:else print(`Number must be a positive integer `):fi:od:fi:if FLAG = 2 thenprint(`Has a text file been created with the data in two columns ? `):print(`Enter Y or N `):A := scanf(` %c`)[1]:print(`Entry = `):print(A):if A = "Y" or A = "y" thenprint(`Input the file name in the form - `):print(`drive:\134name.ext `):print(`For example: A:\134\134DATA.DTA `):NAME := scanf(`%s`)[1]:print(`Entry = `):print(NAME):INP := fopen(NAME,READ,TEXT):OK := FALSE:while OK = FALSE doprint(`Input n `): N := scanf(`%d`)[1]:print(`n = `):print(N):if N > 0 then for I1 from 0 to N doX[I1] := fscanf(INP, `%f`)[1]:Q[I1,0] := fscanf(INP, `%f`)[1]:od:fclose(INP):OK := TRUE:elseprint(`Number must be a positive integer `)fi:od:elseprint(`Please create the input file in two column `):print(`form with the X values and `):print(`F(X) values in the corresponding columns. `):print(`The program will end so the input file can `):print(`be created. `):OK := FALSE:fi:fi:if FLAG = 3 thenprint(`Input the function F(x) in terms of x `):print(`For example: cos(x) `):F := scanf(`%a`)[1]:print(`F(x) = `):print(F):F := unapply(F,x):OK := FALSE:while OK = FALSE doprint(`Input n `):N := scanf(`%d`)[1]:print(`n = `):print(N):if N > 0 thenfor I1 from 0 to N doprint(`Input X(i) for i = `):print(I1):X[I1] := scanf(`%f`)[1]:Q[I1,0] := F(X[I1]):od:OK := TRUE:elseprint(`Number must be a positive integer `):fi:od:fi:if OK = TRUE thenprint(`Select output destination `):print(`1. Screen `):print(`2. Text file `):print(`Enter 1 or 2 `):FLAG := scanf(`%d`)[1]:print(`Entry = `):print(FLAG):if FLAG = 2 thenprint(`Input the file name in the form - drive:\134\134name.ext `):print(`For example: A:\134\134OUTPUT.DTA `):NAME := scanf(`%s`)[1]:print(`Entry = `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `NEWTONS INTERPOLATION POLYNOMIAL\134n\134n`):# STEP 1for I1 from 1 to N dofor J from 1 to I1 doQ[I1,J] := (Q[I1,J-1] - Q[I1-1,J-1]) / (X[I1] - X[I1-J]):od:od:# STEP 2fprintf(OUP, `Input data follows:\134n`): for I1 from 0 to N dofprintf(OUP, `X(%d) = %12.8f F(X(%d)) = %12.8f\134n`, I1, X[I1], I1, Q[I1,0]): od:fprintf(OUP, `\134nThe coefficients Q(0,0), ..., Q(N,N) are:\134n`): for I1 from 0 to N dofprintf(OUP, `%12.8f\134n`, Q[I1,I1]):od:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:JSFH