restart:# HERMITE INTERPOLATION ALGORITHM 3.3## TO OBTAIN THE COEFFICIENTS OF THE HERMITE INTERPOLATING# POLYNOMIAL H ON THE (N+1) DISTINCT NUMBERS X(0), ..., X(N)# FOR THE FUNCTION F:## INPUT: NUMBERS X(0), X(1), ..., X(N): VALUES F(X(0)), # F(X(1)),..., F(X(N)) AND F'(X(0)), F'(X(1)), ..., # F'(X(N)).## OUTPUT: NUMBERS Q(0,0), Q(1,1), ..., Q(2N + 1,2N + 1) WHERE## H(X) = Q(0,0) + Q(1,1) * ( X - X(0) ) + Q(2,2) *# ( X - X(0) )**2 + Q(3,3) * ( X - X(0) )**2 *# ( X - X(1) ) + Q(4,4) * ( X - X(0) )**2 *# ( X - X(1) )**2 + ... + Q(2N + 1,2N + 1) *# ( X - X(0) )**2 * ( X - X(1) )**2 * ... *# ( X - X(N - 1) )**2 * (X - X(N) ).print(`This is Hermite interpolation.\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 the number of data points minus 1 `): N := scanf(`%d`)[1]:print(`n = `):print(N):if N > 0 thenOK := TRUE: for I1 from 0 to N do print(`Input X(i), F(X(i)), and F'(X(i)) separated by spaces for i = `):print(I1):X[I1] := scanf(`%f`)[1]:Q[2*I1,0] := scanf(`%f`)[1]:Q[2*I1+1,1] := scanf(`%f`)[1]:print(`Entries are `):print(X[I1],Q[2*I1,0],Q[2*I1+1,1]):od: elseprint(`Number must be a positive integer `): fi:od:fi:if FLAG = 2 thenprint(`Has a text file been created with the data in three 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:\134\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 the number of data points minus 1 `):N := scanf(`%d`)[1]:print(`n = `):print(N):if N > 0 thenfor I1 from 0 to N doX[I1] := fscanf(INP, `%f`)[1]:Q[2*I1,0] := fscanf(INP, `%f`)[1]:Q[2*I1+1,1] := fscanf(INP, `%f`)[1]:od:fclose(INP):OK := TRUE:else print(`Number must be a positive integer `):fi:od:elseprint(`Please create the input file in three column `):print(`form with the X values, F(X), 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: sin(x) `):F := scanf(`%a`)[1]:print(`F(x) = `):print(F):FP := diff(F,x):F := unapply(F,x):FP := unapply(FP,x):OK := FALSE:while OK = FALSE doprint(`Input the number of data points minus 1 `):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[2*I1,0] := F(X[I1]):Q[2*I1+1,1] := FP(X[I1]):od:OK := TRUE:elseprint(`Number must be a positive integer `):fi:od:fi:if OK = TRUE then# Step 1for I1 from 0 to N do# Step 2Z[2*I1] := X[I1]:Z[2*I1+1] := X[I1]:Q[2*I1+1,0] := Q[2*I1,0]:# Step 3if I1 <> 0 thenQ[2*I1,1] := (Q[2*I1,0]-Q[2*I1-1,0])/(Z[2*I1]-Z[2*I1-1]):fi:od:# Step 4K := 2*N+1:for I1 from 2 to K dofor J from 2 to I1 doQ[I1,J] := (Q[I1,J-1]-Q[I1-1,J-1])/(Z[I1]-Z[I1-J]):od:od:# Step 5print(`Choice of output method: `):print(`1. Output to screen `):print(`2. Output to text file `):print(`Please 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):else OUP := default:fi:fprintf(OUP, `HERMITE INTERPOLATING POLYNOMIAL\134n\134n`):fprintf(OUP, `The input data follows:\134n`):fprintf(OUP, ` X, F(X), F'(X)\134n`):for I1 from 0 to N dofprintf(OUP, ` %12.10e %12.10e %12.10e\134n`, X[I1], Q[2*I1,0], Q[2*I1+1,1]):od:fprintf(OUP, `\134nThe Coefficients of the Hermite Interpolation `):fprintf(OUP, `Polynomial\134n`):fprintf(OUP, `in order of increasing exponent follow:\134n\134n`):for I1 from 0 to K dofprintf(OUP, ` %12.10e\134n`, Q[I1,I1]):od:print(`Do you wish to evaluate this polynomial? `):print(`Enter Y or N `):A := scanf(` %c`)[1]:print(`Entry = `):print(A):if A = "Y" or A = "y" thenprint(`Enter a point at which to evaluate `):XX := scanf(`%f`)[1]:print(`Entry = `):print(XX):S := Q[K,K]*(XX-Z[K-1]):for I1 from 2 to K doJ := K-I1+1:S := (S+Q[J,J])*(XX-Z[J-1]):od:S := S + Q[0,0]:fprintf(OUP, `x-value and interpolated-value\134n`):fprintf(OUP, ` %12.10e %12.10e\134n`, XX, S):fi:if OUP <> default thenfclose(OUP):print(`Output file `, NAME, ` created successfully`):fi:fi:JSFH