day1/cheatsheet6.tex
author Nishanth Amuluru <nishanth@fossee.in>
Tue, 14 Dec 2010 23:15:36 +0530
branchscipyin2010
changeset 456 a27ccfc118fb
parent 438 8af5dfa5432b
permissions -rwxr-xr-x
removed sslc1.txt from circulate since it is not required
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
\documentclass[12pt]{article}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
\title{Solving Equations \& ODEs}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
\author{FOSSEE}
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     4
\usepackage{listings}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     5
\lstset{language=Python,
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     6
    basicstyle=\ttfamily,
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     7
commentstyle=\itshape\bfseries,
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     8
showstringspaces=false,
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
     9
}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    10
\newcommand{\typ}[1]{\lstinline{#1}}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    11
\usepackage[english]{babel}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    12
\usepackage[latin1]{inputenc}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    13
\usepackage{times}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    14
\usepackage[T1]{fontenc}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    15
\usepackage{ae,aecompl}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    16
\usepackage{mathpazo,courier,euler}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    17
\usepackage[scaled=.95]{helvet}
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
    18
\usepackage{amsmath}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
\begin{document}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
\date{}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
\vspace{-1in}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
\begin{center}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
\LARGE{Solving Equations \& ODEs}\\
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
\large{FOSSEE}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
\end{center}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
\section{Solving linear equations}
340
347ff2714deb Minor edits to correct spellings.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 328
diff changeset
    27
Consider following sets of equations:\\
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    28
  \begin{align*}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    29
    3x + 2y - z  & = 1 \\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    30
    2x - 2y + 4z  & = -2 \\
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
    31
    -x + \frac{1}{2}y -z & = 0
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    32
  \end{align*}\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    33
The matrix representation is:\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    34
\begin{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    35
$A*x = B$
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    36
\end{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    37
Where A is coefficient matrix(in this case 3x3)\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    38
B is constant matrix(1x3)\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    39
x is the required solution.\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    40
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    41
In []: A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]])
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    42
In []: B = array([[1], [-2], [0]])
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    43
In []: x = solve(A, B)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    44
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    45
Solve the equation $A x = B$ for $x$.\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    46
To check whether solution is correct try this:
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    47
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    48
In []: Ax = dot(A,x)  #Matrix multiplication of A and x(LHS)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    49
In []: allclose(Ax, B)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    50
Out[]: True
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    51
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    52
\typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. 
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    53
\newpage
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
\section{Finding roots}
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    55
\subsection{Polynomials}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    56
\begin{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    57
  $x^2+6x+13=0$
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    58
\end{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    59
to find roots, pylab provides \typ{roots} function.
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    60
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    61
In []: coeffs = [1, 6, 13] #list of all coefficients
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    62
In []: roots(coeffs)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    63
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    64
\subsection{functions}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    65
Functions can be defined and used by following syntax:
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    66
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    67
def func_name(arg1, arg2):
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    68
    #function code
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    69
    return ret_value
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    70
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    71
A simple example can be
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    72
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    73
def expression(x):
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    74
    y = x*sin(x)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    75
    return y
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    76
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    77
Above function when called with a argument, will return $xsin(x)$ value for that argument.
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    78
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    79
In [95]: expression(pi/2)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    80
Out[95]: 1.5707963267948966
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    81
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    82
In [96]: expression(pi/3)
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    83
Out[96]: 0.90689968211710881
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    84
\end{lstlisting}
330
46533051b9d3 commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents: 328
diff changeset
    85
\subsection{Roots of non-linear equations}
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    86
For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    87
\typ{In []: from scipy.optimize import fsolve}\\
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
    88
\typ{fsolve} is not a part of \typ{pylab}, instead is a function in the \textbf{optimize} module of \textbf{scipy}, and hence we \textbf{import} it.\\
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    89
%\typ{fsolve} takes first argument as name of function, which evaluates $f(x)$, whose roots one wants to find. And second argument is starting estimate, around which roots are found.
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    90
For illustration, we want to find roots of equation:
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    91
\begin{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    92
  $f(x)=sin(x)+cos(x)^2$
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    93
\end{center}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    94
So just like we did above, we define a function:
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    95
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    96
In []: def f(x):
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    97
   ....:        return sin(x)+cos(x)**2
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    98
   ....: 
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
    99
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   100
Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way:
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   101
\begin{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   102
In []: fsolve(f, 0) #arguments are function name and estimate
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   103
Out[]: -0.66623943249251527
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   104
\end{lstlisting}
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   105
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
\section{ODE}
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   107
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   108
We wish to solve an (a system of) Ordinary Differential Equation. For this purpose, we shall use \typ{odeint}.\\
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   109
\typ{In []: from scipy.integrate import odeint}\\
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   110
\typ{odeint} is a function in the \textbf{integrate} module of \textbf{scipy}.\\
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   111
As an illustration, let us solve the ODE below:\\
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   112
$\frac{dy}{dt} = ky(L-y)$, L = 25000, k = 0.00003, y(0) = 250\\
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   113
We define a function (as below) that takes $y$ and time as arguments and returns the right hand side of the ODE.
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   114
\begin{lstlisting}
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   115
In []: def f(y, t):
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
  ....     k, L = 0.00003, 25000
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
  ....     return k*y*(L-y)
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
  ....
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   119
\end{lstlisting}
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   120
Next we define the time over which we wish to solve the ODE. We also note the initial conditions given to us.
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   121
\begin{lstlisting}
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   122
In []: t = linspace(0, 12, 61)
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   123
In []: y0 = 250
317
0eca6c542fce Modified cheat sheet of session 1 and session 6 day 1.
Shantanu <shantanu@fossee.in>
parents: 295
diff changeset
   124
\end{lstlisting}
323
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   125
To solve the ODE, we call the \typ{odeint} function with three arguments - the function \typ{f}, initial conditions and the time vector. 
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   126
\begin{lstlisting}
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   127
In []: y = odeint(f, y0, t)
4e44d7741c94 Added ODE section to cheatsheet6, day1.
Puneeth Chaganti <punchagan@fossee.in>
parents: 317
diff changeset
   128
\end{lstlisting}
438
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   129
Note: To solve a system of ODEs, we need to change the function to
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   130
return the right hand side of all the equations and the system and the
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   131
pass the required number of initial conditions to the \typ{odeint}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   132
function.
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   133
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   134
\section{FFT}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   135
\begin{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   136
    \item We have a simple signal $y(t)$
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   137
    \item Find the FFT and plot it
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   138
\end{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   139
\begin{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   140
In []: t = linspace(0, 2*pi, 500)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   141
In []: y = sin(4*pi*t)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   142
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   143
In []: f = fft(y)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   144
In []: freq = fftfreq(500, t[1] - t[0])
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   145
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   146
In []: plot(freq[:250], abs(f)[:250])
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   147
In []: grid()
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   148
\end{lstlisting} 
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   149
\begin{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   150
    \item We now calculate the inverse Fourier transform.
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   151
    \item Then, verify the solution obtained. 
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   152
\end{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   153
\begin{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   154
In []: y1 = ifft(f) # inverse FFT
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   155
In []: allclose(y, y1)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   156
Out[]: True
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   157
\end{lstlisting} 
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   158
\begin{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   159
\item Let us add some noise to the signal
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   160
\end{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   161
\begin{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   162
In []: yr = y + random(size=500)*0.2
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   163
In []: yn = y + normal(size=500)*0.2
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   164
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   165
In []: plot(t, yr)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   166
In []: figure()
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   167
In []: plot(freq[:250],
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   168
  ...:      abs(fft(yn))[:250])
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   169
\end{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   170
\begin{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   171
    \item \typ{random}: produces uniform deviates in $[0, 1)$
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   172
    \item \typ{normal}: draws random samples from a Gaussian
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   173
        distribution
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   174
    \item Useful to create a random matrix of any shape
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   175
\end{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   176
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   177
\begin{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   178
\item Now, we filter the noisy signal using a Wiener filter
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   179
\end{itemize}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   180
\begin{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   181
In []: from scipy import signal
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   182
In []: yc = signal.wiener(yn, 5)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   183
In []: clf()
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   184
In []: plot(t, yc)
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   185
In []: figure()
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   186
In []: plot(freq[:250], 
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   187
  ...:      abs(fft(yc))[:250])
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   188
\end{lstlisting}
8af5dfa5432b Added FFT stuff to day1/cheatsheet6.
Puneeth Chaganti <punchagan@fossee.in>
parents: 341
diff changeset
   189
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 317
diff changeset
   190
\section{Links and References}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 317
diff changeset
   191
\begin{itemize}
328
4075482a9770 Minor edits to remove errors.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 324
diff changeset
   192
\item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/
4075482a9770 Minor edits to remove errors.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 324
diff changeset
   193
  \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \\ http://www.scipy.org/Cookbook/
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 317
diff changeset
   194
\end{itemize}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   195
\end{document}