day1/session4.tex
changeset 205 bba40c856f68
parent 203 5c0332b97ed6
child 213 ce62706cf870
equal deleted inserted replaced
204:87f914f38ba1 205:bba40c856f68
    72 % }
    72 % }
    73 
    73 
    74 
    74 
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    76 % Title page
    76 % Title page
    77 \title[Basic Python]{Matrices, Solution of equations and Integration\\}
    77 \title[Basic Python]{Matrices, Solution of equations}
    78 
    78 
    79 \author[FOSSEE] {FOSSEE}
    79 \author[FOSSEE] {FOSSEE}
    80 
    80 
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    82 \date[] {31, October 2009\\Day 1, Session 4}
    82 \date[] {31, October 2009\\Day 1, Session 4}
   122   \frametitle{Outline}
   122   \frametitle{Outline}
   123   \tableofcontents
   123   \tableofcontents
   124 %  \pausesections
   124 %  \pausesections
   125 \end{frame}
   125 \end{frame}
   126 
   126 
       
   127 \section{Matrices}
       
   128 
       
   129 \begin{frame}
       
   130 \frametitle{Matrices: Introduction}
       
   131 We looked at the Van der Monde matrix in the previous session,\\ 
       
   132 let us now look at matrices in a little more detail.
       
   133 \end{frame}
       
   134 
       
   135 \subsection{Initializing}
       
   136 \begin{frame}[fragile]
       
   137 \frametitle{Matrices: Initializing}
       
   138 \begin{lstlisting}
       
   139   In []: a = matrix([[1,2,3],
       
   140                      [4,5,6],
       
   141                      [7,8,9]])
       
   142 
       
   143   In []: a
       
   144   Out[]: 
       
   145   matrix([[1, 2, 3],
       
   146          [4, 5, 6],
       
   147          [7, 8, 9]])
       
   148 \end{lstlisting}
       
   149 \end{frame}
       
   150 
       
   151 \subsection{Basic Operations}
       
   152 \begin{frame}[fragile]
       
   153 \frametitle{Inverse of a Matrix}
       
   154 
       
   155 \begin{small}
       
   156 \begin{lstlisting}
       
   157 In []: linalg.inv(A)
       
   158 Out[]: 
       
   159 matrix([[ 0.07734807,  0.01657459,  0.32044199],
       
   160         [ 0.09944751, -0.12154696, -0.01657459],
       
   161         [-0.02762431, -0.07734807,  0.17127072]])
       
   162 
       
   163 \end{lstlisting}
       
   164 \end{small}
       
   165 \end{frame}
       
   166 
       
   167 \begin{frame}[fragile]
       
   168 \frametitle{Determinant}
       
   169 \begin{lstlisting}
       
   170   In []: linalg.det(a)
       
   171   Out[]: -9.5171266700777579e-16
       
   172 \end{lstlisting}
       
   173 \end{frame}
       
   174 
       
   175 \begin{frame}[fragile]
       
   176 \frametitle{Computing Norms}
       
   177 \begin{lstlisting}
       
   178   In []: linalg.norm(a)
       
   179   Out[]: 16.881943016134134
       
   180 \end{lstlisting}
       
   181 \end{frame}
       
   182 
       
   183 \begin{frame}[fragile]
       
   184 \frametitle{Eigen Values and Eigen Matrix}
       
   185 \begin{small}
       
   186 \begin{lstlisting}
       
   187   In []: linalg.eigvals(a)
       
   188   Out[]: array([1.61168440e+01, -1.11684397e+00, -1.22196337e-15])
       
   189 
       
   190   In []: linalg.eig(a)
       
   191   Out[]: 
       
   192   (array([ 1.61168440e+01, -1.11684397e+00, -1.22196337e-15]),
       
   193    matrix([[-0.23197069, -0.78583024,  0.40824829],
       
   194           [-0.52532209, -0.08675134, -0.81649658],
       
   195           [-0.8186735 ,  0.61232756,  0.40824829]]))
       
   196 \end{lstlisting}
       
   197 \end{small}
       
   198 \end{frame}
       
   199 
   127 \section{Solving linear equations}
   200 \section{Solving linear equations}
       
   201 
   128 \begin{frame}[fragile]
   202 \begin{frame}[fragile]
   129 \frametitle{Solution of equations}
   203 \frametitle{Solution of equations}
   130 Consider,
   204 Consider,
   131   \begin{align*}
   205   \begin{align*}
   132     3x + 2y - z  & = 1 \\
   206     3x + 2y - z  & = 1 \\
   169         [ -2.00000000e+00],
   243         [ -2.00000000e+00],
   170         [  2.22044605e-16]])
   244         [  2.22044605e-16]])
   171 \end{lstlisting}
   245 \end{lstlisting}
   172 \end{frame}
   246 \end{frame}
   173 
   247 
   174 \section{Matrices}
       
   175 \subsection{Initializing}
       
   176 \begin{frame}[fragile]
       
   177 \frametitle{Matrices: Initializing}
       
   178 \begin{lstlisting}
       
   179   In []: a = matrix([[1,2,3],
       
   180                      [4,5,6],
       
   181                      [7,8,9]])
       
   182 
       
   183   In []: a
       
   184   Out[]: 
       
   185   matrix([[1, 2, 3],
       
   186          [4, 5, 6],
       
   187          [7, 8, 9]])
       
   188 \end{lstlisting}
       
   189 \end{frame}
       
   190 
       
   191 \subsection{Basic Operations}
       
   192 \begin{frame}[fragile]
       
   193 \frametitle{Inverse of a Matrix}
       
   194 
       
   195 \begin{small}
       
   196 \begin{lstlisting}
       
   197 In []: linalg.inv(A)
       
   198 Out[]: 
       
   199 matrix([[ 0.07734807,  0.01657459,  0.32044199],
       
   200         [ 0.09944751, -0.12154696, -0.01657459],
       
   201         [-0.02762431, -0.07734807,  0.17127072]])
       
   202 
       
   203 \end{lstlisting}
       
   204 \end{small}
       
   205 \end{frame}
       
   206 
       
   207 \begin{frame}[fragile]
       
   208 \frametitle{Determinant}
       
   209 \begin{lstlisting}
       
   210   In []: linalg.det(a)
       
   211   Out[]: -9.5171266700777579e-16
       
   212 \end{lstlisting}
       
   213 \end{frame}
       
   214 
       
   215 \begin{frame}[fragile]
       
   216 \frametitle{Computing Norms}
       
   217 \begin{lstlisting}
       
   218   In []: linalg.norm(a)
       
   219   Out[]: 16.881943016134134
       
   220 \end{lstlisting}
       
   221 \end{frame}
       
   222 
       
   223 \begin{frame}[fragile]
       
   224 \frametitle{Eigen Values and Eigen Matrix}
       
   225 \begin{small}
       
   226 \begin{lstlisting}
       
   227   In []: linalg.eigvals(a)
       
   228   Out[]: array([1.61168440e+01, -1.11684397e+00, -1.22196337e-15])
       
   229 
       
   230   In []: linalg.eig(a)
       
   231   Out[]: 
       
   232   (array([ 1.61168440e+01, -1.11684397e+00, -1.22196337e-15]),
       
   233    matrix([[-0.23197069, -0.78583024,  0.40824829],
       
   234           [-0.52532209, -0.08675134, -0.81649658],
       
   235           [-0.8186735 ,  0.61232756,  0.40824829]]))
       
   236 \end{lstlisting}
       
   237 \end{small}
       
   238 \end{frame}
       
   239 
       
   240 
       
   241 \section{Integration}
       
   242 
       
   243 \subsection{Quadrature}
       
   244 
       
   245 \begin{frame}[fragile]
       
   246 \frametitle{Quadrature}
       
   247 \begin{itemize}
       
   248 \item We wish to find area under a curve
       
   249 \item Area under $(sin(x) + x^2)$ in $(0,1)$
       
   250 \item scipy has functions to do that
       
   251 \end{itemize}
       
   252 \small{\typ{In []: from scipy.integrate import quad}}
       
   253 \begin{itemize}
       
   254 \item Inputs - function to integrate, limits
       
   255 \end{itemize}
       
   256 \begin{lstlisting}
       
   257 In []: x = 0
       
   258 In []: quad(sin(x)+x**2, 0, 1)
       
   259 \end{lstlisting}
       
   260 \alert{\typ{error:}}
       
   261 \typ{First argument must be a callable function.}
       
   262 \end{frame}
       
   263 
       
   264 \begin{frame}[fragile]
       
   265 \frametitle{Functions - Definition}
       
   266 \begin{lstlisting}
       
   267 In []: def f(x):
       
   268            return sin(x)+x**2
       
   269 In []: quad(f, 0, 1)
       
   270 \end{lstlisting}
       
   271 \begin{itemize}
       
   272 \item \typ{def}
       
   273 \item arguments
       
   274 \item \typ{return}
       
   275 \end{itemize}
       
   276 \end{frame}
       
   277 
       
   278 \begin{frame}[fragile]
       
   279 \frametitle{Functions - Calling them}
       
   280 \begin{lstlisting}
       
   281 In [15]: f()
       
   282 ---------------------------------------
       
   283 \end{lstlisting}
       
   284 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
       
   285 \typ{(0 given)}
       
   286 \begin{lstlisting}
       
   287 In []: f(0)
       
   288 Out[]: 0.0
       
   289 In []: f(1)
       
   290 Out[]: 1.8414709848078965
       
   291 \end{lstlisting}
       
   292 \end{frame}
       
   293 
       
   294 
       
   295 \begin{frame}[fragile]
       
   296 \frametitle{Functions - Default Arguments}
       
   297 \begin{lstlisting}
       
   298 In []: def f(x=1):
       
   299            return sin(x)+x**2
       
   300 In []: f(10)
       
   301 Out[]: 99.455978889110625
       
   302 In []: f(1)
       
   303 Out[]: 1.8414709848078965
       
   304 In []: f()
       
   305 Out[]: 1.8414709848078965
       
   306 \end{lstlisting}
       
   307 \end{frame}
       
   308 
       
   309 \begin{frame}[fragile]
       
   310 \frametitle{Functions - Keyword Arguments}
       
   311 \begin{lstlisting}
       
   312 In []: def f(x=1, y=pi):
       
   313            return sin(y)+x**2
       
   314 In []: f()
       
   315 Out[]: 1.0000000000000002
       
   316 In []: f(2)
       
   317 Out[]: 4.0
       
   318 In []: f(y=2)
       
   319 Out[]: 1.9092974268256817
       
   320 In []: f(y=pi/2,x=0)
       
   321 Out[]: 1.0
       
   322 \end{lstlisting}
       
   323 \end{frame}
       
   324 
       
   325 \begin{frame}[fragile]
       
   326   \frametitle{More on functions}
       
   327   \begin{itemize}
       
   328   \item Scope of variables in the function is local
       
   329   \item Mutable items are \alert{passed by reference}
       
   330   \item First line after definition may be a documentation string
       
   331     (\alert{recommended!})
       
   332   \item Function definition and execution defines a name bound to the
       
   333     function
       
   334   \item You \emph{can} assign a variable to a function!
       
   335   \end{itemize}
       
   336 \end{frame}
       
   337 
       
   338 \begin{frame}[fragile]
       
   339 \frametitle{Quadrature \ldots}
       
   340 \begin{lstlisting}
       
   341 In []: quad(f, 0, 1)
       
   342 \end{lstlisting}
       
   343 Returns the integral and an estimate of the absolute error in the result.
       
   344 \begin{itemize}
       
   345 \item Use \typ{dblquad} for Double integrals
       
   346 \item Use \typ{tplquad} for Triple integrals
       
   347 \end{itemize}
       
   348 \end{frame}
       
   349 
       
   350 \subsection{ODEs}
       
   351 
       
   352 \begin{frame}[fragile]
       
   353 \frametitle{ODE Integration}
       
   354 We shall use the simple ODE of a simple pendulum. 
       
   355 \begin{equation*}
       
   356 \ddot{\theta} = -\frac{g}{L}sin(\theta)
       
   357 \end{equation*}
       
   358 \begin{itemize}
       
   359 \item This equation can be written as a system of two first order ODEs
       
   360 \end{itemize}
       
   361 \begin{align}
       
   362 \dot{\theta} &= \omega \\
       
   363 \dot{\omega} &= -\frac{g}{L}sin(\theta) \\
       
   364  \text{At}\ t &= 0 : \nonumber \\
       
   365  \theta = \theta_0\quad & \&\quad  \omega = 0 \nonumber
       
   366 \end{align}
       
   367 \end{frame}
       
   368 
       
   369 \begin{frame}[fragile]
       
   370 \frametitle{Solving ODEs using SciPy}
       
   371 \begin{itemize}
       
   372 \item We use the \typ{odeint} function from scipy to do the integration
       
   373 \item Define a function as below
       
   374 \end{itemize}
       
   375 \begin{lstlisting}
       
   376 In []: def pend_int(unknown, t, p):
       
   377   ....     theta, omega = unknown
       
   378   ....     g, L = p
       
   379   ....     f=[omega, -(g/L)*sin(theta)]
       
   380   ....     return f
       
   381   ....
       
   382 \end{lstlisting}
       
   383 \end{frame}
       
   384 
       
   385 \begin{frame}[fragile]
       
   386 \frametitle{Solving ODEs using SciPy \ldots}
       
   387 \begin{itemize}
       
   388 \item \typ{t} is the time variable \\ 
       
   389 \item \typ{p} has the constants \\
       
   390 \item \typ{initial} has the initial values
       
   391 \end{itemize}
       
   392 \begin{lstlisting}
       
   393 In []: t = linspace(0, 10, 101)
       
   394 In []: p=(-9.81, 0.2)
       
   395 In []: initial = [10*2*pi/360, 0]
       
   396 \end{lstlisting}
       
   397 \end{frame}
       
   398 
       
   399 \begin{frame}[fragile]
       
   400 \frametitle{Solving ODEs using SciPy \ldots}
       
   401 
       
   402 \small{\typ{In []: from scipy.integrate import odeint}}
       
   403 \begin{lstlisting}
       
   404 In []: pend_sol = odeint(pend_int, 
       
   405                          initial,t, 
       
   406                          args=(p,))
       
   407 \end{lstlisting}
       
   408 \end{frame}
       
   409 
       
   410 \begin{frame}
   248 \begin{frame}
   411   \frametitle{Things we have learned}
   249   \frametitle{Things we have learned}
   412   \begin{itemize}
   250   \begin{itemize}
   413   \item
   251   \item
   414   \item
   252   \item
   415   \item Functions
       
   416     \begin{itemize}
       
   417     \item Definition
       
   418     \item Calling
       
   419     \item Default Arguments
       
   420     \item Keyword Arguments
       
   421     \end{itemize}
       
   422     \item Integration
       
   423     \begin{itemize}
       
   424       \item Quadrature
       
   425       \item ODEs
       
   426     \end{itemize}
       
   427   \end{itemize}
   253   \end{itemize}
   428 \end{frame}
   254 \end{frame}
   429 
   255 
   430 \end{document}
   256 \end{document}
   431 
   257