restart:# ADAMS VARIABLE STEP-SIZE PREDICTOR-CORRECTOR ALGORITHM 5.5## 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 step size HMAX: minimum step size HMIN.## OUTPUT: I, T(I), W(I), H where at the Ith step W(I) approximates# y(T(I)) and step size H was used or a message that the# minimum step size was exceeded.# Step 1RK4 := proc(H,X,Y,KK) local K1, K2, K3, K4: global W,T:K1 := H*F(X,Y):K2 := H*F(X+0.5*H,Y+0.5*K1):K3 := H*F(X+0.5*H,Y+0.5*K2):K4 := H*F(X+H,Y+K3):W[KK] := Y+(K1+2.0*(K2+K3)+K4)/6.0:T[KK] := X+H:end:print(`This is the Adams Variable Step-size Predictor-Corrector Method`):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(`Output file is `):print(NAME):OUP := fopen(NAME,WRITE,TEXT):elseOUP := default:fi:fprintf(OUP, `ADAMS VARIABLE STEP-SIZE PREDICTOR CORRECTOR METHOD\134n\134n`):fprintf(OUP, ` t w h sigma\134n`):# Step 2T[0] := A:W[0] := ALPHA:H := HMAX:# OK is used in place of FLAG to exit the loop in Step 4OK := TRUE:# DONE is used in place of LAST to indicate when the last# value is calculatedDONE := FALSE:# Step 3for KK from 1 to 3 doRK4(H,T[KK-1],W[KK-1],KK):od:# NFLAG indicates calculation from RK4NFLAG := 1:I1 := 5:# Use TT in place of tTT := T[3] + H:# Step 4while DONE = FALSE do# Step 5# Predict W(I)Part1 := 55.0*F(T[I1-2],W[I1-2])-59.0*F(T[I1-3],W[I1-3]):Part2 := 37.0*F(T[I1-4],W[I1-4])-9.0*F(T[I1-5],W[I1-5]):WP := W[I1-2]+H*(Part1+Part2)/24.0:# Correct W(I)Part1 := 9.0*F(TT,WP)+19.0*F(T[I1-2],W[I1-2])-5.0*F(T[I1-3],W[I1-3]):Part2 := F(T[I1-4],W[I1-4]):WC := W[I1-2]+H*(Part1+Part2)/24.0:SIG := 19.0*abs(WC-WP)/(270.0*H):# Step 6if SIG <= TOL then# Step 7# Result acceptedW[I1-1] := WC:T[I1-1] := TT:# Step 8if NFLAG = 1 thenK := I1-3:KK := I1-1:# Previous results are also acceptedfor J from K to KK dofprintf(OUP, `%12.8f %11.8f %11.8f %11.8f\134n`, T[J-1], W[J-1], H, SIG):od:fprintf(OUP, `%12.8f %11.8f %11.8f %11.8f\134n`, T[I1-1], W[I1-1], H, SIG):else# Previous results were already acceptedfprintf(OUP, `%12.8f %11.8f %11.8f %11.8f\134n`, T[I1-1], W[I1-1], H, SIG):fi:# Step 9if OK = FALSE thenDONE := TRUE:else# Step 10I1 := I1+1:NFLAG := 0:# Step 11if SIG <= 0.1*TOL or T[I1-2]+H > B then# Increase H if more accuracy has been obtained than is required, or# decrease H to include B as a mesh point# Step 12# To avoid underflowif SIG <= 1.0e-20 thenQ := 4.0:elseQ := (0.5*TOL/SIG)^(1/4):fi:# Step 13if Q > 4.0 thenH := 4.0*H:elseH := Q * H:fi:# Step 14if H > HMAX thenH := HMAX:fi:# Step 15if T[I1-2]+4.0*H > B thenH := 0.25*(B-T[I1-2]):if H < TOL thenDONE := TRUE:fi:OK := FALSE:fi:# Step 16for KK from I1-1 to I1+2 doRK4(H,T[KK-1],W[KK-1],KK):od:NFLAG := 1:I1 := I1+3:fi:fi:else# FALSE branch for Step 6 - result rejected# Step 17Q := (0.5*TOL/SIG)^(1/4):# Step 18if Q < 0.1 thenH := 0.1 * H:elseH := Q * H:fi:# Step 19if H < HMIN thenfprintf(OUP, `HMIN exceeded\134n`):DONE := TRUE:elseif T[I1-2]+4.0*H > B thenH := 0.25*(B-T[I1-2]):fi:if NFLAG = 1 thenI1 := I1-3:fi:for KK from I1-1 to I1+2 doRK4(H,T[KK-1],W[KK-1],KK):od:I1 := I1+3:NFLAG := 1:fi:fi:# Step 20TT := T[I1-2] + H:od:# Step 21if OUP <> default thenfclose(OUP):print(`Output file`,NAME,` created successfully`):fi:fi:JSFH