Page 6 - DOC-20220324-WA0012.-488-500
P. 6

470    Chapter 11 Systems of Ordinary Differential Equations


                            procedure RK4 System1(n, h, t,(x i ), nsteps)
                            integer i, j, n;  real h, t;  real array (x i ) 1:n
                            allocate real array (y i ) 1:n ,(K i, j ) 1:n×1:4
                            output 0, t,(x i )
                            for j = 1 to nsteps do
                                call XP System(n, t,(x i ), (K i,1 ))
                                for i = 1 to n do
                                             1
                                    y i ← x i + hK i,1
                                             2
                                end for
                                call XP System(n, t + h/2,(y i ), (K i,2 ))
                                for i = 1 to n do
                                             1
                                    y i ← x i + hK i,2
                                             2
                                end for
                                call XP System(n, t + h/2,(y i ), (K i,3 ))
                                for i = 1 to n do
                                    y i ← x i + hK i,3
                                end for
                                call XP System(n, t + h,(y) i ,(K i,4 ))
                                for i = 1 to n do
                                             1
                                    x i ← x i + h[K i,1 + 2K i,2 + 2K i,3 + K i,4 ]
                                             6
                                end for
                                t ← t + h
                                output j, t,(x i )
                            end for
                            deallocate array (y i ), (K i, j )
                            end procedure RK4 System1



                            To illustrate the use of this procedure, we again use System (1) for our example. Of
                         course, it must be rewritten in the form of Equation (4). A suitable main program and a
                         procedure for computing the right-hand side of Equation (4) follow:



                            program Test RK4 System1
                            integer n ← 2, nsteps ← 100
                            real a ← 0, b ← 1
                            real h, t;  real array (x i ) 1:n
                            t ← 0
                            (x i ) ← (1, 0)
                            h ← (b − a)/nsteps
                            call RK4 System1(n, h, t,(x i ), nsteps)
                            end program Test RK4 System1
                            procedure XP System(n, t,(x i ), ( f i ))
                            real array (x i ) 1:n ,( f i ) 1:n
                            integer n
   1   2   3   4   5   6   7   8   9   10   11