'******************************************************************** '* Differential equations with p variables of order 1 * '* by Runge-Kutta method of order 4 * '* ---------------------------------------------------------------- * '* Reference: "Analyse en Turbo Pascal versions 5.5 et 6.0 by Marc * '* DUCAMP et Alain REVERCHON - Eyrolles, Paris 1991" * '* * '* Basic version 1.1 By J-P Moreau, Paris * '* (www.jpmoreau.fr) * '* ---------------------------------------------------------------- * '* SAMPLE RUN: * '* * '* Example #1: integrate system of equations from x=0 to x=3: * '* y1' = y2 + y3 - 3*y1 * '* y2' = y1 + y3 - 3*y2 * '* y3' = y1 + y2 - 3*y3 * '* with initial conditions: y1(0)=1, y2(0)=2 and y3(0)=-1 * '* * '* * '* DIFFERENTIAL EQUATION WITH P VARIABLE OF ORDER 1 * '* of type yi' = f(y1,y2,...,yn), i=1..n * '* * '* number of variables: 3 * '* begin value x : 0 * '* end value x : 3 * '* y1 value at x0 : 1 * '* y2 value at x0 : 2 * '* y3 value at x0 : -1 * '* number of points : 7 * '* finesse : 30 * '* * '* X Y1 Y2 Y3 * '* -------------------------------------------------------- * '* 0.000000 1.000000 2.000000 -1.000000 * '* 0.500000 0.449466 0.584801 0.178795 * '* 1.000000 0.251358 0.269674 0.214727 * '* 1.500000 0.149580 0.152058 0.144622 * '* 2.000000 0.090335 0.090671 0.089664 * '* 2.500000 0.054738 0.054784 0.054648 * '* 3.000000 0.033193 0.033200 0.033181 * '* -------------------------------------------------------- * '* * '* Example #2: integrate system of equations from x=0 to PI*Sqrt(2) * '* y1' = y2 * '* y2' = -4*y1 - 3*y3 * '* y3' = y4 * '* y4' = -8*y1 - 2*y3 * '* with initial conditions: y1(0)=3, y2(0)=0, y3(0)=4, y4(0)=0 * '* * '* * '* DIFFERENTIAL EQUATION WITH P VARIABLE OF ORDER 1 * '* of type yi' = f(y1,y2,...,yn), i=1..n * '* * '* number of variables: 4 * '* begin value x : 0 * '* end value x : 4.442883 * '* y1 value at x0 : 3 * '* y2 value at x0 : 0 * '* y3 value at x0 : 4 * '* y4 value at x0 : 0 * '* number of points : 9 * '* finesse : 30 * '* * '* X Y1 Y2 Y3 Y4 * '* -------------------------------------------------------- * '* 0.000000 3.000000 0.000000 4.000000 0.000000 * '* 0.555360 0.000000 -8.485281 0.000000 -11.313708 * '* 1.110721 -3.000000 -0.000001 -4.000000 -0.000002 * '* 1.666081 -0.000001 8.485281 -0.000001 11.313708 * '* 2.221442 3.000000 0.000003 4.000000 0.000003 * '* 2.776802 0.000001 -8.485281 0.000002 -11.313708 * '* 3.332162 -3.000000 -0.000004 -4.000000 -0.000005 * '* 3.887523 -0.000002 8.485281 -0.000002 11.313708 * '* 4.442883 3.000000 0.000005 4.000000 0.000007 * '* -------------------------------------------------------- * '* * '* Release 1.1: Added example #2 (Sept. 2008). * '******************************************************************** DEFINT I-N DEFDBL A-H, O-Z 'ifi,ip : INTEGER DIM y(10), yi(10), t1(50), t2(50), t3(50), t4(50) CLS PRINT PRINT " DIFFERENTIAL EQUATIONS WITH P VARIABLES OF ORDER 1" PRINT " of type yi' = f(y1,y2,...,yn), i=1..n" PRINT PRINT " number of variables : "; : INPUT ip PRINT PRINT " begin value x : "; : INPUT xi PRINT " end value x : "; : INPUT xf FOR i = 0 TO ip - 1 PRINT " y"; i + 1; " value at x0 : "; : INPUT yi(i) NEXT i PRINT " number of points : "; : INPUT m PRINT " finesse : "; : INPUT ifi 'call subroutine eqdifp GOSUB 2000 END 'Example #1: y1'=y2+y3-3y1, y2'=y1+y3-3y2, y3'=y1+y2-3y3 '1000 'FUNCTION fp ' IF k = 0 THEN ' fp = y(1) + y(2) - 3# * y(0) ' ELSEIF k = 1 THEN ' fp = y(0) + y(2) - 3# * y(1) ' ELSEIF k = 2 THEN ' fp = y(0) + y(1) - 3# * y(2) ' ELSE ' fp = 0# ' END IF 'RETURN 'Example #2: y1'=y2, y2'=-4y1-3y3, y'3=y4, y'4=-8y1-2y3 1000 'FUNCTION fp IF k = 0 THEN fp = y(1) ELSEIF k = 1 THEN fp = -4# * y(0) - 3# * y(2) ELSEIF k = 2 THEN fp = y(3) ELSEIF k = 3 THEN fp = -8# * y(0) - 2# * y(2) ELSE fp = 0# END IF RETURN '**************************************************************************** '* SOLVING DIFFERENTIAL SYSTEMS WITH P VARIABLES OF ORDER 1 * '* of type yi' = f(y1,y2,...,yn), i=1..n * '* ------------------------------------------------------------------------ * '* INPUTS: * '* m number of points to calculate * '* xi, xf begin, end values of variable x * '* yi table of begin values of functions at xi * '* ip number of independant variables * '* ifi finesse (number of intermediary points) * '* * '* OUTPUTS: * '* t1,t2 real vectors storing the results for first two functions, * '* y1 and y2. * '* ------------------------------------------------------------------------ * '* EXAMPLE: y1'=y2+y3-3y1, y2'=y1+y3-3y2, y3'=y1+y2-3y3 * '* Exact solution : y1 = 1/3 (exp(-4x) + 2 exp(-x)) * '* y2 = 1/3 (4exp(-4x) + 2 exp(-x)) * '* y3 = 1/3 (-5exp(-4x)+ 2 exp(-x)) * '**************************************************************************** 2000 'subroutine Eqdifp DIM ta(10), tb(10), tc(10), td(10), z(10) h = (xf - xi) / ifi / (m - 1) ip = ip - 1 t1(1) = yi(0) t2(1) = yi(1) t3(1) = yi(2) t4(1) = yi(3) FOR k = 0 TO ip y(k) = yi(k): z(k) = yi(k) NEXT k FOR i = 1 TO m ni = (i - 1) * ifi - 1 FOR j = 1 TO ifi x = xi + h * (ni + j) FOR k = 0 TO ip y(k) = z(k) NEXT k FOR k = 0 TO ip GOSUB 1000 ta(k) = h * fp NEXT k FOR k = 0 TO ip y(k) = z(k) + ta(k) / 2# NEXT k x = x + h / 2# FOR k = 0 TO ip GOSUB 1000 tb(k) = h * fp NEXT k FOR k = 0 TO ip y(k) = z(k) + tb(k) / 2# NEXT k FOR k = 0 TO ip GOSUB 1000 tc(k) = h * fp NEXT k FOR k = 0 TO ip y(k) = z(k) + tc(k) NEXT k x = x + h / 2# FOR k = 0 TO ip GOSUB 1000 td(k) = h * fp NEXT k FOR k = 0 TO ip z(k) = z(k) + (ta(k) + 2# * tb(k) + 2# * tc(k) + td(k)) / 6# NEXT k NEXT j t1(i + 1) = z(0) t2(i + 1) = z(1) t3(i + 1) = z(2) t4(i + 1) = z(3) NEXT i 'Display(t1,t2,t3,t4,m,p,xi,xf) GOSUB 3000 RETURN 3000 'subroutine Display h = (xf - xi) / (m - 1) x = xi - h PRINT PRINT " X"; FOR i = 1 TO ip + 1 PRINT " Y"; i; NEXT i PRINT PRINT "--------------------------------------------------------" FOR i = 1 TO m x = x + h IF ip = 1 THEN PRINT USING " ##.###### ##.###### ##.######"; x; t1(i); t2(i) ELSEIF ip = 2 THEN PRINT USING " ##.###### ##.###### ##.###### ##.######"; x; t1(i); t2(i); t3(i) ELSEIF ip > 2 THEN PRINT USING " ##.###### ##.###### ##.###### ##.###### ###.######"; x; t1(i); t2(i); t3(i); t4(i) END IF NEXT i PRINT "--------------------------------------------------------" RETURN 'End of file teqdifp.bas