author | Shantanu <shantanu@fossee.in> |
Wed, 28 Apr 2010 16:33:18 +0530 | |
changeset 115 | d35eccbf206d |
parent 88 | cc4f615f3f8c |
child 116 | 8b650688f4e1 |
permissions | -rw-r--r-- |
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
1 |
* Solving ODEs |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
2 |
*** Outline |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
3 |
***** Introduction |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
4 |
******* What are we going to do? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
5 |
******* How are we going to do? |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
6 |
******* Arsenal Required |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
7 |
********* working knowledge of arrays |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
8 |
********* working knowledge of functions |
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
9 |
*** Script |
86 | 10 |
Welcome friends. |
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
11 |
|
86 | 12 |
In this tutorial we shall look at solving Ordinary Differential Equations, |
88 | 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 |
|
15 |
previous tutorials. |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
16 |
|
86 | 17 |
Let's consider the classic problem of the spread of an epidemic in a |
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
18 |
population. |
86 | 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 |
|
21 |
problem Let us use L=25000, k=0.00003. |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
22 |
Let the boundary condition be y(0)=250. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
23 |
|
88 | 24 |
Let's now fire up IPython by typing ipython -pylab interpreter. |
84 | 25 |
|
86 | 26 |
As we saw in one of earlier session, sometimes pylab wont 'import' all |
84 | 27 |
packages. For solving 'ordinary differential equations' also we shall |
86 | 28 |
have to import 'odeint' function which is a part of the SciPy package. |
29 |
So we run the magic command: |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
30 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
31 |
In []: from scipy.integrate import odeint |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
32 |
|
86 | 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. |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
36 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
37 |
We can represent the given ODE as a Python function. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
38 |
This function takes the dependent variable y and the independent variable t |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
39 |
as arguments and returns the ODE. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
40 |
Our function looks like this: |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
41 |
(Showing the slide should be sufficient) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
42 |
|
86 | 43 |
Let us now define our function. |
44 |
||
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
45 |
In []: def epid(y, t): |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
46 |
.... k = 0.00003 |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
47 |
.... L = 25000 |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
48 |
.... return k*y*(L-y) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
49 |
|
86 | 50 |
Independent variable t can be assigned the values in the interval of |
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
51 |
0 and 12 with 61 points using linspace: |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
52 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
53 |
In []: t = linspace(0, 12, 61) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
54 |
|
86 | 55 |
Now obtaining the solution of the ode we defined, is as simple as |
56 |
calling the Python's odeint function which we just imported: |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
57 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
58 |
In []: y = odeint(epid, 250, t) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
59 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
60 |
We can plot the the values of y against t to get a graphical picture our ODE. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
61 |
|
84 | 62 |
plot(y, t) |
86 | 63 |
Lets now close this plot and move on to solving ordinary differential equation of |
84 | 64 |
second order. |
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
65 |
Here we shall take the example ODEs of a simple pendulum. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
66 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
67 |
The equations can be written as a system of two first order ODEs |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
68 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
69 |
d(theta)/dt = omega |
84 | 70 |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
71 |
and |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
72 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
73 |
d(omega)/dt = - g/L sin(theta) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
74 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
75 |
Let us define the boundary conditions as: at t = 0, |
84 | 76 |
theta = theta naught = 10 degrees and |
77 |
omega = 0 |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
78 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
79 |
Let us first define our system of equations as a Python function, pend_int. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
80 |
As in the earlier case of single ODE we shall use odeint function of Python |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
81 |
to solve this system of equations by passing pend_int to odeint. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
82 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
83 |
pend_int is defined as shown: |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
84 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
85 |
In []: def pend_int(initial, t): |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
86 |
.... theta = initial[0] |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
87 |
.... omega = initial[1] |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
88 |
.... g = 9.81 |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
89 |
.... L = 0.2 |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
90 |
.... f=[omega, -(g/L)*sin(theta)] |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
91 |
.... return f |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
92 |
.... |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
93 |
|
84 | 94 |
It takes two arguments. The first argument itself containing two |
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
95 |
dependent variables in the system, theta and omega. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
96 |
The second argument is the independent variable t. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
97 |
|
86 | 98 |
In the function we assign the first and second values of the |
99 |
initial argument to theta and omega respectively. |
|
100 |
Acceleration due to gravity, as we know is 9.81 meter per second sqaure. |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
101 |
Let the length of the the pendulum be 0.2 meter. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
102 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
103 |
We create a list, f, of two equations which corresponds to our two ODEs, |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
104 |
that is d(theta)/dt = omega and d(omega)/dt = - g/L sin(theta). |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
105 |
We return this list of equations f. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
106 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
107 |
Now we can create a set of values for our time variable t over which we need |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
108 |
to integrate our system of ODEs. Let us say, |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
109 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
110 |
In []: t = linspace(0, 20, 101) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
111 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
112 |
We shall assign the boundary conditions to the variable initial. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
113 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
114 |
In []: initial = [10*2*pi/360, 0] |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
115 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
116 |
Now solving this system is just a matter of calling the odeint function with |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
117 |
the correct arguments. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
118 |
|
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
119 |
In []: pend_sol = odeint(pend_int, initial,t) |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
120 |
|
84 | 121 |
In []: plot(pend_sol[0], t) plot theta against t |
122 |
In []: plot(pend_sol[1], t) will plot omega against t |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
123 |
Plotting theta against t and omega against t we obtain the plots as shown |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
124 |
in the slide. |
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
125 |
|
86 | 126 |
Thus we come to the end of this tutorial on solving ordinary differential |
88 | 127 |
equations in Python. In this tutorial we have learnt how to solve ordinary |
128 |
differential equations of first and second order. |
|
14
4182c6c7e1c6
Added script on ODEs, session 6 of our workshop.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2
diff
changeset
|
129 |
|
2
008c0edc6eac
Added scripts for session-4 and session-6 of day-1.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff
changeset
|
130 |
*** Notes |