day1/session5.tex
changeset 202 2b902f2d25e5
child 221 9ed9539446bc
equal deleted inserted replaced
201:c81ebcd9e397 202:2b902f2d25e5
       
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     2 %Tutorial slides on Python.
       
     3 %
       
     4 % Author: FOSSEE 
       
     5 % Copyright (c) 2009, FOSSEE, IIT Bombay
       
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     7 
       
     8 \documentclass[14pt,compress]{beamer}
       
     9 %\documentclass[draft]{beamer}
       
    10 %\documentclass[compress,handout]{beamer}
       
    11 %\usepackage{pgfpages} 
       
    12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
       
    13 
       
    14 % Modified from: generic-ornate-15min-45min.de.tex
       
    15 \mode<presentation>
       
    16 {
       
    17   \usetheme{Warsaw}
       
    18   \useoutertheme{split}
       
    19   \setbeamercovered{transparent}
       
    20 }
       
    21 
       
    22 \usepackage[english]{babel}
       
    23 \usepackage[latin1]{inputenc}
       
    24 %\usepackage{times}
       
    25 \usepackage[T1]{fontenc}
       
    26 \usepackage{amsmath}
       
    27 
       
    28 % Taken from Fernando's slides.
       
    29 \usepackage{ae,aecompl}
       
    30 \usepackage{mathpazo,courier,euler}
       
    31 \usepackage[scaled=.95]{helvet}
       
    32 
       
    33 \definecolor{darkgreen}{rgb}{0,0.5,0}
       
    34 
       
    35 \usepackage{listings}
       
    36 \lstset{language=Python,
       
    37     basicstyle=\ttfamily\bfseries,
       
    38     commentstyle=\color{red}\itshape,
       
    39   stringstyle=\color{darkgreen},
       
    40   showstringspaces=false,
       
    41   keywordstyle=\color{blue}\bfseries}
       
    42 
       
    43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    44 % Macros
       
    45 \setbeamercolor{emphbar}{bg=blue!20, fg=black}
       
    46 \newcommand{\emphbar}[1]
       
    47 {\begin{beamercolorbox}[rounded=true]{emphbar} 
       
    48       {#1}
       
    49  \end{beamercolorbox}
       
    50 }
       
    51 \newcounter{time}
       
    52 \setcounter{time}{0}
       
    53 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
       
    54 
       
    55 \newcommand{\typ}[1]{\lstinline{#1}}
       
    56 
       
    57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
       
    58 
       
    59 %%% This is from Fernando's setup.
       
    60 % \usepackage{color}
       
    61 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
       
    62 % % Use and configure listings package for nicely formatted code
       
    63 % \usepackage{listings}
       
    64 % \lstset{
       
    65 %    language=Python,
       
    66 %    basicstyle=\small\ttfamily,
       
    67 %    commentstyle=\ttfamily\color{blue},
       
    68 %    stringstyle=\ttfamily\color{orange},
       
    69 %    showstringspaces=false,
       
    70 %    breaklines=true,
       
    71 %    postbreak = \space\dots
       
    72 % }
       
    73 
       
    74 
       
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    76 % Title page
       
    77 \title[Basic Python]{Interpolation, Differentiation and Integration}
       
    78 
       
    79 \author[FOSSEE] {FOSSEE}
       
    80 
       
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
       
    82 \date[] {31, October 2009\\Day 1, Session 4}
       
    83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    84 
       
    85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
       
    86 %\logo{\pgfuseimage{iitmlogo}}
       
    87 
       
    88 
       
    89 %% Delete this, if you do not want the table of contents to pop up at
       
    90 %% the beginning of each subsection:
       
    91 \AtBeginSubsection[]
       
    92 {
       
    93   \begin{frame}<beamer>
       
    94     \frametitle{Outline}
       
    95     \tableofcontents[currentsection,currentsubsection]
       
    96   \end{frame}
       
    97 }
       
    98 
       
    99 %%\AtBeginSection[]
       
   100 %%{
       
   101   %%\begin{frame}<beamer>
       
   102 %%    \frametitle{Outline}
       
   103   %%  \tableofcontents[currentsection,currentsubsection]
       
   104   %%\end{frame}
       
   105 %%}
       
   106 
       
   107 % If you wish to uncover everything in a step-wise fashion, uncomment
       
   108 % the following command: 
       
   109 %\beamerdefaultoverlayspecification{<+->}
       
   110 
       
   111 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
       
   112 
       
   113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   114 % DOCUMENT STARTS
       
   115 \begin{document}
       
   116 
       
   117 \begin{frame}
       
   118   \titlepage
       
   119 \end{frame}
       
   120 
       
   121 \begin{frame}
       
   122   \frametitle{Outline}
       
   123   \tableofcontents
       
   124 %  \pausesections
       
   125 \end{frame}
       
   126 
       
   127 \section{Integration}
       
   128 
       
   129 \subsection{Quadrature}
       
   130 
       
   131 \begin{frame}[fragile]
       
   132 \frametitle{Quadrature}
       
   133 \begin{itemize}
       
   134 \item We wish to find area under a curve
       
   135 \item Area under $(sin(x) + x^2)$ in $(0,1)$
       
   136 \item scipy has functions to do that
       
   137 \end{itemize}
       
   138 \small{\typ{In []: from scipy.integrate import quad}}
       
   139 \begin{itemize}
       
   140 \item Inputs - function to integrate, limits
       
   141 \end{itemize}
       
   142 \begin{lstlisting}
       
   143 In []: x = 0
       
   144 In []: quad(sin(x)+x**2, 0, 1)
       
   145 \end{lstlisting}
       
   146 \alert{\typ{error:}}
       
   147 \typ{First argument must be a callable function.}
       
   148 \end{frame}
       
   149 
       
   150 \begin{frame}[fragile]
       
   151 \frametitle{Functions - Definition}
       
   152 \begin{lstlisting}
       
   153 In []: def f(x):
       
   154            return sin(x)+x**2
       
   155 In []: quad(f, 0, 1)
       
   156 \end{lstlisting}
       
   157 \begin{itemize}
       
   158 \item \typ{def}
       
   159 \item arguments
       
   160 \item \typ{return}
       
   161 \end{itemize}
       
   162 \end{frame}
       
   163 
       
   164 \begin{frame}[fragile]
       
   165 \frametitle{Functions - Calling them}
       
   166 \begin{lstlisting}
       
   167 In [15]: f()
       
   168 ---------------------------------------
       
   169 \end{lstlisting}
       
   170 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
       
   171 \typ{(0 given)}
       
   172 \begin{lstlisting}
       
   173 In []: f(0)
       
   174 Out[]: 0.0
       
   175 In []: f(1)
       
   176 Out[]: 1.8414709848078965
       
   177 \end{lstlisting}
       
   178 \end{frame}
       
   179 
       
   180 
       
   181 \begin{frame}[fragile]
       
   182 \frametitle{Functions - Default Arguments}
       
   183 \begin{lstlisting}
       
   184 In []: def f(x=1):
       
   185            return sin(x)+x**2
       
   186 In []: f(10)
       
   187 Out[]: 99.455978889110625
       
   188 In []: f(1)
       
   189 Out[]: 1.8414709848078965
       
   190 In []: f()
       
   191 Out[]: 1.8414709848078965
       
   192 \end{lstlisting}
       
   193 \end{frame}
       
   194 
       
   195 \begin{frame}[fragile]
       
   196 \frametitle{Functions - Keyword Arguments}
       
   197 \begin{lstlisting}
       
   198 In []: def f(x=1, y=pi):
       
   199            return sin(y)+x**2
       
   200 In []: f()
       
   201 Out[]: 1.0000000000000002
       
   202 In []: f(2)
       
   203 Out[]: 4.0
       
   204 In []: f(y=2)
       
   205 Out[]: 1.9092974268256817
       
   206 In []: f(y=pi/2,x=0)
       
   207 Out[]: 1.0
       
   208 \end{lstlisting}
       
   209 \end{frame}
       
   210 
       
   211 \begin{frame}[fragile]
       
   212   \frametitle{More on functions}
       
   213   \begin{itemize}
       
   214   \item Scope of variables in the function is local
       
   215   \item Mutable items are \alert{passed by reference}
       
   216   \item First line after definition may be a documentation string
       
   217     (\alert{recommended!})
       
   218   \item Function definition and execution defines a name bound to the
       
   219     function
       
   220   \item You \emph{can} assign a variable to a function!
       
   221   \end{itemize}
       
   222 \end{frame}
       
   223 
       
   224 \begin{frame}[fragile]
       
   225 \frametitle{Quadrature \ldots}
       
   226 \begin{lstlisting}
       
   227 In []: quad(f, 0, 1)
       
   228 \end{lstlisting}
       
   229 Returns the integral and an estimate of the absolute error in the result.
       
   230 \begin{itemize}
       
   231 \item Use \typ{dblquad} for Double integrals
       
   232 \item Use \typ{tplquad} for Triple integrals
       
   233 \end{itemize}
       
   234 \end{frame}
       
   235 
       
   236 \subsection{ODEs}
       
   237 
       
   238 \begin{frame}[fragile]
       
   239 \frametitle{ODE Integration}
       
   240 We shall use the simple ODE of a simple pendulum. 
       
   241 \begin{equation*}
       
   242 \ddot{\theta} = -\frac{g}{L}sin(\theta)
       
   243 \end{equation*}
       
   244 \begin{itemize}
       
   245 \item This equation can be written as a system of two first order ODEs
       
   246 \end{itemize}
       
   247 \begin{align}
       
   248 \dot{\theta} &= \omega \\
       
   249 \dot{\omega} &= -\frac{g}{L}sin(\theta) \\
       
   250  \text{At}\ t &= 0 : \nonumber \\
       
   251  \theta = \theta_0\quad & \&\quad  \omega = 0 \nonumber
       
   252 \end{align}
       
   253 \end{frame}
       
   254 
       
   255 \begin{frame}[fragile]
       
   256 \frametitle{Solving ODEs using SciPy}
       
   257 \begin{itemize}
       
   258 \item We use the \typ{odeint} function from scipy to do the integration
       
   259 \item Define a function as below
       
   260 \end{itemize}
       
   261 \begin{lstlisting}
       
   262 In []: def pend_int(unknown, t, p):
       
   263   ....     theta, omega = unknown
       
   264   ....     g, L = p
       
   265   ....     f=[omega, -(g/L)*sin(theta)]
       
   266   ....     return f
       
   267   ....
       
   268 \end{lstlisting}
       
   269 \end{frame}
       
   270 
       
   271 \begin{frame}[fragile]
       
   272 \frametitle{Solving ODEs using SciPy \ldots}
       
   273 \begin{itemize}
       
   274 \item \typ{t} is the time variable \\ 
       
   275 \item \typ{p} has the constants \\
       
   276 \item \typ{initial} has the initial values
       
   277 \end{itemize}
       
   278 \begin{lstlisting}
       
   279 In []: t = linspace(0, 10, 101)
       
   280 In []: p=(-9.81, 0.2)
       
   281 In []: initial = [10*2*pi/360, 0]
       
   282 \end{lstlisting}
       
   283 \end{frame}
       
   284 
       
   285 \begin{frame}[fragile]
       
   286 \frametitle{Solving ODEs using SciPy \ldots}
       
   287 
       
   288 \small{\typ{In []: from scipy.integrate import odeint}}
       
   289 \begin{lstlisting}
       
   290 In []: pend_sol = odeint(pend_int, 
       
   291                          initial,t, 
       
   292                          args=(p,))
       
   293 \end{lstlisting}
       
   294 \end{frame}
       
   295 
       
   296 \begin{frame}
       
   297   \frametitle{Things we have learned}
       
   298   \begin{itemize}
       
   299   \item Functions
       
   300     \begin{itemize}
       
   301     \item Definition
       
   302     \item Calling
       
   303     \item Default Arguments
       
   304     \item Keyword Arguments
       
   305     \end{itemize}
       
   306   \item Quadrature
       
   307   \end{itemize}
       
   308 \end{frame}
       
   309 
       
   310 \end{document}
       
   311