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 do print(`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 then OK := TRUE: fi: od: if FLAG = 1 then OK := FALSE: while OK = FALSE do print(`Input n `): N := scanf(`%d`)[1]:print(`n = `):print(N): if N > 0 then OK := TRUE: for I1 from 0 to N do print(`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 then print(`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" then print(`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 do print(`Input n `): N := scanf(`%d`)[1]:print(`n = `):print(N): if N > 0 then for I1 from 0 to N do X[I1] := fscanf(INP, `%f`)[1]: Q[I1,0] := fscanf(INP, `%f`)[1]: od: fclose(INP): OK := TRUE: else print(`Number must be a positive integer `) fi: od: else print(`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 then print(`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 do print(`Input n `): N := scanf(`%d`)[1]:print(`n = `):print(N): if N > 0 then for I1 from 0 to N do print(`Input X(i) for i = `):print(I1): X[I1] := scanf(`%f`)[1]: Q[I1,0] := F(X[I1]): od: OK := TRUE: else print(`Number must be a positive integer `): fi: od: fi: if OK = TRUE then print(`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 then print(`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): else OUP := default: fi: fprintf(OUP, `NEWTONS INTERPOLATION POLYNOMIAL\134n\134n`): # STEP 1 for I1 from 1 to N do for J from 1 to I1 do Q[I1,J] := (Q[I1,J-1] - Q[I1-1,J-1]) / (X[I1] - X[I1-J]): od: od: # STEP 2 fprintf(OUP, `Input data follows:\134n`): for I1 from 0 to N do fprintf(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 do fprintf(OUP, `%12.8f\134n`, Q[I1,I1]): od: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: SU5OZXd0b25zfmZvcm1+b2Z+dGhlfmludGVycG9sYXRpb25+cG9seW5vbWlhbHwrRzYi STlDaG9pY2V+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(1) = 1.30000000 F(X(1)) = 0.62008600 X(2) = 1.60000000 F(X(2)) = 0.45540220 X(3) = 1.90000000 F(X(3)) = 0.28181860 The coefficients Q(0,0), ..., Q(N,N) are: 0.76519770 -0.48370567 -0.10873389 0.06587839 JSFH