restart:# EXTRAPOLATION ALGORITHM 5.6## To approximate the solution of the initial value problem:# y' = f(t,y), a <= t <= b, y(a) = ALPHA,# with local truncation error within a given tolerance:## INPUT: endpoints a,b: initial condition ALPHA: tolerance TOL:# maximum stepsize HMAX: minimum stepsize HMIN.## OUTPUT: T, W, H where W approximates y(T) and stepsize H was# used or a message that minimum stepsize was exceeded.print(`This is Gragg Extrapolation`):print(`Input the function f(t,y) in terms of t and y`):print(`For example: y-t^2+1`):F := scanf(`%a`)[1]:print(`f(t,y) = `):print(F):F := unapply(F,t,y):OK := FALSE:while OK = FALSE doprint(`Input left and right endpoints separated by blank.`):A := scanf(`%f`)[1]:print(`a = `):print(A):B := scanf(`%f`)[1]:print(`b = `):print(B):if A >= B thenprint(`Left endpoint must be less than right endpoint.`):elseOK := TRUE:fi:od:OK := FALSE:print(`Input the initial condition.`):ALPHA := scanf(`%f`)[1]:print(`y(a) = `):print(ALPHA):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 minimum and maximum mesh spacing `):print(`separated by a blank.`):HMIN := scanf(`%f`)[1]:print(`Minimum spacing = `):print(HMIN):HMAX := scanf(`%f`)[1]:print(`Maximum spacing = `):print(HMAX):if HMIN < HMAX and HMIN > 0 thenOK := TRUE:elseprint(`Minimum mesh spacing must be a `):print(`positive real number and less than`):print(`the maximum mesh spacing.`):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(`Your choice 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(`File name is `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `GRAGG EXTRAPOLATION\134n\134n`):fprintf(OUP, ` T W H K\134n`):# STEP 1 NK[0] := 2:NK[1] := 4:for J from 1 to 3 doI1 := 2*J:NK[I1] := 3*NK[I1-1]/2:NK[I1+1] := 2*NK[I1-1]:od:# STEP 2 T0 := A:W0 := ALPHA:H := HMAX:# DONE is used in place of FLAG to exit the loop in Step 4 DONE := FALSE:# STEP 3 for I1 from 1 to 7 dofor J from 1 to I1 doQ[I1-1,J-1] := (NK[I1]*1/NK[J-1])*(NK[I1]*1/NK[J-1]):od:od:# STEP 4 while DONE = FALSE do# STEP 5 K := 1:# when desired accuracy achieved, NFLAG is set to 1 NFLAG := 0:# STEP 6 while K <= 8 and NFLAG = 0 do# STEP 7 HK := H/NK[K-1]:T := T0:W2 := W0:# Euler first step W3 := W2+HK*F(T, W2):T := T0+HK:# STEP 8 M := NK[K-1]-1:for J from 1 to M doW1 := W2:W2 := W3:# midpoint method W3 := W1+2*HK*F(T, W2):T := T0+(J+1)*HK:od:# STEP 9 # endpoint correction to compute Y(K,1) Y[K] := (W3+W2+HK*F(T, W3))/2:# STEP 10 # NOTE: Y(K-1)=Y(K-1,1),Y(K-2)=Y(K-2,2),..., # Y(1)=Y(K-1,K-1) since only previous row of table # is saved if K >= 2 then# STEP 11 J := K:# save Y(K-1,K-1) V := Y[1]:# STEP 12 while J >= 2 do# extrapolation to compute # Y(J-1) := Y(K,K-J+2) Y[J-1] := Y[J]+(Y[J]-Y[J-1])/(Q[K-2,J-2]-1):J := J-1:od:# STEP 13 if abs(Y[1] - V) <= TOL thenNFLAG := 1:fi:# Y(1) accepted as new w fi:# STEP 14 K := K+1:od:# STEP 15 K := K-1:# STEP 16 if NFLAG = 0 then# STEP 17 # new value for w rejected, decrease H H := H/2:# STEP 18 if H < HMIN thenfprintf(OUP, `HMIN exceeded\134n`):DONE := TRUE:fi:else# STEP 19 # new value for w accepted W0 := Y[1]:T0 := T0 + H:fprintf(OUP, `%12.8f %11.8f %11.8f %6d\134n`, T0, W0, H, K):# STEP 20 # increase H if possible if T0 >= B thenDONE := TRUE:# Procedure completed successfully.elseif T0 + H > B thenH := B - T0:# Terminate at t = B.elseif K <= 3 thenH := 2*H:fi:fi:fi:if H > HMAX thenH := H/2:fi:fi:od:# STEP 21 if OUP <> default thenfclose(OUP):print(`Output file`,NAME,` created successfully`):fi:fi:JSFHJSFH