day1/session5.tex
changeset 269 9c9be698d7ad
parent 266 28d4714a9702
child 339 8ac5fe07810f
equal deleted inserted replaced
268:f978ddc90960 269:9c9be698d7ad
    72 % }
    72 % }
    73 
    73 
    74 
    74 
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    76 % Title page
    76 % Title page
    77 \title[Calculus]{Python for Science and Engg: Interpolation, Differentiation and Integration}
    77 \title[]{}
    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 5}
    82 \date[] {31, October 2009\\Day 1, Session 5}
   122 %%   \frametitle{Outline}
   122 %%   \frametitle{Outline}
   123 %%   \tableofcontents
   123 %%   \tableofcontents
   124 %% %  \pausesections
   124 %% %  \pausesections
   125 %% \end{frame}
   125 %% \end{frame}
   126 
   126 
   127 \section{\typ{loadtxt}}
       
   128 
       
   129 \begin{frame}[fragile]
       
   130   \frametitle{Array slicing}
       
   131   \begin{lstlisting}
       
   132 In []: A = array([[ 1,  1,  2, -1],
       
   133                   [ 2,  5, -1, -9],
       
   134                   [ 2,  1, -1,  3],
       
   135                   [ 1, -3,  2,  7]])
       
   136 
       
   137 In []: A[:,0]
       
   138 Out[]: array([ 1,  2,  2, 1])
       
   139 
       
   140 In []: A[1:3,1:3]
       
   141 Out[]: 
       
   142 array([[ 5, -1],
       
   143        [ 1, -1]])
       
   144 \end{lstlisting}
       
   145 \end{frame}
       
   146 
       
   147 \begin{frame}[fragile]
       
   148   \frametitle{\typ{loadtxt}}
       
   149   \begin{itemize}
       
   150   \item Load data from a text file.
       
   151   \item Each row must have same number of values.
       
   152   \end{itemize}
       
   153 \begin{lstlisting}
       
   154 In []: data = loadtxt('pendulum.txt')
       
   155 In []: x = data[:, 0]
       
   156 In []: y = data[:, 1]
       
   157 \end{lstlisting}
       
   158 \end{frame}
       
   159 
       
   160 %% \begin{frame}[fragile]
       
   161 %%   \frametitle{\typ{loadtxt}}
       
   162 %% \end{frame}
       
   163 
       
   164 \section{Interpolation}
       
   165 \begin{frame}[fragile]
       
   166 \frametitle{Loading data (revisited)}
       
   167 \begin{itemize}
       
   168   \item Given data file \typ{points.txt}.
       
   169   \item It contains x,y position of particle.
       
   170   \item Plot the given points.
       
   171 %%  \item Interpolate the missing region.
       
   172 \end{itemize}
       
   173 \begin{lstlisting}
       
   174 In []: x, y = loadtxt('points.txt',
       
   175                        unpack = True)
       
   176 In []: plot(x, y, '.')
       
   177 \end{lstlisting}
       
   178 \end{frame}
       
   179 
       
   180 \begin{frame}
       
   181   \frametitle{Plot}
       
   182   \begin{center}
       
   183     \includegraphics[height=2in, interpolate=true]{data/missing_points}
       
   184   \end{center}
       
   185 \end{frame}
       
   186 %% \begin{frame}[fragile]
       
   187 %% \frametitle{Interpolation \ldots}
       
   188 %% \begin{small}
       
   189 %%   \typ{In []: from scipy.interpolate import interp1d}
       
   190 %% \end{small}
       
   191 %% \begin{itemize}
       
   192 %% \item The \typ{interp1d} function returns a function
       
   193 %% \begin{lstlisting}
       
   194 %%   In []: f = interp1d(L, T)
       
   195 %% \end{lstlisting}
       
   196 %% \item Functions can be assigned to variables 
       
   197 %% \item This function interpolates between known data values to obtain unknown
       
   198 %% \end{itemize}
       
   199 %% \end{frame}
       
   200 
       
   201 %% \begin{frame}[fragile]
       
   202 %% \frametitle{Interpolation \ldots}
       
   203 %% \begin{lstlisting}
       
   204 %% In []: Ln = arange(0.1,0.99,0.005)
       
   205 %% # Interpolating! 
       
   206 %% # The new values in range of old data
       
   207 %% In []: plot(L, T, 'o', Ln, f(Ln), '-')
       
   208 %% In []: f = interp1d(L, T, kind='cubic')
       
   209 %% # When kind not specified, it's linear
       
   210 %% # Others are ...
       
   211 %% # 'nearest', 'zero', 
       
   212 %% # 'slinear', 'quadratic'
       
   213 %% \end{lstlisting}
       
   214 %% \end{frame}
       
   215 
       
   216 \begin{frame}[fragile]
       
   217 \frametitle{Spline Interpolation}
       
   218 \begin{small}
       
   219 \begin{lstlisting}
       
   220 In []: from scipy.interpolate import splrep
       
   221 In []: from scipy.interpolate import splev
       
   222 \end{lstlisting}
       
   223 \end{small}
       
   224 \begin{itemize}
       
   225 \item Involves two steps
       
   226   \begin{enumerate}
       
   227   \item Find out the spline curve, coefficients
       
   228   \item Evaluate the spline at new points
       
   229   \end{enumerate}
       
   230 \end{itemize}
       
   231 \end{frame}
       
   232 
       
   233 \begin{frame}[fragile]
       
   234 \frametitle{\typ{splrep}}
       
   235 To find the spline curve
       
   236 \begin{lstlisting}
       
   237 In []: tck = splrep(x, y)
       
   238 \end{lstlisting}
       
   239 \typ{tck} contains parameters required for representing the spline curve!
       
   240 \end{frame}
       
   241 
       
   242 \begin{frame}[fragile]
       
   243 \frametitle{\typ{splev}}
       
   244 To Evaluate a spline and it's derivatives
       
   245 \begin{lstlisting}
       
   246 In []: Xnew = arange(0.01,3,0.02)
       
   247 In []: Ynew = splev(Xnew, tck)
       
   248 
       
   249 In []: y.shape
       
   250 Out[]: (40,)
       
   251 
       
   252 In []: Ynew.shape
       
   253 Out[]: (150,)
       
   254  
       
   255 In []: plot(Xnew, Ynew)
       
   256 \end{lstlisting}
       
   257 
       
   258 \end{frame}
       
   259 
       
   260 %% \begin{frame}[fragile]
       
   261 %% \frametitle{Interpolation \ldots}
       
   262 %% \begin{itemize}
       
   263 %% \item 
       
   264 %% \end{itemize}
       
   265 %% \end{frame}
       
   266 
       
   267 \begin{frame}
       
   268   \frametitle{Plot}
       
   269   \begin{center}
       
   270     \includegraphics[height=2in, interpolate=true]{data/interpolate}
       
   271   \end{center}
       
   272 \end{frame}
       
   273 
       
   274 \section{Differentiation}
       
   275 
       
   276 \begin{frame}[fragile]
       
   277 \frametitle{Numerical Differentiation}
       
   278 \begin{itemize}
       
   279 \item Given function $f(x)$ or data points $y=f(x)$
       
   280 \item We wish to calculate $f^{'}(x)$ at points $x$
       
   281 \item Taylor series - finite difference approximations
       
   282 \end{itemize}
       
   283 \begin{center}
       
   284 \begin{tabular}{l l}
       
   285 $f(x+h)=f(x)+hf^{'}(x)$ &Forward \\
       
   286 $f(x-h)=f(x)-hf^{'}(x)$ &Backward
       
   287 \end{tabular}
       
   288 \end{center}
       
   289 \end{frame}
       
   290 
       
   291 \begin{frame}[fragile]
       
   292 \frametitle{Forward Difference}
       
   293 \begin{lstlisting}
       
   294 In []: x = linspace(0, 2*pi, 100)
       
   295 In []: y = sin(x)
       
   296 In []: deltax = x[1] - x[0]
       
   297 \end{lstlisting}
       
   298 Obtain the finite forward difference of y
       
   299 \end{frame}
       
   300 
       
   301 \begin{frame}[fragile]
       
   302 \frametitle{Forward Difference \ldots}
       
   303 \begin{lstlisting}
       
   304 In []: fD = (y[1:] - y[:-1]) / deltax
       
   305 In []: print len(fD)
       
   306 Out[]: 99
       
   307 In []: plot(x, y) 
       
   308 In []: plot(x[:-1], fD)
       
   309 \end{lstlisting}
       
   310 \vspace{-.2in}
       
   311 \begin{center}
       
   312   \includegraphics[height=1.8in, interpolate=true]{data/fwdDiff}
       
   313 \end{center}
       
   314 \end{frame}
       
   315 
       
   316 \begin{frame}[fragile]
       
   317 \frametitle{Example}
       
   318 \begin{itemize}
       
   319 \item Given x, y positions of a particle in \typ{pos.txt}
       
   320 \item Find velocity \& acceleration in x, y directions
       
   321 \end{itemize}
       
   322 \small{
       
   323 \begin{center}
       
   324 \begin{tabular}{| c | c | c |}
       
   325 \hline
       
   326 $X$ & $Y$ \\ \hline
       
   327 0.     &  0.\\ \hline
       
   328 0.25   &  0.47775\\ \hline
       
   329 0.5    &  0.931\\ \hline
       
   330 0.75   &  1.35975\\ \hline
       
   331 1.     &  1.764\\ \hline
       
   332 1.25   &  2.14375\\ \hline
       
   333 \vdots & \vdots\\ \hline
       
   334 \end{tabular}
       
   335 \end{center}}
       
   336 \end{frame}
       
   337 
       
   338 \begin{frame}[fragile]
       
   339 \frametitle{Example \ldots}
       
   340 \begin{itemize}
       
   341 \item Read the file
       
   342 \item Obtain an array of X, Y
       
   343 \item Obtain velocity and acceleration
       
   344 \item use \typ{deltaT = 0.05}
       
   345 \end{itemize}
       
   346 \begin{lstlisting}
       
   347 In []: data = loadtxt('pos.txt')
       
   348 In []: X,Y = data[:,0], data[:,1]
       
   349 In []: S = array([X, Y])
       
   350 \end{lstlisting}
       
   351 \end{frame}
       
   352 
       
   353 
       
   354 \begin{frame}[fragile]
       
   355 \frametitle{Example \ldots}
       
   356 \begin{lstlisting}
       
   357 In []: deltaT = 0.05
       
   358 
       
   359 In []: v = (S[:,1:]-S[:,:-1])/deltaT
       
   360 
       
   361 In []: a = (v[:,1:]-v[:,:-1])/deltaT
       
   362 \end{lstlisting}
       
   363 \end{frame}
       
   364 
       
   365 \begin{frame}[fragile]
       
   366 \frametitle{Example \ldots}
       
   367 Plotting Y, $v_y$, $a_y$
       
   368 \begin{lstlisting}
       
   369 In []: plot(Y)
       
   370 In []: plot(v[1,:])
       
   371 In []: plot(a[1,:])
       
   372 \end{lstlisting}
       
   373 \begin{center}
       
   374   \includegraphics[height=1.8in, interpolate=true]{data/pos_vel_accel}  
       
   375 \end{center}
       
   376 \end{frame}
       
   377 
       
   378 \section{Quadrature}
       
   379 
       
   380 \begin{frame}[fragile]
       
   381 \frametitle{Quadrature}
       
   382 
       
   383 \emphbar{$\int_0^1(sin(x) + x^2)$}
       
   384 
       
   385 \typ{In []: from scipy.integrate import quad}
       
   386 
       
   387 \begin{itemize}
       
   388 \item Inputs - function to integrate, limits
       
   389 \end{itemize}
       
   390 \begin{lstlisting}
       
   391 In []: quad(sin(x)+x**2, 0, 1)
       
   392 NameError: name 'x' is not defined
       
   393 In []: x = 0
       
   394 In []: quad(sin(x)+x**2, 0, 1)
       
   395 \end{lstlisting}
       
   396 \begin{small}
       
   397 \alert{\typ{error:}}
       
   398 \typ{First argument must be a callable function.}
       
   399 \end{small}
       
   400 \end{frame}
       
   401 
       
   402 \begin{frame}[fragile]
       
   403 \frametitle{Functions - Definition}
       
   404 We have been using them all along. Now let's see how to define them.
       
   405 \begin{lstlisting}
       
   406 In []: def f(x):
       
   407            return sin(x)+x**2
       
   408 In []: quad(f, 0, 1)
       
   409 \end{lstlisting}
       
   410 \begin{itemize}
       
   411 \item \typ{def}
       
   412 \item name
       
   413 \item arguments
       
   414 \item \typ{return}
       
   415 \end{itemize}
       
   416 \end{frame}
       
   417 
       
   418 \begin{frame}[fragile]
       
   419 \frametitle{Functions - Calling them}
       
   420 \begin{lstlisting}
       
   421 In [15]: f()
       
   422 ---------------------------------------
       
   423 \end{lstlisting}
       
   424 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
       
   425 \typ{(0 given)}
       
   426 \begin{lstlisting}
       
   427 In []: f(0)
       
   428 Out[]: 0.0
       
   429 In []: f(1)
       
   430 Out[]: 1.8414709848078965
       
   431 \end{lstlisting}
       
   432 More on Functions later \ldots
       
   433 \end{frame}
       
   434 
       
   435 \begin{frame}[fragile]
       
   436 \frametitle{Quadrature \ldots}
       
   437 \begin{lstlisting}
       
   438 In []: quad(f, 0, 1)
       
   439 \end{lstlisting}
       
   440 Returns the integral and an estimate of the absolute error in the result.
       
   441 \begin{itemize}
       
   442 \item \typ{dblquad}, \typ{tplquad} are available
       
   443 \end{itemize}
       
   444 \end{frame}
       
   445 
       
   446 \begin{frame}
       
   447   \frametitle{Things we have learned}
       
   448   \begin{itemize}
       
   449   \item Interpolation
       
   450   \item Differentiation
       
   451   \item Functions
       
   452     \begin{itemize}
       
   453     \item Definition
       
   454     \item Calling
       
   455     \item Default Arguments
       
   456     \item Keyword Arguments
       
   457     \end{itemize}
       
   458   \item Quadrature
       
   459   \end{itemize}
       
   460 \end{frame}
       
   461 
   127 
   462 \end{document}
   128 \end{document}
   463 
   129