restart: # RUNGE-KUTTA (ORDER 4) ALGORITHM 5.2 # # TO APPROXIMATE THE SOLUTION TO THE INITIAL VALUE PROBLEM: # Y' = F(T,Y), A<=T<=B, Y(A) = ALPHA, # AT (N+1) EQUALLY SPACED NUMBERS 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 the Runge-Kutta Order Four 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 a positive integer for the number of subintervals`): N := scanf(`%d`)[1]:print(`N = `):print(N): if N <= 0 then print(`Number must be a positive integer`): 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, `RUNGE-KUTTA FOURTH ORDER METHOD\134n\134n`): fprintf(OUP, ` t w\134n\134n`): # Step 1 H := (B-A)/N: T := A: W := ALPHA: fprintf(OUP, `%5.3f %11.7f\134n`, T, W): # Step 2 for I1 from 1 to N do # Step 3 # Use K1, K2, K3, K4 for K(1), K(2), K(3), K(4) resp. K1 := H*F(T,W): K2 := H*F(T+H/2.0, W+K1/2.0): K3 := H*F(T+H/2.0, W+K2/2.0): K4 := H*F(T+H,W+K3): # Step 4 # Compute W(I) W := W+(K1+2.0*(K2+K3)+K4)/6.0: # Compute T(I) T := A+I1*H: # Step 5 fprintf(OUP, `%5.3f %11.7f\134n`, T, W): od: # Step 6 if OUP <> default then fclose(OUP): print(`Output file`,NAME,` created successfully`): fi: fi: SUtUaGlzfmlzfnRoZX5SdW5nZS1LdXR0YX5PcmRlcn5Gb3Vyfk1ldGhvZC5HNiI= SU5JbnB1dH50aGV+ZnVuY3Rpb25+Zih0LHkpfmlufnRlcm1zfm9mfnR+YW5kfnlHNiI= STVGb3J+ZXhhbXBsZTp+eS10XjIrMUc2Ig== SSpmKHQseSl+PX5HNiI= LChJInlHNiIiIiIqJEkidEdGJCIiIyEiIkYlRiU= SVJJbnB1dH5sZWZ0fmFuZH5yaWdodH5lbmRwb2ludHN+c2VwYXJhdGVkfmJ5fmJsYW5rRzYi SSVhfj1+RzYi JCIiIUYj SSVifj1+RzYi JCIiIyIiIQ== STxJbnB1dH50aGV+aW5pdGlhbH5jb25kaXRpb25HNiI= SSh5KGEpfj1+RzYi JCIiJiEiIg== SVhJbnB1dH5hfnBvc2l0aXZlfmludGVnZXJ+Zm9yfnRoZX5udW1iZXJ+b2Z+c3ViaW50ZXJ2YWxzRzYi SSVOfj1+RzYi IiM1 STlDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDpHNiI= STQxLn5PdXRwdXR+dG9+c2NyZWVuRzYi STcyLn5PdXRwdXR+dG9+dGV4dH5maWxlRzYi STRQbGVhc2V+ZW50ZXJ+MX5vcn4yRzYi STBZb3VyfmNob2ljZX5pc35HNiI= IiIi RUNGE-KUTTA FOURTH ORDER METHOD t w 0.000 0.5000000 0.200 0.8292933 0.400 1.2140762 0.600 1.6489220 0.800 2.1272027 1.000 2.6408227 1.200 3.1798942 1.400 3.7323401 1.600 4.2834095 1.800 4.8150857 2.000 5.3053630 JSFH