9 *** Script |
9 *** Script |
10 Welcome friends. |
10 Welcome friends. |
11 |
11 |
12 In this tutorial we shall look at solving Ordinary Differential Equations, |
12 In this tutorial we shall look at solving Ordinary Differential Equations, |
13 ODE henceforth using odeint in Python. In this tutorial we shall be using |
13 ODE henceforth using odeint in Python. In this tutorial we shall be using |
14 the concepts of arrays, functions and lists which we have covered in various |
14 the concepts of arrays, functions and lists which we have covered in |
15 previous tutorials. |
15 previous tutorials. |
16 |
16 |
17 Let's consider the classic problem of the spread of an epidemic in a |
17 Let's consider the classic problem of the spread of an epidemic in a |
18 population. |
18 population. |
19 This is given by the ordinary differential equation dy/dt = ky(L-y) |
19 This is given by the ordinary differential equation dy/dt = ky(L-y) |
20 where L is the total population and k is an arbitrary constant. For our |
20 where L is the total population and k is an arbitrary constant. For our |
21 problem Let us use L=25000, k=0.00003. |
21 problem Let us use L=250000, k=0.00003. |
22 Let the boundary condition be y(0)=250. |
22 Let the boundary condition be y(0)=250. |
23 |
23 |
24 Let's now fire up IPython by typing ipython -pylab interpreter. |
24 Let's now fire up IPython by typing ipython -pylab interpreter. |
25 |
25 |
26 As we saw in one of earlier session, sometimes pylab wont 'import' all |
26 As we saw in one of the earlier sessions, sometimes we will need more than |
27 packages. For solving 'ordinary differential equations' also we shall |
27 pylab to get our job done. For solving 'ordinary differential equations' |
28 have to import 'odeint' function which is a part of the SciPy package. |
28 we shall have to import 'odeint' function, which is a part of the SciPy package. |
29 So we run the magic command: |
29 So we run the magic command: |
30 |
30 |
31 In []: from scipy.integrate import odeint |
31 In []: from scipy.integrate import odeint |
32 |
32 |
33 For now just remember this as a command that does some magic to obtain |
|
34 the function odeint in to our program. |
|
35 The details regarding `import' shall be covered in a subsequent tutorial. |
33 The details regarding `import' shall be covered in a subsequent tutorial. |
36 |
34 |
37 We can represent the given ODE as a Python function. |
35 We can represent the given ODE as a Python function. |
38 This function takes the dependent variable y and the independent variable t |
36 This function takes the dependent variable y and the independent variable t |
39 as arguments and returns the ODE. |
37 as arguments and returns the ODE. |
40 Our function looks like this: |
|
41 (Showing the slide should be sufficient) |
|
42 |
38 |
43 Let us now define our function. |
39 Let us now define our function. |
44 |
40 |
45 In []: def epid(y, t): |
41 In []: def epid(y, t): |
46 .... k = 0.00003 |
42 .... k = 0.00003 |
47 .... L = 25000 |
43 .... L = 250000 |
48 .... return k*y*(L-y) |
44 .... return k*y*(L-y) |
49 |
45 |
50 Independent variable t can be assigned the values in the interval of |
46 Independent variable t can be assigned the values in the interval of |
51 0 and 12 with 61 points using linspace: |
47 0 and 12 with 61 points using linspace: |
52 |
48 |
116 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 |
117 the correct arguments. |
113 the correct arguments. |
118 |
114 |
119 In []: pend_sol = odeint(pend_int, initial,t) |
115 In []: pend_sol = odeint(pend_int, initial,t) |
120 |
116 |
121 In []: plot(pend_sol[0], t) plot theta against t |
117 In []: plot(pend_sol) |
122 In []: plot(pend_sol[1], t) will plot omega against t |
118 This gives us 2 plots. The green plot is omega vs t and the blue is theta vs t. |
123 Plotting theta against t and omega against t we obtain the plots as shown |
|
124 in the slide. |
|
125 |
119 |
126 Thus we come to the end of this tutorial on solving ordinary differential |
120 Thus we come to the end of this tutorial. In this tutorial we have learnt how to solve ordinary |
127 equations in Python. In this tutorial we have learnt how to solve ordinary |
|
128 differential equations of first and second order. |
121 differential equations of first and second order. |
129 |
122 |
130 *** Notes |
123 *** Notes |