restart:# RUNGE-KUTTA-FEHLBERG ALGORITHM 5.3## 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 THE MINIMUM STEPSIZE WAS EXCEEDED. print(`This is the Runge-Kutta-Fehlberg 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 then print(`Left endpoint must be less than right endpoint`):elseOK := TRUE:fi:od:print(`Input the initial condition`):ALPHA := scanf(`%f`)[1]:print(`y(a)= `):print(ALPHA):OK := FALSE:while OK = FALSE doprint(`Input tolerance`):TOL := scanf(`%f`)[1]:print(`Tolerance= `):print(TOL):if TOL <= 0 thenprint(`Tolerance must be a positive.`):elseOK := TRUE:fi:od:OK := FALSE:while OK = FALSE doprint(`Input minimum and maximum mesh spacing separated by a space`):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 positive real number`): print(`and less than 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= `):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, `RUNGE-KUTTA-FEHLBERG METHOD\134n\134n`):fprintf(OUP, ` T(I) W(I) H R\134n\134n`):# Step 1H := HMAX:T := A:W := ALPHA:fprintf(OUP, `%12.7f %11.7f 0 0\134n`, T, W):OK := TRUE:# Step 2while T < B and OK = TRUE do# Step 3K1 := H*F(T,W):K2 := H*F(T+H/4,W+K1/4):K3 := H*F(T+3*H/8,W+(3*K1+9*K2)/32):K4 := H*F(T+12*H/13,W+(1932*K1-7200*K2+7296*K3)/2197):K5 := H*F(T+H,W+439*K1/216-8*K2+3680*K3/513-845*K4/4104):K6 := H*F(T+H/2,W-8*K1/27+2*K2-3544*K3/2565+1859*K4/4104-11*K5/40):# Step 4R := abs(K1/360-128*K3/4275-2197*K4/75240.0+K5/50+2*K6/55)/H:# Step 5if R <= TOL then# Step 6# Approximation acceptedT := T+H:W := W+25*K1/216+1408*K3/2565+2197*K4/4104-K5/5:# Step 7fprintf(OUP, `%12.7f %11.7f %11.7f %11.7f\134n`, T, W, H, R):fi:# Step 8# To avoid underflowif R > 1.0E-20 thenDELTA := 0.84 * exp(0.25 * log(TOL / R)):elseDELTA := 10.0:fi:# Step 9# Calculate new Hif DELTA <= 0.1 thenH := 0.1*H:elseif DELTA >= 4 thenH := 4.0 * H:elseH := DELTA * H:fi:fi:# Step 10if H > HMAX thenH := HMAX:fi:# Step 11if H < HMIN thenOK := FALSE:elseif T+H > B thenif abs(B-T) < TOL thenT := B:elseH := B-T:fi:fi:fi:od:if OK = FALSE thenfprintf(OUP, `Minimal H exceeded\134n`):fi:# Step 12# Process is completeif OUP <> default thenfclose(OUP):print(`Output file`,NAME,` created successfully`):fi:fi:JSFH