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 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 the number of data points minus 1 `): 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), 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: 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 three 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:\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 do print(`Input the number of data points minus 1 `): 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[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: else print(`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 then print(`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 do print(`Input the number of data points minus 1 `): 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[2*I1,0] := F(X[I1]): Q[2*I1+1,1] := FP(X[I1]): od: OK := TRUE: else print(`Number must be a positive integer `): fi: od: fi: if OK = TRUE then # Step 1 for I1 from 0 to N do # Step 2 Z[2*I1] := X[I1]: Z[2*I1+1] := X[I1]: Q[2*I1+1,0] := Q[2*I1,0]: # Step 3 if I1 <> 0 then Q[2*I1,1] := (Q[2*I1,0]-Q[2*I1-1,0])/(Z[2*I1]-Z[2*I1-1]): fi: od: # Step 4 K := 2*N+1: for I1 from 2 to K do for J from 2 to I1 do Q[I1,J] := (Q[I1,J-1]-Q[I1-1,J-1])/(Z[I1]-Z[I1-J]): od: od: # Step 5 print(`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 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, `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 do fprintf(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 do fprintf(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" then print(`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 do J := 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 then fclose(OUP): print(`Output file `, NAME, ` created successfully`): fi: fi: SUBUaGlzfmlzfkhlcm1pdGV+aW50ZXJwb2xhdGlvbi58K0c2Ig== STlDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kOn5HNiI= SUcxLn5JbnB1dH5lbnRyeX5ieX5lbnRyeX5mcm9tfmtleWJvYXJkfkc2Ig== SUAyLn5JbnB1dH5kYXRhfmZyb21+YX50ZXh0fmZpbGV+RzYi SUUzLn5HZW5lcmF0ZX5kYXRhfnVzaW5nfmF+ZnVuY3Rpb25+Rn5HNiI= STpDaG9vc2V+MSx+Mix+b3J+M35wbGVhc2V+RzYi SSlFbnRyeX49fkc2Ig== IiIi SUlJbnB1dH50aGV+bnVtYmVyfm9mfmRhdGF+cG9pbnRzfm1pbnVzfjF+RzYi SSVufj1+RzYi IiIj SWluSW5wdXR+WChpKSx+RihYKGkpKSx+YW5kfkYnKFgoaSkpfnNlcGFyYXRlZH5ieX5zcGFjZXN+Zm9yfml+PX5HNiI= IiIh SS1FbnRyaWVzfmFyZX5HNiI= NiUkIiM4ISIiJCInJzM/JyEiJyQhKEstQSYhIig= SWluSW5wdXR+WChpKSx+RihYKGkpKSx+YW5kfkYnKFgoaSkpfnNlcGFyYXRlZH5ieX5zcGFjZXN+Zm9yfml+PX5HNiI= IiIi SS1FbnRyaWVzfmFyZX5HNiI= NiUkIiM7ISIiJCIoQVNiJSEiKCQhKGYqKXAmRig= SWluSW5wdXR+WChpKSx+RihYKGkpKSx+YW5kfkYnKFgoaSkpfnNlcGFyYXRlZH5ieX5zcGFjZXN+Zm9yfml+PX5HNiI= IiIj SS1FbnRyaWVzfmFyZX5HNiI= NiUkIiM+ISIiJCIoJz09RyEiKCQhKHI6ImVGKA== STpDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDp+RzYi STUxLn5PdXRwdXR+dG9+c2NyZWVufkc2Ig== STgyLn5PdXRwdXR+dG9+dGV4dH5maWxlfkc2Ig== STVQbGVhc2V+ZW50ZXJ+MX5vcn4yfkc2Ig== SSlFbnRyeX49fkc2Ig== IiIi HERMITE INTERPOLATING POLYNOMIAL The input data follows: X, F(X), F'(X) 1.3000000000e+00 6.2008600000e-01 -5.2202320000e-01 1.6000000000e+00 4.5540220000e-01 -5.6989590000e-01 1.9000000000e+00 2.8181860000e-01 -5.8115710000e-01 The Coefficients of the Hermite Interpolation Polynomial in order of increasing exponent follow: 6.2008600000e-01 -5.2202320000e-01 -8.9742666670e-02 6.6365555570e-02 2.6666666330e-03 -2.7746912770e-03 SUpEb355b3V+d2lzaH50b35ldmFsdWF0ZX50aGlzfnBvbHlub21pYWw/fkc2Ig== SS5FbnRlcn5Zfm9yfk5+RzYi SSlFbnRyeX49fkc2Ig== USJ5NiI= SURFbnRlcn5hfnBvaW50fmF0fndoaWNofnRvfmV2YWx1YXRlfkc2Ig== SSlFbnRyeX49fkc2Ig== JCIjOiEiIg== x-value and interpolated-value 1.5000000000e+00 5.1182770170e-01 JSFH