Page 128 -
P. 128

(where the prime and double primes superscripted functions refer, respec-
                             tively, to the first and second derivative of this function).

                             Solution: Introduce the two-dimensional array z, and define

                                                           z(1) = y                       (4.56a)

                                                           z(2) = y′                      (4.56b)

                             The system of first-order equations now reads:

                                                          z′(1) = z(2)                    (4.57a)

                                                z′(2) = (1/a)(sin(t) – bz(2) – cz(1))     (4.57b)


                             Example 4.11
                             Using the fourth-order Runge-Kutta iterator, numerically solve the same
                             problem as in Application 1 following Example 4.9.

                             Solution: Edit and save the following function M-files:

                                function zp=zprime(t,z)
                                a=1; b=3; c=1;
                                zp(1,1)=z(2,1);
                                zp(2,1)=(1/a)*(sin(t)-b*z(2,1)-c*z(1,1));

                             The above file specifies the system of ODE that we are trying to solve.
                              Next, in another function M-file, we edit and save the fourth-order Runge-
                             Kutta algorithm, specifically:

                                function zn=prk4(t,z,dt)
                                k1=dt*zprime(t,z);
                                k2=dt*zprime(t+dt/2,z+k1/2);
                                k3=dt*zprime(t+dt/2,z+k2/2);
                                k4=dt*zprime(t+dt,z+k3);
                                zn=z+(k1+2*k2+2*k3+k4)/6;

                             Finally, edit and execute the following script M-file:

                                yinit=0;
                                ypinit=0;
                                z=[yinit;ypinit];



                             © 2001 by CRC Press LLC
   123   124   125   126   127   128   129   130   131   132   133