restart: # CLAMPED CUBIC SPLINE ALGORITHM 3.5 # # To construct the cubic spline interpolant S for the function f, # defined at the numbers x(0) < x(1) < ... < x(n), satisfying # S'(x(0)) = f'(x(0)) and S'(x(n)) = f'(x(n)): # # INPUT: n: x(0), x(1), ..., x(n): either generate A(I) = f(x(I)) # for i = 0, 1, ..., n or input A(I) for I = 0, 1, ..., n: # FP0 = f'(x(0)): FPN = f'(x(n)). # # OUTPUT: A(J), B(J), C(J), D(J) for J = 0, 1, ..., n - 1. # # NOTE: S(x) = A(J) + B(J) * ( x - x(J) ) + C(J) * ( x - x(J) )**2 + # D(J) * ( x - x(J) )**3 for x(J) <= x < x(J + 1) print(`This is Clamped Cubic Spline 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 with nodes entered `): print(`from keyboard `): print(`4. Generate data using a function F with nodes from `): print(`a text file `): print(`Choose 1, 2, 3, or 4 please `): FLAG := scanf(`%d`)[1]:print(`Entry = `):print(FLAG): if FLAG >= 1 and FLAG <= 4 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 a space `): X[I1] := scanf(`%f`)[1]: A[I1] := scanf(`%f`)[1]:print(`Entries are `):print(X[I1],A[I1]): 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 `): AA := scanf(` %c`)[1]:print(`Entry = `):print(AA): if AA = "Y" or AA = "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 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]: A[I1] := 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 `): print(`X values and F(X) values in the corresponding columns. `): print(`The program will end so the input file can 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]:print(`Entry = `):print(X[I1]): A[I1] := F(X[I1]): od: OK := TRUE: else print(`Number must be a positive integer `): fi: od: fi: if FLAG = 4 then print(`Has the text file with X-values been created? `): print(`Enter Y or N `): AA := scanf(` %c`)[1]:print(`Entry = `):print(AA): if AA = "Y" or AA = "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): 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 OK := TRUE: for I1 from 0 to N do X[I1] := fscanf(INP, `%f`)[1]: A[I1] := F(X[I1]): od: fclose(INP): else print(`Number must be a positive integer `): fi: od: else print(`The program will end so the input file can be created`): OK := FALSE: fi: fi: if OK = TRUE then print(`Enter F'(X(0)) and F'(X(N)) separated by a space `): FP0 := scanf(`%f`)[1]: FPN := scanf(`%f`)[1]:print(`Entries are `):print(FP0,FPN): fi: if OK = TRUE then M := N - 1: # Step 1 for I1 from 0 to M do H[I1] := X[I1+1] - X[I1]: od: # Step 2 # use XA instead of ALPHA XA[0] := 3.0 * (A[1] - A[0]) / H[0] - 3.0 * FP0: XA[N] := 3.0 * FPN - 3.0 * (A[N] - A[N-1]) / H[N-1]: # Step 3 for I1 from 1 to M do XA[I1] := 3.0*(A[I1+1]*H[I1-1]-A[I1]*(X[I1+1]-X[I1-1])+A[I1-1]*H[I1])/(H[I1]*H[I1-1]): od: # Step 4 # STEPS 4, 5, 6 and part of 7 solve the tridiagonal system using # Algorithm 6.7. # use XL, XU, XZ in place of L, MU, Z resp. XL[0] := 2.0 * H[0]: XU[0] := 0.5: XZ[0] := XA[0] / XL[0]: # Step 5 for I1 from 1 to M do XL[I1] := 2.0 * (X[I1+1] - X[I1-1]) - H[I1-1] * XU[I1-1]: XU[I1] := H[I1] / XL[I1]: XZ[I1] := (XA[I1] - H[I1-1] * XZ[I1-1]) / XL[I1]: od: # Step 6 XL[N] := H[N-1] * (2.0 - XU[N-1]): XZ[N] := (XA[N] - H[N-1] * XZ[N-1]) / XL[N]: C[N] := XZ[N]: # Step 7 for I1 from 1 to N do J := N - I1: C[J] := XZ[J] - XU[J] * C[J+1]: B[J] := (A[J+1] - A[J]) / H[J] - H[J] * (C[J+1] + 2.0 * C[J]) / 3.0: D1[J] := (C[J+1] - C[J]) / (3.0 * H[J]): od: # Step 8 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, `CLAMPED CUBIC SPLINE INTERPOLATION\134n\134n`): fprintf(OUP, `The numbers X(0), ..., X(N) are:\134n`): for I1 from 0 to N do fprintf(OUP, ` %11.8f`, X[I1]): od: fprintf(OUP, `\134n\134nThe coefficients of the spline on the subintervals `): fprintf(OUP, `are:\134n`): fprintf(OUP, `for I = 0, ..., N-1\134n`): fprintf(OUP, ` A(I) B(I) C(I) D(I)\134n`): for I1 from 0 to M do fprintf(OUP, ` %13.8f %13.8f %13.8f %13.8f \134n`, A[I1], B[I1], C[I1], D1[I1]): od: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: SU1UaGlzfmlzfkNsYW1wZWR+Q3ViaWN+U3BsaW5lfkludGVycG9sYXRpb24ufCtHNiI= STlDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kOn5HNiI= SUcxLn5JbnB1dH5lbnRyeX5ieX5lbnRyeX5mcm9tfmtleWJvYXJkfkc2Ig== SUAyLn5JbnB1dH5kYXRhfmZyb21+YX50ZXh0fmZpbGV+RzYi SVgzLn5HZW5lcmF0ZX5kYXRhfnVzaW5nfmF+ZnVuY3Rpb25+Rn53aXRofm5vZGVzfmVudGVyZWR+RzYi SS9mcm9tfmtleWJvYXJkfkc2Ig== SVU0Ln5HZW5lcmF0ZX5kYXRhfnVzaW5nfmF+ZnVuY3Rpb25+Rn53aXRofm5vZGVzfmZyb21+RzYi SS1hfnRleHR+ZmlsZX5HNiI= ST1DaG9vc2V+MSx+Mix+Myx+b3J+NH5wbGVhc2V+RzYi SSlFbnRyeX49fkc2Ig== IiIi SSlJbnB1dH5ufkc2Ig== SSVufj1+RzYi IiIk SUBJbnB1dH5YKGkpfmFuZH5GKFgoaSkpfmZvcn5pfj1+RzYi IiIh STZzZXBhcmF0ZWR+Ynl+YX5zcGFjZX5HNiI= SS1FbnRyaWVzfmFyZX5HNiI= NiQkIiIhRiQkIiM1ISIi SUBJbnB1dH5YKGkpfmFuZH5GKFgoaSkpfmZvcn5pfj1+RzYi IiIi STZzZXBhcmF0ZWR+Ynl+YX5zcGFjZX5HNiI= SS1FbnRyaWVzfmFyZX5HNiI= NiQkIiNEISIjJCIqRkAoWzshIik= SUBJbnB1dH5YKGkpfmFuZH5GKFgoaSkpfmZvcn5pfj1+RzYi IiIj STZzZXBhcmF0ZWR+Ynl+YX5zcGFjZX5HNiI= SS1FbnRyaWVzfmFyZX5HNiI= NiQkIiImISIiJCIqJD1HPUYhIik= SUBJbnB1dH5YKGkpfmFuZH5GKFgoaSkpfmZvcn5pfj1+RzYi IiIk STZzZXBhcmF0ZWR+Ynl+YX5zcGFjZX5HNiI= SS1FbnRyaWVzfmFyZX5HNiI= NiQkIiN2ISIjJCIqMipvIlslISIp SVJFbnRlcn5GJyhYKDApKX5hbmR+RicoWChOKSl+c2VwYXJhdGVkfmJ5fmF+c3BhY2V+RzYi SS1FbnRyaWVzfmFyZX5HNiI= NiQkIiIjIiIhJCIrPzcieVoiISIp STtTZWxlY3R+b3V0cHV0fmRlc3RpbmF0aW9ufkc2Ig== SSsxLn5TY3JlZW5+RzYi SS4yLn5UZXh0fmZpbGV+RzYi SS5FbnRlcn4xfm9yfjJ+RzYi SSlFbnRyeX49fkc2Ig== IiIi CLAMPED CUBIC SPLINE INTERPOLATION The numbers X(0), ..., X(N) are: 0.00000000 0.25000000 0.50000000 0.75000000 The coefficients of the spline on the subintervals are: for I = 0, ..., N-1 A(I) B(I) C(I) D(I) 1.00000000 2.00000000 0.40261391 7.90770562 1.64872127 3.68400176 6.33339313 -15.82572487 2.71828183 3.88337491 -5.53590052 72.86766693 JSFH