restart: # NATURAL CUBIC SPLINE ALGORITHM 3.4 # # 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)) = S''(x(n)) = 0: # # 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. # # 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 the natural 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 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 `): print(`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 M := N - 1: # Step 1 for I1 from 0 to M do H[I1] := X[I1+1] - X[I1]: od: # Step 2 # Use XA in place of ALPHA 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 3 # Steps 3, 4, 5 and part of 6 solve the tridiagonal system using # Algorithm 6.7 # Use XL, XU, XZ in place of L, Mu, Z resp. XL[0] := 1: XU[0] := 0: XZ[0] := 0: # Step 4 for I1 from 1 to M do XL[I1] := 2*(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 5 XL[N] := 1: XZ[N] := 0: C[N] := XZ[N]: # Step 6 for I1 from 0 to M do J := M-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 7 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, `NATURAL 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: SVFUaGlzfmlzfnRoZX5uYXR1cmFsfmN1YmljfnNwbGluZX5pbnRlcnBvbGF0aW9uLnwrRzYi STlDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kOn5HNiI= SUcxLn5Jb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i SS5FbnRlcn4xfm9yfjJ+RzYi SSlFbnRyeX49fkc2Ig== IiIi NATURAL 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.33101562 0.00000000 4.22191137 1.64872127 3.12262400 3.16643353 5.82415770 2.71828183 5.79787033 7.53455180 -10.04606907 JSFH