restart:# NEWTON'S METHOD FOR SYSTEMS ALGORITHM 10.1## 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)): tolerance TOL: maximum number of# iterations N.## OUTPUT: Approximate solution X=(X(1),...,X(n)) or a message# that the number of iterations was exceeded.LINSYS := proc(N,OK,A,Y) local K, I1, Z, IR, IA, J, C, L, JA:K := N-1:OK := TRUE:I1 := 1:while OK = TRUE and I1 <= K doZ := abs(A[I1-1,I1-1]):IR := I1:IA := I1+1:for J from IA to N doif abs(A[J-1,I1-1]) > Z thenIR := J:Z := abs(A[J-1,I1-1]):fi:od:if Z <= 1.0e-20 thenOK := FALSE:elseif IR <> I1 thenfor J from I1 to N+1 doC := A[I1-1,J-1]:A[I1-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,I1-1]/A[I1-1,I1-1]:if abs(C) <= 1.0e-20 thenC := 0:fi:for L from I1 to N+1 doA[J-1,L-1] := A[J-1,L-1]-C*A[I1-1,L-1]:od:od:fi:I1 := I1+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 I1 from 1 to K doJ := N-I1: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 Newton Method for Nonlinear Systems.`):OK := FALSE:while OK = FALSE do print(`Input the number n of equations.`): N := scanf(`%d`)[1]: print(`n = `):print(N): if N >= 2 then OK := TRUE: else print(`n must be an integer greater than 1.`): fi:od:for I1 from 1 to N doprint(`Input the function f[i](x1,x2, ... xn) in terms of x1, x2, ... for i = `):print(I1):print(`For example: x1-x2^2+1`):F[I1] := scanf(`%a`)[1]:print(`function is `):print(F[I1]):od:for I1 from 1 to N dofor J from 1 to N doP[I1,J] := diff(F[I1],evaln(x || J)):print(`partial derivative i,j `):print(I1,J):print(P[I1,J]):P[I1,J] := unapply(P[I1,J],evaln(x || (1..N))):od:od:for I1 from 1 to N doF[I1] := unapply(F[I1],evaln(x || (1..N))):od:OK := FALSE:while OK = FALSE doprint(`Input the Tolerance.`):TOL := scanf(`%f`)[1]: print(`Tolerance = `): print(TOL):if TOL > 0 thenOK := TRUE:elseprint(`Tolerance must be positive.`):fi:od:OK := FALSE:while OK = FALSE doprint(`Input the maximum number of iterations.`):NN := scanf(`%d`)[1]: print(`Maximum number of iterations - `): print(NN):if NN > 0 thenOK := TRUE:elseprint(`Must be a positive integer.`):fi:od:for I1 from 1 to N doprint(`Input initial approximation X(I), I = `, I1):X[I1-1] := scanf(`%f`)[1]: print(`Input is `): print(X[I1-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(`Response 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:print(`Select amount of output\134n`):print(`1. Answer only\134n`):print(`2. All intermeditate approximations\134n`):print(`Enter 1 or 2\134n`):FLAG := scanf(`%d`)[1]: print(`Input is `):print(FLAG):fprintf(OUP, `NEWTONS METHOD FOR NONLINEAR SYSTEMS\134n\134n`):if FLAG = 2 thenfprintf(OUP, `Iteration, Approximation, Error\134n`):fi:# Step 1K := 1:# Step 2while OK = TRUE and K <= NN do# Step 3for I1 from 1 to N dofor J from 1 to N doA[I1-1,J-1] := evalf(P[I1,J](seq(X[i],i=0..N-1))):od:A[I1-1,N] := evalf(-F[I1](seq(X[i],i=0..N-1))):od:# Step 4LINSYS(N,OK,A,Y):if OK = TRUE then# Step 5R := 0:for I1 from 1 to N doif abs(Y[I1-1]) > R thenR := abs(Y[I1-1]):fi:X[I1-1] := X[I1-1]+Y[I1-1]:od:if FLAG = 2 thenfprintf(OUP, ` %2d`, K):for I1 from 1 to N dofprintf(OUP, ` %11.8f`, X[I1-1]):od:fprintf(OUP, `\134n%12.6e\134n`, R):fi:# Step 6if R < TOL thenOK := FALSE:fprintf(OUP, `Iteration %d gives solution:\134n\134n`, K):for I1 from 1 to N dofprintf(OUP, ` %11.8f`, X[I1-1]):od:fprintf(OUP, `\134n\134nto within tolerance %.10e\134n`, TOL):# Step 7elseK := K+1:fi:fi:od:if K > NN then# Step 8fprintf(OUP, `Procedure does not converge in %d iterations\134n`, NN):fi:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created sucessfully`):fi:fi:JSFH