restart:# PADE' RATIONAL APPROXIMATION ALGORITHM 8.1## To obtain the rational approximation## r(x) = p(x) / q(x)# = (p0 + p1*x + ... + pn*x^n) / (q0 + q1*x + ... + qm*x^m)## for a given function f(x):## INPUT nonnegative integers m and n.## OUTPUT coefficients q0, q1, ... , qm, p0, p1, ... , pn.## The coefficients of the Maclaurin polynomial a0, a1, ... , aN could# be calculated instead of input as is assumed in this program.print(`This is Pade Approximation.`):OK := FALSE:while OK = FALSE doprint(`Input m and n separated by blanks`):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:elseprint(`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:print(`The MacLaurin coefficients a[0], a[1], ... , a[N]`):print(`are to be input.`):print(`Choice of input method:`):print(`1. Input entry by entry from keyboard`):print(`2. Input data from a text file`):print(`Choose 1 or 2 please`):FLAG := scanf(`%d`)[1]:print(`Your input is `):print(FLAG):if FLAG = 1 thenprint(`Input in order a[0] to a[N] `):for I1 from 0 to BN doprint(`Input coefficient a[I] for I = `):print(I1):AA[I1] := scanf(`%f`)[1]:print(`Your input is `):print(AA[I1]):od:OK := TRUE:elseprint(`As many entries as desired can be placed`):print(`on each line of the file each separated by blank.`):print(`Has the input file been created? - enter 1 for yes or 2 for no.`):ANS := scanf(`%d`)[1]: print(`Your response is`): print(ANS):if ANS = 1 thenprint(`Input the file name in the form - `):print(`drive:\134\134name.ext`):print(`for example: A:\134\134DATA.DTA`):NAME := scanf(`%s`)[1]:INP := fopen(NAME,READ,TEXT):for I1 from 0 to BN doAA[I1] := fscanf(INP, `%f`)[1]:od:fclose(INP):OK := TRUE:elseprint(`The program will end so the input file can `):print(`be created.`):OK := FALSE:fi:fi:if OK = TRUE thenOUP := default:fprintf(OUP,`Coefficients a[I] for I = 0, 1, 2, ... are`):for I1 from 0 to BN dofprintf(OUP, ` %11.8f`, AA[I1]):od:fprintf(OUP,`\134n\134n`):# Step 1N := BN:M := N+1:# Step 2 - performed during inputfor I3 from 1 to N doNROW[I3-1] := I3:od:# Initialize row pointer for linear systemNN := N-1:# Step 3Q[0] := 1:P[0] := AA[0]:# Step 4# Set up a linear system but use A[i,j] in place of B[i,j]for I3 from 1 to N do# Step 5for J from 1 to I3-1 doif J <= LN thenA[I3-1,J-1] := 0:fi:od:# Step 6if I3 <= LN thenA[I3-1,I3-1] := 1:fi:# Step 7for J from I3+1 to N doA[I3-1,J-1] := 0:od:# Step 8for J from 1 to I3 doif J <= LM thenA[I3-1,LN+J-1] := -AA[I3-J]:fi:od:# Step 9for J from LN+I3+1 to N doA[I3-1,J-1] := 0:od:# Step 10A[I3-1,N] := AA[I3]:od:fprintf(OUP,`The Linear System is: \134n`):for I1 from 1 to N dofor J1 from 1 to N+1 dofprintf(OUP, ` %11.8f`, A[I1-1,J1-1]):od:fprintf(OUP,`\134n`):od:fprintf(OUP,`\134n`):# Solve the linear system using partial pivoting.I3 := LN+1:# Step 11while OK = TRUE and I3 <= NN do# Step 12IMAX := NROW[I3-1]:AMAX := abs(A[IMAX-1,I3-1]):IMAX := I3:JJ := I3+1:for IP from JJ to N doJP := NROW[IP-1]:if abs(A[JP-1,I3-1]) > AMAX thenAMAX := abs(A[JP-1,I3-1]):IMAX := IP:fi:od:# Step 13if AMAX <= 1.0e-20 thenOK := false:else# Step 14# Simulate row interchangeif NROW[I3-1] <> NROW[IMAX-1] thenNCOPY := NROW[I3-1]:NROW[I3-1] := NROW[IMAX-1]:NROW[IMAX-1] := NCOPY:fi:I1 := NROW[I3-1]:# Step 15# Perform eliminationfor J from JJ to M-1 doJ1 := NROW[J-1]:# Step 16XM := A[J1-1,I3-1]/A[I1-1,I3-1]:# Step 17for K from JJ to M doA[J1-1,K-1] := A[J1-1,K-1]-XM * A[I1-1,K-1]:od:# Step 18A[J1-1,I3-1] := 0:od:fi:I3 := I3+1:od:fprintf(OUP,`The Reduced Linear System is: \134n`):for I4 from 1 to N dofor J1 from 1 to N+1 dofprintf(OUP, ` %11.8f`, A[I4-1,J1-1]):od:fprintf(OUP,`\134n`):od:fprintf(OUP,`\134n`):if OK = TRUE then# Step 19N1 := NROW[N-1]:if abs(A[N1-1,N-1]) <= 1.0e-20 thenOK := FALSE:# System has no unique solutionelse# Step 20# Start backward substitutionif LM > 0 thenQ[LM] := A[N1-1,M-1]/A[N1-1,N-1]:A[N1-1,M-1] := Q[LM]:fi:PP := 1:# Step 21for K from LN+1 to NN doI3 := NN-K+LN+1:JJ := I3+1:N2 := NROW[I3-1]:SUM := A[N2-1,N]:for KK from JJ to N doLL := NROW[KK-1]:SUM := SUM-A[N2-1,KK-1]*A[LL-1,M-1]:od:# Step 22A[N2-1,M-1] := SUM/A[N2-1,I3-1]:Q[LM-PP] := A[N2-1,M-1]:PP := PP+1:od:for K from 1 to LN doI3 := LN-K+1:N2 := NROW[I3-1]:SUM := A[N2-1,N]:for KK from LN+1 to N doLL := NROW[KK-1]:SUM := SUM-A[N2-1,KK-1]*A[LL-1,M-1]:od:# Step 23# Procedure is completed.A[N2-1,M-1] := SUM:P[LN-K+1] := A[N2-1,M-1]:od:print(`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(`Your 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, `PADE' RATIONAL APPROXIMATION\134n\134n`):fprintf(OUP, `Denominator Coefficients Q[0], ..., Q[M] \134n`):for I1 from 0 to LM dofprintf(OUP, ` %11.8f`, Q[I1]):od:fprintf(OUP, `\134n`):fprintf(OUP, `Numerator Coefficients P[0], ..., P[N]\134n`):for I1 from 0 to LN dofprintf(OUP, ` %11.8f`, P[I1]):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