restart: # ADAMS-FOURTH ORDER PREDICTOR-CORRECTOR ALGORITHM 5.4 # # To approximate the solution of the initial value problem # y' = f(t,y), a <= t <= b, y(a) = alpha, # at N+1 equally spaced points in the interval [a,b]. # # INPUT: endpoints a,b: initial condition alpha: integer N. # # OUTPUT: approximation w to y at the (N+1) values of t. print(`This is Adams-Bashforth 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 do print(`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 then print(`Left endpoint must be less than right endpoint`): else OK := TRUE: fi: od: print(`Input the initial condition`): ALPHA := scanf(`%f`)[1]:print(`y(a) = `):print(ALPHA): OK := FALSE: while OK = FALSE do print(`Input an integer > 3 for the number of subintervals`): N := scanf(`%d`)[1]:print(`N = `):print(N): if N <= 3 then print(`Number must be greater than 3`): else OK := TRUE: fi: od: if OK = TRUE then 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(`Your choice is `):print(FLAG): if FLAG = 2 then print(`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): else OUP := default: fi: fprintf(OUP, `ADAMS-BASHFORTH FOURTH ORDER PREDICTOR CORRECTOR METHOD\134n\134n`): fprintf(OUP, ` t w\134n`): # Step 1 H := (B-A)/N: T[0] := A: W[0] := ALPHA: fprintf(OUP, `%5.3f %11.7f\134n`, T[0], W[0]): # Step 2 for I1 from 1 to 3 do # Steps 3 and 4 # Compute starting values using Runger-Kutta 4 T[I1] := T[I1-1]+H: K1 := H*F(T[I1-1], W[I1-1]): K2 := H*F(T[I1-1]+0.5*H, W[I1-1]+0.5*K1): K3 := H*F(T[I1-1]+0.5*H, W[I1-1]+0.5*K2): K4 := H*F(T[I1], W[I1-1]+K3): W[I1] := W[I1-1]+(K1+2.0*(K2+K3)+K4)/6.0: # Step 5 fprintf(OUP, `%5.3f %11.7f\134n`, T[I1], W[I1]): od: # Step 6 for I1 from 4 to N do # Step 7 # T0 and W0 are used in place of t, w resp. T0 := A+I1*H: # Predict W(I) Part1 := 55.0*F(T[3],W[3])-59.0*F(T[2],W[2])+37.0*F(T[1],W[1]): Part2 :=-9.0*F(T[0],W[0]): W0 := W[3]+H*(Part1+Part2)/24.0: # Correct W(I) Part1 := 9.0*F(T0,W0)+19.0*F(T[3],W[3])-5.0*F(T[2],W[2]): Part2 := F(T[1],W[1]): W0 := W[3]+H*(Part1+Part2)/24.0: # Step 8 fprintf(OUP, `%5.3f %11.7f\134n`, T0, W0): # Step 9 # Prepare for next iteration for J from 1 to 3 do T[J-1] := T[J]: W[J-1] := W[J]: od: # Step 10 T[3] := T0: W[3] := W0: od: fi: # Step 11 if OUP <> default then fclose(OUP): print(`Output file`,NAME,` created successfully`): fi: SVNUaGlzfmlzfkFkYW1zLUJhc2hmb3J0aH5QcmVkaWN0b3J+Q29ycmVjdG9yfk1ldGhvZEc2Ig== SU5JbnB1dH50aGV+ZnVuY3Rpb25+Zih0LHkpfmlufnRlcm1zfm9mfnR+YW5kfnlHNiI= STVGb3J+ZXhhbXBsZTp+eS10XjIrMUc2Ig== SSpmKHQseSl+PX5HNiI= LChJInlHNiIiIiIqJEkidEdGJCIiIyEiIkYlRiU= SVJJbnB1dH5sZWZ0fmFuZH5yaWdodH5lbmRwb2ludHN+c2VwYXJhdGVkfmJ5fmJsYW5rRzYi SSVhfj1+RzYi JCIiIUYj SSVifj1+RzYi JCIiIyIiIQ== STxJbnB1dH50aGV+aW5pdGlhbH5jb25kaXRpb25HNiI= SSh5KGEpfj1+RzYi JCIiJiEiIg== SVRJbnB1dH5hbn5pbnRlZ2Vyfj5+M35mb3J+dGhlfm51bWJlcn5vZn5zdWJpbnRlcnZhbHNHNiI= SSVOfj1+RzYi IiM1 STlDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDpHNiI= STQxLn5PdXRwdXR+dG9+c2NyZWVuRzYi STcyLn5PdXRwdXR+dG9+dGV4dH5maWxlRzYi STRQbGVhc2V+ZW50ZXJ+MX5vcn4yRzYi STBZb3VyfmNob2ljZX5pc35HNiI= IiIi ADAMS-BASHFORTH FOURTH ORDER PREDICTOR CORRECTOR METHOD t w 0.000 0.5000000 0.200 0.8292933 0.400 1.2140762 0.600 1.6489220 0.800 2.1272056 1.000 2.6408286 1.200 3.1799026 1.400 3.7323505 1.600 4.2834208 1.800 4.8150964 2.000 5.3053707 JSFH