Page 129 -
P. 129

tinit=0;
                                tfin=16*pi;
                                N=1001;
                                t=linspace(tinit,tfin,N);
                                dt=(tfin-tinit)/(N-1);

                                   for k=1:N-1
                                   z(:,k+1)=prk4(t(k),z(:,k),dt);
                                   end

                                plot(t,z(1,:),t,sin(t),'--')
                             In the above plot, we are comparing the temporal profiles of the voltage dif-
                             ference across the capacitor with that of the source voltage.


                             4.7.3  MATLAB ODE Solvers
                             MATLAB has many ODE solvers, ODE23 and ODE45 being most commonly
                             used. ODE23 is based on a pair of second-order and third-order Runge-Kutta
                             methods running simultaneously to solve the ODE. The program automati-
                             cally corrects for the step size if the answers from the two methods have a dis-
                             crepancy at any stage of the calculation that will lead to a larger error than the
                             allowed tolerance.
                              To use this solver, we start by creating a function M-file that includes the sys-
                             tem of equations under consideration. This function is then called from the
                             command window with the ODE23 or ODE45 command.


                             Example 4.12
                             Using the MATLAB ODE solver, find the voltage across the capacitor in the
                             RLC circuit of Example 4.11, and compare it to the source potential time-profile.

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

                                function zp=RLC11(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));

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

                                tspan=[0 16*pi];


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