day1/session4.tex
changeset 201 c81ebcd9e397
parent 197 8c42ffdaaeec
child 203 5c0332b97ed6
equal deleted inserted replaced
198:4cb13665b3b6 201:c81ebcd9e397
    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}
   243         [ -2.00000000e+00],
   243         [ -2.00000000e+00],
   244         [  2.22044605e-16]])
   244         [  2.22044605e-16]])
   245 \end{lstlisting}
   245 \end{lstlisting}
   246 \end{frame}
   246 \end{frame}
   247 
   247 
   248 \section{Integration}
       
   249 
       
   250 \subsection{Quadrature}
       
   251 
       
   252 \begin{frame}[fragile]
       
   253 \frametitle{Quadrature}
       
   254 \begin{itemize}
       
   255 \item We wish to find area under a curve
       
   256 \item Area under $(sin(x) + x^2)$ in $(0,1)$
       
   257 \item scipy has functions to do that
       
   258 \end{itemize}
       
   259 \small{\typ{In []: from scipy.integrate import quad}}
       
   260 \begin{itemize}
       
   261 \item Inputs - function to integrate, limits
       
   262 \end{itemize}
       
   263 \begin{lstlisting}
       
   264 In []: x = 0
       
   265 In []: quad(sin(x)+x**2, 0, 1)
       
   266 \end{lstlisting}
       
   267 \alert{\typ{error:}}
       
   268 \typ{First argument must be a callable function.}
       
   269 \end{frame}
       
   270 
       
   271 \begin{frame}[fragile]
       
   272 \frametitle{Functions - Definition}
       
   273 \begin{lstlisting}
       
   274 In []: def f(x):
       
   275            return sin(x)+x**2
       
   276 In []: quad(f, 0, 1)
       
   277 \end{lstlisting}
       
   278 \begin{itemize}
       
   279 \item \typ{def}
       
   280 \item arguments
       
   281 \item \typ{return}
       
   282 \end{itemize}
       
   283 \end{frame}
       
   284 
       
   285 \begin{frame}[fragile]
       
   286 \frametitle{Functions - Calling them}
       
   287 \begin{lstlisting}
       
   288 In [15]: f()
       
   289 ---------------------------------------
       
   290 \end{lstlisting}
       
   291 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
       
   292 \typ{(0 given)}
       
   293 \begin{lstlisting}
       
   294 In []: f(0)
       
   295 Out[]: 0.0
       
   296 In []: f(1)
       
   297 Out[]: 1.8414709848078965
       
   298 \end{lstlisting}
       
   299 \end{frame}
       
   300 
       
   301 
       
   302 \begin{frame}[fragile]
       
   303 \frametitle{Functions - Default Arguments}
       
   304 \begin{lstlisting}
       
   305 In []: def f(x=1):
       
   306            return sin(x)+x**2
       
   307 In []: f(10)
       
   308 Out[]: 99.455978889110625
       
   309 In []: f(1)
       
   310 Out[]: 1.8414709848078965
       
   311 In []: f()
       
   312 Out[]: 1.8414709848078965
       
   313 \end{lstlisting}
       
   314 \end{frame}
       
   315 
       
   316 \begin{frame}[fragile]
       
   317 \frametitle{Functions - Keyword Arguments}
       
   318 \begin{lstlisting}
       
   319 In []: def f(x=1, y=pi):
       
   320            return sin(y)+x**2
       
   321 In []: f()
       
   322 Out[]: 1.0000000000000002
       
   323 In []: f(2)
       
   324 Out[]: 4.0
       
   325 In []: f(y=2)
       
   326 Out[]: 1.9092974268256817
       
   327 In []: f(y=pi/2,x=0)
       
   328 Out[]: 1.0
       
   329 \end{lstlisting}
       
   330 \end{frame}
       
   331 
       
   332 \begin{frame}[fragile]
       
   333   \frametitle{More on functions}
       
   334   \begin{itemize}
       
   335   \item Scope of variables in the function is local
       
   336   \item Mutable items are \alert{passed by reference}
       
   337   \item First line after definition may be a documentation string
       
   338     (\alert{recommended!})
       
   339   \item Function definition and execution defines a name bound to the
       
   340     function
       
   341   \item You \emph{can} assign a variable to a function!
       
   342   \end{itemize}
       
   343 \end{frame}
       
   344 
       
   345 \begin{frame}[fragile]
       
   346 \frametitle{Quadrature \ldots}
       
   347 \begin{lstlisting}
       
   348 In []: quad(f, 0, 1)
       
   349 \end{lstlisting}
       
   350 Returns the integral and an estimate of the absolute error in the result.
       
   351 \begin{itemize}
       
   352 \item Use \typ{dblquad} for Double integrals
       
   353 \item Use \typ{tplquad} for Triple integrals
       
   354 \end{itemize}
       
   355 \end{frame}
       
   356 
       
   357 \subsection{ODEs}
       
   358 
       
   359 \begin{frame}[fragile]
       
   360 \frametitle{ODE Integration}
       
   361 We shall use the simple ODE of a simple pendulum. 
       
   362 \begin{equation*}
       
   363 \ddot{\theta} = -\frac{g}{L}sin(\theta)
       
   364 \end{equation*}
       
   365 \begin{itemize}
       
   366 \item This equation can be written as a system of two first order ODEs
       
   367 \end{itemize}
       
   368 \begin{align}
       
   369 \dot{\theta} &= \omega \\
       
   370 \dot{\omega} &= -\frac{g}{L}sin(\theta) \\
       
   371  \text{At}\ t &= 0 : \nonumber \\
       
   372  \theta = \theta_0\quad & \&\quad  \omega = 0 \nonumber
       
   373 \end{align}
       
   374 \end{frame}
       
   375 
       
   376 \begin{frame}[fragile]
       
   377 \frametitle{Solving ODEs using SciPy}
       
   378 \begin{itemize}
       
   379 \item We use the \typ{odeint} function from scipy to do the integration
       
   380 \item Define a function as below
       
   381 \end{itemize}
       
   382 \begin{lstlisting}
       
   383 In []: def pend_int(unknown, t, p):
       
   384   ....     theta, omega = unknown
       
   385   ....     g, L = p
       
   386   ....     f=[omega, -(g/L)*sin(theta)]
       
   387   ....     return f
       
   388   ....
       
   389 \end{lstlisting}
       
   390 \end{frame}
       
   391 
       
   392 \begin{frame}[fragile]
       
   393 \frametitle{Solving ODEs using SciPy \ldots}
       
   394 \begin{itemize}
       
   395 \item \typ{t} is the time variable \\ 
       
   396 \item \typ{p} has the constants \\
       
   397 \item \typ{initial} has the initial values
       
   398 \end{itemize}
       
   399 \begin{lstlisting}
       
   400 In []: t = linspace(0, 10, 101)
       
   401 In []: p=(-9.81, 0.2)
       
   402 In []: initial = [10*2*pi/360, 0]
       
   403 \end{lstlisting}
       
   404 \end{frame}
       
   405 
       
   406 \begin{frame}[fragile]
       
   407 \frametitle{Solving ODEs using SciPy \ldots}
       
   408 
       
   409 \small{\typ{In []: from scipy.integrate import odeint}}
       
   410 \begin{lstlisting}
       
   411 In []: pend_sol = odeint(pend_int, 
       
   412                          initial,t, 
       
   413                          args=(p,))
       
   414 \end{lstlisting}
       
   415 \end{frame}
       
   416 
       
   417 \begin{frame}
   248 \begin{frame}
   418   \frametitle{Things we have learned}
   249   \frametitle{Things we have learned}
   419   \begin{itemize}
   250   \begin{itemize}
   420   \item
   251   \item
   421   \item
   252   \item
   422   \item Functions
       
   423     \begin{itemize}
       
   424     \item Definition
       
   425     \item Calling
       
   426     \item Default Arguments
       
   427     \item Keyword Arguments
       
   428     \end{itemize}
       
   429     \item Integration
       
   430     \begin{itemize}
       
   431       \item Quadrature
       
   432       \item ODEs
       
   433     \end{itemize}
       
   434   \end{itemize}
   253   \end{itemize}
   435 \end{frame}
   254 \end{frame}
   436 
   255 
   437 \end{document}
   256 \end{document}
   438 
   257