> restart; > # NEVILLE'S ITERATED INTERPOLATION ALGORITHM 3.1 > # > # To evaluate the interpolating polynomial P on the > # (n+1) distinct numbers x(0), ..., x(n) at the number x > # for the function f: > # > # INPUT: numbers x(0),..., x(n) as XX(0),...,XX(N); > # number x; values of f as the first column of Q > # or may be computed if function f is supplied. > # > # OUTPUT: the table Q with P(x) = Q(N+1,N+1). > alg031 := proc() local TRUE, FALSE, OK, FLAG, N, I, XX, Q, A, NAME, INP, F, X, D, J, OUP; > printf(`This is Neville's Method.\n`); > OK := FALSE; > while OK = FALSE do > printf(`Choice of input method:\n`); > printf(`1. Input entry by entry from keyboard\n`); > printf(`2. Input data from a text file\n`); > printf(`3. Generate data using a function F\n`); > printf(`Choose 1, 2, or 3 please\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 1 or FLAG = 2 or FLAG = 3 then > OK := TRUE; > fi; > od; > if FLAG = 1 then > OK := FALSE; > while OK <> TRUE do > printf(`Input n\n`); > N := scanf(`%d`)[1]; > if N > 0 then > OK := TRUE; > for I from 0 to N do > printf(`Input X(%d) and F(X(%d)) `, I, I); > printf(`separated by a space\n`); > XX[I] := scanf(`%f`)[1]; > Q[I,0] := scanf(`%f`)[1]; > od; > else > printf(`Number must be a positive integer\n`); > fi; > od; > fi; > if FLAG = 2 then > printf(`Has a text file been created with the data in two columns ?\n`); > printf(`Enter Y or N\n`); > A := scanf(`\n%c`)[1]; > if A = "Y" or A = "y" then > printf(`Input the file name in the form - `); > printf(`drive:\\name.ext\n`); > printf(`For example: A:\\DATA.DTA\n`); > NAME := scanf(`%s`)[1]; > INP := fopen(NAME,READ,TEXT); > OK := FALSE; > while OK = FALSE do > printf(`Input N\n`); > N := scanf(`%d`)[1]; > if N > 0 then > for I from 0 to N do > XX[I] := fscanf(INP, `%f`)[1]; > Q[I,0] := fscanf(INP, `%f`)[1]; > od; > fclose(INP); > OK := TRUE; > else > printf(`Number must be a positive integer\n`); > fi; > od; > else > printf(`Please create the input file in two column `); > printf(`form with the X values and\n`); > printf(`F(X) values in the corresponding columns.\n`); > printf(`The program will end so the input file can `); > printf(`be created.\n`); > OK := FALSE; > fi; > fi; > if FLAG = 3 then > printf(`Input the function F(x) in terms of x\n`); > printf(`For example: cos(x)\n`); > F := scanf(`%a`)[1]; > F := unapply(F,x); > OK := FALSE; > while OK = FALSE do > printf(`Input n\n`); > N := scanf(`%d`)[1]; > if N > 0 then > for I from 0 to N do > printf(`Input X(%d)\n`, I); > XX[I] := scanf(`%f`)[1]; > Q[I,0] := F(XX[I]); > od; > OK := TRUE; > else > printf(`Number must be a positive integer\n`); > fi; > od; > fi; > if OK = TRUE then > printf(`Input the point at which the polynomial is to be evaluated\n`); > X := scanf(`%f`)[1]; > fi; > if OK = TRUE then > # Step 1 > D[0] := evalf(X-XX[0]); > for I from 1 to N do > D[I] := evalf(X-XX[I]); > for J from 1 to I do > Q[I,J] := evalf((D[I]*Q[I-1,J-1]-D[I-J]*Q[I,J-1])/(D[I]-D[I-J])); > od; > od; > # Step 2 > printf(`Select output destination\n`); > printf(`1. Screen\n`); > printf(`2. Text file\n`); > printf(`Enter 1 or 2\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 2 then > printf(`Input the file name in the form - drive:\\name.ext\n`); > printf(`For example: A:\\OUTPUT.DTA\n`); > NAME := scanf(`%s`)[1]; > OUP := fopen(NAME,WRITE,TEXT); > else > OUP := default; > fi; > fprintf(OUP, `NEVILLE'S METHOD\n`); > fprintf(OUP, `Table for P evaluated at X = %12.8f , follows: \n`, X); > fprintf(OUP, `Entries are XX(I), Q(I,0), ..., Q(I,I) `); > fprintf(OUP, `for each I = 0, ..., N where N = %3d\n\n`, N); > for I from 0 to N do > fprintf(OUP, `%11.8f `, XX[I]); > for J from 0 to I do > fprintf(OUP, `%11.8f `, Q[I,J]); > od; > fprintf(OUP, `\n`); > od; > if OUP <> default then > fclose(OUP); > printf(`Output file %s created successfully`,NAME); > fi; > fi; > RETURN(0); > end; > alg031();