restart:# CONTINUATION METHOD FOR SYSTEMS ALGORITHM 104## To approximate the solution of the nonlinear system F(X)=0 given# an initial approximation X:## INPUT: Number n of equations and unknowns: initial approximation# X=(X(1),...,X(n)): number of Runge-Kutta 4 iterations N.## OUTPUT: Approximate solution X=(X(1),...,X(n)). # LINSYS := proc(N,OK,A,Y) local K, I2, Z, IR, IA, J, C, L, JA:K := N-1:OK := TRUE:I2 := 1:while OK = TRUE and I2 <= K doZ := abs(A[I2-1,I2-1]):IR := I2:IA := I2+1:for J from IA to N doif abs(A[J-1,I2-1]) > Z thenIR := J:Z := abs(A[J-1,I2-1]):fi:od:if Z <= 1.0e-20 thenOK := FALSE:elseif IR <> I2 thenfor J from I2 to N+1 doC := A[I2-1,J-1]:A[I2-1,J-1] := A[IR-1,J-1]:A[IR-1,J-1] := C:od:fi:for J from IA to N doC :=A[J-1,I2-1]/A[I2-1,I2-1]:if abs(C) <= 1.0e-20 thenC := 0:fi:for L from I2 to N+1 doA[J-1,L-1] := A[J-1,L-1]-C*A[I2-1,L-1]:od:od:fi:I2 := I2+1:od:if OK = TRUEthen if abs(A[N-1,N-1]) <= 1.0e-20 thenOK := FALSE:elseY[N-1] := A[N-1,N]/A[N-1,N-1]:for I2 from 1 to K doJ := N-I2:JA := J+1:C := A[J-1,N]:for L from JA to N doC := C-A[J-1,L-1]*Y[L-1]:od:Y[J-1] := C/A[J-1,J-1]:od:fi:fi:if OK = FALSE thenprint(`Linear system is singular`):fi:end:print(`This is the Continuation Method for Nonlinear Systems.`):OK := FALSE:while OK = FALSE doprint(`Input the number n of equations.`):N := scanf(`%d`)[1]: print(`n = `):print(N):if N >= 2 thenOK := TRUE:elseprint(`n must be an integer greater than 1.\134n`):fi:od:for I2 from 1 to N doprint(`Input the function F_I in terms of x1 ... xn for I = `,I2):F[I2] := scanf(`%a`)[1]: print(`Function is `): print(F[I2]):od:for I2 from 1 to N dofor J from 1 to N doP[I2,J] := diff(F[I2],evaln(x || J)):P[I2,J] := unapply(P[I2,J],evaln(x || (1..N))):od:od:for I2 from 1 to N doF[I2] := unapply(F[I2],evaln(x || (1..N))):od:OK := FALSE:while OK = FALSE doprint(`Input the number N for RK4.`):NN := scanf(`%d`)[1]: print(`N = `):print(NN):if NN > 0 thenOK := TRUE:elseprint(`Must be a positive integer.`):fi:od:for I2 from 1 to N doprint(`Input initial approximation X(I) for I = `, I2):X[I2-1] := scanf(`%f`)[1]: print(`Input is `): print(X[I2-1]):od:if OK = TRUE thenprint(`Select output destination`):print(`1. Screen`):print(`2. Text file`):print(`Enter 1 or 2`):FLAG := scanf(`%d`)[1]: print(`Input is `):print(FLAG):if FLAG = 2 thenprint(`Input the file name in the form - drive\134\134:name.ext`):print(`for example A:\134\134OUTPUT.DTA`):NAME := scanf(`%s`)[1]: print(`Output file is `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP,`This is the Continuation Method using the Runge Kutta Method of Order 4 \134n\134n`):fprintf(OUP,`N = %2d \134n\134n`,NN):fprintf(OUP,`Iter# Approximation \134n\134n`):K:=0:fprintf(OUP, ` %2d`, K):for I2 from 1 to N dofprintf(OUP, ` %11.8f `, X[I2-1]):od:fprintf(OUP,` \134n`):# Step 1H := 1/NN:for I2 from 1 to N dob[I2-1] := H*evalf(-F[I2](seq(X[i],i=0..N-1))):od:# Step 2for K from 1 to NN do# Steps 3 - 6for I2 from 1 to N dofor J from 1 to N doA[I2-1,J-1] := evalf(P[I2,J](seq(X[i],i=0..N-1))):od:od:for I2 from 1 to N doA[I2-1,N] := b[I2-1]:od:LINSYS(N,OK,A,Y):if OK = FALSE thenbreak:fi:for I2 from 1 to N doK1[I2-1] := Y[I2-1]:X1[I2-1] := X[I2-1]+0.5*K1[I2-1]:od:for I2 from 1 to N dofor J from 1 to N doA[I2-1,J-1] := evalf(P[I2,J](seq(X1[i],i=0..N-1))):od:od:for I2 from 1 to N doA[I2-1,N] := b[I2-1]:od:LINSYS(N,OK,A,Y):if OK = FALSE thenbreak:fi:for I2 from 1 to N doK2[I2-1] := Y[I2-1]:X2[I2-1] := X[I2-1]+0.5*K2[I2-1]:od:for I2 from 1 to N dofor J from 1 to N doA[I2-1,J-1] := evalf(P[I2,J](seq(X2[i],i=0..N-1))):od:od:for I2 from 1 to N doA[I2-1,N] := b[I2-1]:od:LINSYS(N,OK,A,Y):if OK = FALSE thenbreak:fi:for I2 from 1 to N doK3[I2-1] := Y[I2-1]:X3[I2-1] := X[I2-1]+K3[I2-1]:od:for I2 from 1 to N dofor J from 1 to N doA[I2-1,J-1] := evalf(P[I2,J](seq(X3[i],i=0..N-1))):od:od:for I2 from 1 to N doA[I2-1,N] := b[I2-1]:od:LINSYS(N,OK,A,Y):if OK = FALSE thenbreak:fi:# Step 7for I2 from 1 to N doK4[I2-1] := Y[I2-1]:X4[I2-1] := X[I2-1]+(K1[I2-1]+2*K2[I2-1]+2*K3[I2-1]+K4[I2-1])/6:X[I2-1] := X4[I2-1]:od:fprintf(OUP, ` %2d`, K):for I2 from 1 to N dofprintf(OUP, ` %11.8f `, X[I2-1]):od:fprintf(OUP,` \134n`):od:# Step 8if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created sucessfully`):fi:fi:JSFH