odes.org
changeset 84 417992d2711e
parent 14 4182c6c7e1c6
child 86 a63a14de8584
equal deleted inserted replaced
83:b27e16802957 84:417992d2711e
    16     population.
    16     population.
    17     This is given by dy/dt = ky(L-y) where L is the total population.
    17     This is given by dy/dt = ky(L-y) where L is the total population.
    18     For our problem Let us use L=25000, k=0.00003.
    18     For our problem Let us use L=25000, k=0.00003.
    19     Let the boundary condition be y(0)=250.
    19     Let the boundary condition be y(0)=250.
    20 
    20 
    21     First of all run the magic command to import odeint to our program.
    21     Lets start ipython -pylab interpreter.    
       
    22     
       
    23     As we saw in one of earlier session, sometime pylab wont 'import' all
       
    24     packages. For solving 'ordinary differential equations' also we shall
       
    25     import 'odeint' function which is part SciPy package. So we run the 
       
    26     magic command:
    22 
    27 
    23     In []: from scipy.integrate import odeint
    28     In []: from scipy.integrate import odeint
    24 
    29 
    25 
    30     # For now just remember this as a command that does some magic to obtain
    26     For now just remember this as a command that does some magic to obtain
    31     # the function odeint in to our program.
    27     the function odeint in to our program.
    32     We will cover more details regarding 'import' in subsequent sessions.
    28     We will come back to the details of this command in subsequent sessions.
       
    29 
    33 
    30     We can represent the given ODE as a Python function.
    34     We can represent the given ODE as a Python function.
    31     This function takes the dependent variable y and the independent variable t
    35     This function takes the dependent variable y and the independent variable t
    32     as arguments and returns the ODE.
    36     as arguments and returns the ODE.
    33     Our function looks like this:
    37     Our function looks like this:
    49     
    53     
    50     In []: y = odeint(epid, 250, t)
    54     In []: y = odeint(epid, 250, t)
    51 
    55 
    52     We can plot the the values of y against t to get a graphical picture our ODE.
    56     We can plot the the values of y against t to get a graphical picture our ODE.
    53 
    57 
    54 
    58     plot(y, t)
    55     Let us move on to solving a system of two ordinary differential equations.
    59     Lets close this plot and move on to solving ordinary differential equation of 
       
    60     second order.
    56     Here we shall take the example ODEs of a simple pendulum.
    61     Here we shall take the example ODEs of a simple pendulum.
    57 
    62 
    58     The equations can be written as a system of two first order ODEs
    63     The equations can be written as a system of two first order ODEs
    59 
    64 
    60     d(theta)/dt = omega
    65     d(theta)/dt = omega
    61 
    66     
    62     and
    67     and
    63 
    68 
    64     d(omega)/dt = - g/L sin(theta)
    69     d(omega)/dt = - g/L sin(theta)
    65 
    70 
    66     Let us define the boundary conditions as: at t = 0, 
    71     Let us define the boundary conditions as: at t = 0, 
    67     theta = theta 0 (10 degrees) and omega = 0
    72     theta = theta naught = 10 degrees and 
       
    73     omega = 0
    68 
    74 
    69     Let us first define our system of equations as a Python function, pend_int.
    75     Let us first define our system of equations as a Python function, pend_int.
    70     As in the earlier case of single ODE we shall use odeint function of Python
    76     As in the earlier case of single ODE we shall use odeint function of Python
    71     to solve this system of equations by passing pend_int to odeint.
    77     to solve this system of equations by passing pend_int to odeint.
    72 
    78 
    79       ....     L = 0.2
    85       ....     L = 0.2
    80       ....     f=[omega, -(g/L)*sin(theta)]
    86       ....     f=[omega, -(g/L)*sin(theta)]
    81       ....     return f
    87       ....     return f
    82       ....
    88       ....
    83 
    89 
    84     It takes two arguments. The first argument is a 2-tuple containing the two
    90     It takes two arguments. The first argument itself containing two
    85     dependent variables in the system, theta and omega.
    91     dependent variables in the system, theta and omega.
    86     The second argument is the independent variable t.
    92     The second argument is the independent variable t.
    87 
    93 
    88     In the function we assign theta and omega to first and second values of the
    94     In the function we assign theta and omega to first and second values of the
    89     initial argument respectively.
    95     initial argument respectively.
   103 
   109 
   104     In []: initial = [10*2*pi/360, 0]
   110     In []: initial = [10*2*pi/360, 0]
   105 
   111 
   106     Now solving this system is just a matter of calling the odeint function with
   112     Now solving this system is just a matter of calling the odeint function with
   107     the correct arguments.
   113     the correct arguments.
   108     So first let us import odeint function into our program using the magic
       
   109     import command
       
   110 
       
   111     In []: from scipy.integrate import odeint
       
   112 
       
   113     We can call ode_int as:
       
   114 
   114 
   115     In []: pend_sol = odeint(pend_int, initial,t)
   115     In []: pend_sol = odeint(pend_int, initial,t)
   116 
   116 
       
   117     In []: plot(pend_sol[0], t) plot theta against t
       
   118     In []: plot(pend_sol[1], t) will plot omega against t
   117     Plotting theta against t and omega against t we obtain the plots as shown
   119     Plotting theta against t and omega against t we obtain the plots as shown
   118     in the slide.
   120     in the slide.
   119 
   121 
   120     Thus we come to the end of this session on solving ordinary differential
   122     Thus we come to the end of this session on solving ordinary differential
   121     equations in Python. Thanks for listening to this tutorial.
   123     equations in Python. Thanks for listening to this tutorial.