restart:# NONLINEAR FINITE-DIFFERENCE ALGORITHM 11.4## To approximate the solution to the nonlinear boundary-value problem## Y'' = F(X,Y,Y'), A<=X<=B, Y(A) = ALPHA, Y(B) = BETA:## INPUT: Endpoints A,B: boundary conditions ALPHA, BETA:# integer N: tolerance TOL: maximum number of iterations M.## OUTPUT: Approximations W(I) TO Y(X(I)) for each I=0,1,...,N+1# or a message that the maximum number of iterations was# exceeded.print(`This is the Nonlinear Finite-Difference Method.`):print(`Input the function F(X,Y,Z) in terms of x, y, z.`):print(`For example: (32+2*x^3-y*z)/8`):F := scanf(`%a`)[1]: print(`F(x,y,z) = `): print(F):FY := diff(F,y):FYP := diff(F,z):F := unapply(F,x,y,z):FY := unapply(FY,x,y,z):FYP := unapply(FYP,x,y,z):OK := FALSE:while OK = FALSE doprint(`Input left and right endpoints separated by blank.`):AA := scanf(`%f`)[1]:BB := scanf(`%f`)[1]: print(`Left endpoint =`): print(AA): print(`Right endpoint =`): print(BB):if AA >= BB thenprint(`Left endpoint must be less than right endpoint.`):elseOK := TRUE:fi:od:print(`Input Y(a).`):ALPHA := scanf(`%f`)[1]: print(`y(a) =`): print(ALPHA): print(`Input Y(b).`):BETA := scanf(`%f`)[1]: print(`y(b) =`): print(BETA):OK := FALSE:while OK = FALSE doprint(`Input an integer > 1 for the number of`):print(`subintervals. Note that h := (b-a)/(n+1)`):N := scanf(`%d`)[1]: print(`Number of subintervals =`): print(N):if N <= 1 thenprint(`Number must exceed 1.`):elseOK := TRUE:fi:od:OK := FALSE:while OK = FALSE doprint(`Input Tolerance.`):TOL := scanf(`%f`)[1]: print(`Tolerance =`): print(TOL):if TOL <= 0 thenprint(`Tolerance must be positive.`):elseOK := TRUE:fi:od:OK := FALSE:while OK = FALSE doprint(`Input maximum number of iterations.`):NN := scanf(`%d`)[1]: print(`Maximum number of iterations =`): print(NN):if NN <= 0 thenprint(`Must be positive integer.`):elseOK := TRUE:fi:od:print(`Choice of output method:`):print(`1. Output to screen`):print(`2. Output to text File`):print(`Please 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\134name.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, `NONLINEAR FINITE-DIFFERENCE METHOD\134n\134n`):fprintf(OUP, ` I X(I) W(I)\134n`):# Step 1N1 := N-1:H := (BB-AA)/(N+1):# Step 2for I2 from 1 to N doW[I2-1] := ALPHA+I2*H*(BETA-ALPHA)/(BB-AA):od:# Step 3K := 1:# Step 4while K <= NN and OK = TRUE do# Step 5X := AA+H:T := (W[1]-ALPHA)/(2*H):A[0] := 2+H*H*FY(X,W[0],T):B[0] := -1+H*FYP(X,W[0],T)/2:D2[0] := -(2*W[0]-W[1]-ALPHA+H*H*F(X,W[0],T)):# Step 6for I2 from 2 to N1 doX := AA+I2*H:T := (W[I2]-W[I2-2])/(2*H):A[I2-1] := 2+H*H*FY(X,W[I2-1],T):B[I2-1] := -1+H*FYP(X,W[I2-1],T)/2:C[I2-1] := -1-H*FYP(X,W[I2-1],T)/2:D2[I2-1] := -(2*W[I2-1]-W[I2]-W[I2-2]+H*H*F(X,W[I2-1],T)):od:# Step 7X := BB - H:T := (BETA-W[N-2])/(2*H):A[N-1] := 2+H*H*FY(X,W[N-1],T):C[N-1] := -1-H*FYP(X,W[N-1],T)/2:D2[N-1] := -(2*W[N-1]-W[N-2]-BETA+H*H*F(X,W[N-1],T)):# Step 8# Steps 8 - 12 solve a tridiagonal linear system using Algorithm 6.7L[0] := A[0]:U[0] := B[0]/A[0]:Z[0] := D2[0]/L[0]:# Step 9for I2 from 2 to N1 doL[I2-1] := A[I2-1]-C[I2-1]*U[I2-2]:U[I2-1] := B[I2-1]/L[I2-1]:Z[I2-1] := (D2[I2-1]-C[I2-1]*Z[I2-2])/L[I2-1]:od:# Step 10L[N-1] := A[N-1]-C[N-1]*U[N-2]:Z[N-1] := (D2[N-1]-C[N-1]*Z[N-2])/L[N-1]:# Step 11V[N-1] := Z[N-1]:VMAX := abs(V[N-1]):W[N-1] := W[N-1]+V[N-1]:# Step 12for J from 1 to N1 doI2 := N-J:V[I2-1] := Z[I2-1]-U[I2-1]*V[I2]:W[I2-1] := W[I2-1]+V[I2-1]:if abs(V[I2-1]) > VMAX thenVMAX := abs(V[I2-1]):fi:od:# Step 13# Test for accuracyif VMAX <= TOL thenI2 := 0:fprintf(OUP, `%3d %13.8f %13.8f\134n`, I2, AA, ALPHA):# Step 14for I2 from 1 to N doX := AA+I2*H:fprintf(OUP, `%3d %13.8f %13.8f\134n`, I2, X, W[I2-1]):od:I2 := N+1:# Step 15fprintf(OUP, `%3d %13.8f %13.8f\134n`, I2, BB, BETA):fprintf(OUP, `Convergence in %d iterations\134n`, K):OK := FALSE:else# Step 16K := K+1:fi:od:# Step 17if K > NN thenfprintf(OUP, `No convergence in %d iterations\134n`, NN):fi:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:JSFH