restart:# NONLINEAR SHOOTING ALGORITHM 11.2## To approximate the solution of 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: number of# subintervals N: tolerance TOL: maximum number of iterations M.## OUTPUT: Approximations W(1,I) TO Y(X(I)): W(2,I) TO Y'(X(I))# for each I=0,1,...,N or a message that the maximum# number of iterations was exceeded.print(`This is the Nonlinear Shooting 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.`):A := scanf(`%e`)[1]: print(`Left endpoint = `):print(A):B := scanf(`%e`)[1]: print(`Right endpoint = `):print(B):if A >= B thenprint(`Left endpoint must be less than right endpoint.`):else OK := TRUE:fi:od:print(`Input Y(A).`):ALPHA := scanf(`%e`)[1]: print(`alpha = `):print(ALPHA):print(`Input Y(B).`): BETA := scanf(`%e`)[1]: print(`beta = `):print(BETA):TK := (BETA-ALPHA)/(B-A):print(`TK := `): print(TK):print(`Input new TK? Enter 1 for yes or 2 for no.`):AA := scanf(`%d`)[1]: print(`Input is `):print(AA):if AA = 1 thenprint(`Input new TK`):TK := scanf(`%f`)[1]: print(`New TK = `):print(TK):fi:OK := FALSE:while OK = FALSE doprint(`Input an integer > 1 for the number of subintervals.`):N := scanf(`%d`)[1]: print(`n = `):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:if OK = TRUE thenprint(`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 SHOOTING METHOD\134n\134n`):fprintf(OUP, ` I X(I) W1(I) W2(I)\134n`):# Step 1H := (B-A)/N:K := 1:# TK is already computed.OK := FALSE:# Step 2while K <= NN and OK = FALSE do# Step 3W1[0] := ALPHA:W2[0] := TK:U1 := 0 :U2 := 1:# Step 4# Runge-Kutta method for systems is used in Steps 5 and 6for I2 from 1 to N do# Step 5X := A+(I2-1)*H:T := X+0.5*H:# Step 6K11 := H*W2[I2-1]:K12 := H*F(X,W1[I2-1],W2[I2-1]):K21 := H*(W2[I2-1]+0.5*K12):K22 := H*F(T,W1[I2-1]+0.5*K11,W2[I2-1]+0.5*K12):K31 := H*(W2[I2-1]+0.5*K22):K32 := H*F(T,W1[I2-1]+0.5*K21,W2[I2-1]+0.5*K22):K41 := H*(W2[I2-1]+K32):K42 := H*F(X+H,W1[I2-1]+K31,W2[I2-1]+K32):W1[I2] := W1[I2-1]+(K11+2*(K21+K31)+K41)/6:W2[I2] := W2[I2-1]+(K12+2*(K22+K32)+K42)/6:K11 := H*U2:K12 := H*(FY(X,W1[I2-1],W2[I2-1])*U1+FYP(X,W1[I2-1],W2[I2-1])*U2):K21 := H*(U2+0.5*K12):K22 := H*(FY(T,W1[I2-1],W2[I2-1])*(U1+0.5*K11)+FYP(T,W1[I2-1],W2[I2-1])*(U2+0.5*K21)):K31 := H*(U2+0.5*K22):K32 := H*(FY(T,W1[I2-1],W2[I2-1])*(U1+0.5*K21)+FYP(T,W1[I2-1],W2[I2-1])*(U2+0.5*K22)):K41 := H*(U2+K32):K42 := H*(FY(X+H,W1[I2-1],W2[I2-1])*(U1+K31)+FYP(X+H,W1[I2-1],W2[I2-1])*(U2+K32)):U1 := U1+(K11+2*(K21+K31)+K41)/6:U2 := U2+(K12+2*(K22+K32)+K42)/6:od:# Step 7# Test for accuracyif abs(W1[N]-BETA) < TOL then# Step 8I2 := 0:fprintf(OUP, `%3d %13.8f %13.8f %13.8f\134n`, I2, A, ALPHA, TK):for I2 from 1 to N doJ := I2+1:X := A+I2*H:fprintf(OUP, `%3d %13.8f %13.8f %13.8f\134n`, I2, X, W1[J-1], W2[J-1]):od:fprintf(OUP, `Convergence in %d iterations\134n`, K):fprintf(OUP, ` t = %14.7e\134n`, TK):# Step 9OK := TRUE:else# Step 10# Newton's method applied to improve TKTK := TK-(W1[N]-BETA)/U1:K := K+1:fi:od:# Step 11# Procedure completed unsuccessfully.if OK = FALSE thenfprintf(OUP, `Method failed after %d iterations\134n`, NN):fi:fi:if OUP <> default thenfclose(OUP):print(`Output file `,NAME,` created successfully`):fi:JSFH