restart:# RUNGE-KUTTA FOR SYSTEMS OF DIFFERENTIAL EQUATIONS ALGORITHM 5.7## TO APPROXIMATE THE SOLUTION OF THE MTH-ORDER SYSTEM OF FIRST-# ORDER INITIAL-VALUE PROBLEMS# UJ` = FJ( T, U1, U2, ..., UM ), J = 1, 2, ..., M# A <= T <= B, UJ(A) = ALPHAJ, J = 1, 2, ..., M# AT (N+1) EQUALLY SPACED NUMBERS IN THE INTERVAL [A,B].## INPUT: ENDPOINTS A,B: NUMBER OF EQUATIONS M: INITIAL# CONDITIONS ALPHA1, ..., ALPHAM: INTEGER N.## OUTPUT: APPROXIMATION WJ TO UJ(T) AT THE (N+1) VALUES OF T.print(`This is the Runge-Kutta Method for Systems of m equations`):OK := FALSE:while OK = FALSE doprint(`Input the number of equations`):M := scanf(`%d`)[1]:print(`Number of equations is `):print(M):if M <= 0 then print(`Number must be a positive integer\134n`):elseOK := TRUE:fi:od:for I1 from 1 to M doprint(`Input the function f[i](t,y1 ... yM) in terms of t, y1, ... for i = `):print(I1):print(`For example: y1-t^2+1`):F[I1] := scanf(`%a`)[1]:print(`function is `):print(F[I1]):F[I1] := unapply(F[I1],t,evaln(y || (1..M))):od:OK := FALSE:while OK = FALSE doprint(`Input left and right endpoints separated by blank`):A := scanf(`%f`)[1]:B := scanf(`%f`)[1]:print(`a = `):print(A): print(`b = `):print(B):if A >= B then print(`Left endpoint must be less than right endpoint`):elseOK := TRUE:fi:od:for I1 from 1 to M doprint(`Input the initial condition for i = `):print(I1):ALPHA[I1] := scanf(`%f`)[1]:print(`initial condition is `):print(ALPHA[I1]):od:OK := FALSE:while OK = FALSE doprint(`Input a positive integer for the number of subintervals`):N := scanf(`%d`)[1]:print(`N = `):print(N):if N <= 0 thenprint(`Number must be a positive integer`):elseOK := TRUE: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(`Entry = `):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, `RUNGE-KUTTA METHOD FOR SYSTEMS OF DIFFERENTIAL EQUATIONS\134n\134n`):fprintf(OUP, ` T`):for I1 from 1 to M dofprintf(OUP, ` W%d`, I1):od:# Step 1H := (B-A)/N:T := A:# Step 2for J from 1 to M doW[J] := ALPHA[J]:od:# Step 3fprintf(OUP, `\134n%5.3f`, T):for I1 from 1 to M dofprintf(OUP, ` %11.8f`, W[I1]):od:fprintf(OUP, `\134n`):# Step 4for L from 1 to N do# Step 5for J from 1 to M doK[1,J] := H*F[J](T,seq(W[i1],i1=1..M)):od:# Step 6for J from 1 to M doK[2,J] := H*F[J](T+H/2.0,seq(W[i1]+K[1,i1]/2.0,i1=1..M)):od:# Step 7for J from 1 to M doK[3,J] := H*F[J](T+H/2.0,seq(W[i1]+K[2,i1]/2.0,i1=1..M)):od:# Step 8for J from 1 to M doK[4,J] := H*F[J](T+H,seq(W[i1]+K[3,i1],i1=1..M)):od:# Step 9for J from 1 to M doW[J] := W[J]+(K[1,J]+2.0*K[2,J]+2.0*K[3,J]+K[4,J])/6.0:od:# Step 10T := A+L*H:# Step 11fprintf(OUP, `%5.3f`, T):for I1 from 1 to M dofprintf(OUP, ` %11.8f`, W[I1]):od:fprintf(OUP, `\134n`):od:# Step 12if OUP <> default thenfclose(OUP):print(`Output file`,NAME,` created successfully`):fi:fi:JSFH