restart:# CHEBYSHEV RATIONAL APPROXIMATION ALGORITHM 8.2## To obtain the rational approximation## rT(x) = (p0*T0 + p1*T1 +...+ pn*Tn) / (q0*T0 + q1*T1 +...+ qm*Tm)## for a given function f(x):## INPUT nonnegative integers m and n.## OUTPUT coefficients q0, q1, ... , qm, p0, p1, ... , pn.## The coefficients of the Chebyshev expansion a0, a1, ..., aN could# be calculated instead of input as is assumed in this program.print(`This is Chebyshev Approximation.\134n\134n`):OK := FALSE:while OK = FALSE doprint(`Input m and n separated by a space\134n`):LM := scanf(`%d`)[1]:LN := scanf(`%d`)[1]:print(`m = `):print(LM):print(`n = `):print(LN):BN := LM+LN:if LM >= 0 and LN >= 0 thenOK := TRUE:else print(`m and n must both be nonnegative.\134n`):fi:if LM = 0 and LN = 0 thenOK := FALSE:print(`Not both m and n can be zero\134n`):fi:od:OK := FALSE:while OK = FALSE doprint(`The Chebyshev coefficients a[0], a[1], ... , a[N+m]\134n`):print(`are to be input.\134n`):print(`Choice of input method:\134n`):print(`1. Input entry by entry from keyboard\134n`):print(`2. Input data from a text file\134n`):print(`Choose 1 or 2 please\134n`):FLAG := scanf(`%d`)[1]:print(`Input is `):print(FLAG):if FLAG = 1 or FLAG = 2 thenOK := TRUE:fi:od:if FLAG = 1 thenprint(`Input in order a[0] to a[N+m]\134n`):for I2 from 0 to BN+LM doprint(`Input A[I] for I = `):print(I2):AA[I2] := scanf(`%f`)[1]:print(`a[I] = `):print(AA[I2]):od:fi:if FLAG = 2 thenprint(`The text file may contain as many entries\134n`):print(`per line as desired each separated by blank.\134n`):print(`Has such a text file been created?\134n`):print(`Enter 1 for yes or 2 for no`):ANS := scanf(`%d`)[1]:PRINT(`Input is `):print(ANS):if ANS = 1 thenprint(`Input the file name in the form - `):print(`drive:\134\134name.ext\134n`):print(`for example: A:\134\134DATA.DTA\134n`):NAME := scanf(`%s`)[1]:INP := fopen(NAME,READ,TEXT):for I2 from 0 to BN+LM doAA[I2] := fscanf(INP, `%f`)[1]:od:fclose(INP):elseprint(`The program will end so the input file can `):print(`be created.\134n`):OK := FALSE:fi:fi:if OK = TRUE thenOUP := default:fprintf(OUP,`Coefficients a[I] for I = 0, 1, 2, ... are`):for I2 from 0 to BN dofprintf(OUP, ` %11.8f`, AA[I2]):od:fprintf(OUP,`\134n\134n`):# Step 1N := BN:M := N+1:# Step 2 - performed during inputfor I2 from 0 to N doNROW[I2] := I2:od:# Initialize row pointerNN := N-1:# Step 3Q[0] := 1.0:# Step 4# Set up a linear system with A used in place of Bfor I2 from 0 to N do# Step 5for J from 0 to I2 doif J <= LN thenA[I2,J] := 0:fi:od:# Step 6if I2 <= LN thenA[I2,I2] := 1.0:fi:# Step 7for J from I2+1 to LN doA[I2,J] := 0:od:# Step 8for J from LN+1 to N doif I2 <> 0 thenPP := I2-J+LN:if PP < 0 thenPP := -PP:fi:A[I2,J] := -(AA[I2+J-LN]+AA[PP])/2.0:elseA[I2,J] := -AA[J-LN]/2.0:fi:od:A[I2,N+1] := AA[I2]:od:# Step 9A[0,N+1] := A[0,N+1]/2.0:fprintf(OUP,`The Linear System is: \134n`):for I1 from 1 to N+1 dofor J1 from 1 to N+2 dofprintf(OUP, ` %11.8f`, A[I1-1,J1-1]):od:fprintf(OUP,`\134n`):od:fprintf(OUP,`\134n`):# Steps 10 - 21 solve the linear system using partial pivotingI2 := LN+1:# Step 10while OK = TRUE and I2 <= NN do# Step 11IMAX := NROW[I2]:AMAX := abs(A[IMAX,I2]):IMAX := I2:JJ := I2+1:for IP from JJ to N doJP := NROW[IP]:if abs(A[JP,I2]) > AMAX thenAMAX := abs(A[JP,I2]):IMAX := IP:fi:od:# Step 12if AMAX <= 1.0e-20 thenOK := false:else# Step 13# Simulate row interchangeif NROW[I2] <> NROW[IMAX] thenNCOPY := NROW[I2]:NROW[I2] := NROW[IMAX]:NROW[IMAX] := NCOPY:fi:I1 := NROW[I2]:# Step 14# Perform eliminationfor J from JJ to M-1 doJ1 := NROW[J]:# Step 15XM := A[J1,I2]/A[I1,I2]:# Step 16for K from JJ to M doA[J1,K] := A[J1,K]-XM*A[I1,K]:od:# Step 17A[J1,I2] := 0:od:fi:I2 := I2+1:od:if OK = TRUE then# Step 18N1 := NROW[N]:if abs(A[N1,N]) <= 1.0e-20 thenOK := false:# System has no unique solutionelse# Step 19fprintf(OUP,`The Reduced Linear System is: \134n`):for I1 from 1 to N+1 dofor J1 from 1 to N+2 dofprintf(OUP, ` %11.8f`, A[I1-1,J1-1]):od:fprintf(OUP,`\134n`):od:fprintf(OUP,`\134n`):# Start backward substitutionif LM > 0 thenQ[LM] := A[N1,M]/A[N1,N]:A[N1,M] := Q[LM]:fi:PP := 1:# Step 20for K from LN+1 to NN doI2 := NN-K+LN+1:JJ := I2+1:N2 := NROW[I2]:SUM := A[N2,N+1]:for KK from JJ to N doLL := NROW[KK]:SUM := SUM - A[N2,KK] * A[LL,M]:od:A[N2,M] := SUM / A[N2,I2]:Q[LM-PP] := A[N2,M]:PP := PP+1:od:# Step 21for K from 0 to LN doI2 := LN-K:N2 := NROW[I2]:SUM := A[N2,N+1]:for KK from LN+1 to N doLL := NROW[KK]:SUM := SUM-A[N2,KK]*A[LL,M]:od:A[N2,M] := SUM:P[LN-K] := A[N2,M]:od:# Step 22# Procedure completedprint(`Choice of output method:\134n`):print(`1. Output to screen\134n`):print(`2. Output to text file\134n`):print(`Enter 1 or 2\134n`):FLAG := scanf(`%d`)[1]:print(`Input is`):print(FLAG):if FLAG = 2 thenprint(`Input the file name in the form - drive:\134\134name.ext\134n`):print(`for example: A:\134\134OUTPUT.DTA\134n`):NAME := scanf(`%s`)[1]:print(`Output file is `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `CHEBYSHEV RATIONAL APPROXIMATION\134n\134n`):fprintf(OUP, `Denominator Coefficients Q[0], ..., Q[M] \134n`):for I2 from 0 to LM dofprintf(OUP, ` %11.8f`, Q[I2]):od:fprintf(OUP, `\134n`):fprintf(OUP, `Numerator Coefficients P[0], ..., P[N]\134n`):for I2 from 0 to LN dofprintf(OUP, ` %11.8f`, P[I2]):od:fprintf(OUP, `\134n`):if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:fi:fi:if OK = FALSE thenprint(`System has no unique solution\134n`):fi:fi:JSFH