day1/session5.tex
changeset 265 ac31e2f3754e
parent 260 1f5fea506927
child 266 28d4714a9702
equal deleted inserted replaced
264:c3a1de5b8216 265:ac31e2f3754e
   122 %%   \frametitle{Outline}
   122 %%   \frametitle{Outline}
   123 %%   \tableofcontents
   123 %%   \tableofcontents
   124 %% %  \pausesections
   124 %% %  \pausesections
   125 %% \end{frame}
   125 %% \end{frame}
   126 
   126 
   127 
       
   128 \section{\typ{loadtxt}}
   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}
   129 
   146 
   130 \begin{frame}[fragile]
   147 \begin{frame}[fragile]
   131   \frametitle{\typ{loadtxt}}
   148   \frametitle{\typ{loadtxt}}
   132   \begin{itemize}
   149   \begin{itemize}
   133   \item Load data from a text file.
   150   \item Load data from a text file.
   134   \item Each row must have same number of values.
   151   \item Each row must have same number of values.
   135   \end{itemize}
   152   \end{itemize}
   136 \begin{lstlisting}
   153 \begin{lstlisting}
   137 In []: L, T = loadtxt('pendulum.txt', 
   154 In []: data = loadtxt('pendulum.txt')
   138                        unpack = True)
   155 In []: x = data[:, 0]
       
   156 In []: y = data[:, 1]
   139 \end{lstlisting}
   157 \end{lstlisting}
   140 \end{frame}
   158 \end{frame}
   141 
   159 
   142 %% \begin{frame}[fragile]
   160 %% \begin{frame}[fragile]
   143 %%   \frametitle{\typ{loadtxt}}
   161 %%   \frametitle{\typ{loadtxt}}
   144 %% \end{frame}
   162 %% \end{frame}
   145 
   163 
   146 \section{Interpolation}
   164 \section{Interpolation}
   147 \begin{frame}[fragile]
   165 \begin{frame}[fragile]
   148 \frametitle{Interpolation}
   166 \frametitle{Loading data (revisited)}
   149 \begin{itemize}
   167 \begin{itemize}
   150   \item Given data file \typ{points.txt}.
   168   \item Given data file \typ{points.txt}.
   151   \item It contains x,y position of particle.
   169   \item It contains x,y position of particle.
   152   \item Plot the given points.
   170   \item Plot the given points.
   153 %%  \item Interpolate the missing region.
   171 %%  \item Interpolate the missing region.
   154 \end{itemize}
   172 \end{itemize}
   155 \emphbar{Loading data (revisited)}
       
   156 \begin{lstlisting}
   173 \begin{lstlisting}
   157 In []: x, y = loadtxt('points.txt',
   174 In []: x, y = loadtxt('points.txt',
   158                        unpack = True)
   175                        unpack = True)
   159 In []: plot(x, y, '.')
   176 In []: plot(x, y, '.')
   160 \end{lstlisting}
   177 \end{lstlisting}
   161 \end{frame}
   178 \end{frame}
   162 
   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}
   163 %% \begin{frame}[fragile]
   186 %% \begin{frame}[fragile]
   164 %% \frametitle{Interpolation \ldots}
   187 %% \frametitle{Interpolation \ldots}
   165 %% \begin{small}
   188 %% \begin{small}
   166 %%   \typ{In []: from scipy.interpolate import interp1d}
   189 %%   \typ{In []: from scipy.interpolate import interp1d}
   167 %% \end{small}
   190 %% \end{small}
   216 \typ{tck} contains parameters required for representing the spline curve!
   239 \typ{tck} contains parameters required for representing the spline curve!
   217 \end{frame}
   240 \end{frame}
   218 
   241 
   219 \begin{frame}[fragile]
   242 \begin{frame}[fragile]
   220 \frametitle{\typ{splev}}
   243 \frametitle{\typ{splev}}
   221 To Evaluate a B-spline and it's derivatives
   244 To Evaluate a spline and it's derivatives
   222 \begin{lstlisting}
   245 \begin{lstlisting}
   223 In []: Xnew = arange(0.01,3,0.02)
   246 In []: Xnew = arange(0.01,3,0.02)
   224 In []: Ynew = splev(Xnew, tck)
   247 In []: Ynew = splev(Xnew, tck)
   225 
   248 
   226 In []: y.shape
   249 In []: y.shape
   239 %% \begin{itemize}
   262 %% \begin{itemize}
   240 %% \item 
   263 %% \item 
   241 %% \end{itemize}
   264 %% \end{itemize}
   242 %% \end{frame}
   265 %% \end{frame}
   243 
   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 
   244 \section{Differentiation}
   274 \section{Differentiation}
   245 
   275 
   246 \begin{frame}[fragile]
   276 \begin{frame}[fragile]
   247 \frametitle{Numerical Differentiation}
   277 \frametitle{Numerical Differentiation}
   248 \begin{itemize}
   278 \begin{itemize}
   250 \item We wish to calculate $f^{'}(x)$ at points $x$
   280 \item We wish to calculate $f^{'}(x)$ at points $x$
   251 \item Taylor series - finite difference approximations
   281 \item Taylor series - finite difference approximations
   252 \end{itemize}
   282 \end{itemize}
   253 \begin{center}
   283 \begin{center}
   254 \begin{tabular}{l l}
   284 \begin{tabular}{l l}
   255 $f(x+h)=f(x)+h.f^{'}(x)$ &Forward \\
   285 $f(x+h)=f(x)+hf^{'}(x)$ &Forward \\
   256 $f(x-h)=f(x)-h.f^{'}(x)$ &Backward
   286 $f(x-h)=f(x)-hf^{'}(x)$ &Backward
   257 \end{tabular}
   287 \end{tabular}
   258 \end{center}
   288 \end{center}
   259 \end{frame}
   289 \end{frame}
   260 
   290 
   261 \begin{frame}[fragile]
   291 \begin{frame}[fragile]
   270 
   300 
   271 \begin{frame}[fragile]
   301 \begin{frame}[fragile]
   272 \frametitle{Forward Difference \ldots}
   302 \frametitle{Forward Difference \ldots}
   273 \begin{lstlisting}
   303 \begin{lstlisting}
   274 In []: fD = (y[1:] - y[:-1]) / deltax
   304 In []: fD = (y[1:] - y[:-1]) / deltax
   275 In []: plot(x, y, x[:-1], fD)
   305 In []: print len(fD)
   276 \end{lstlisting}
   306 Out[]: 99
       
   307 In []: plot(x, y) 
       
   308 In []: plot(x[:-1], fD)
       
   309 \end{lstlisting}
       
   310 \vspace{-.2in}
   277 \begin{center}
   311 \begin{center}
   278   \includegraphics[height=2in, interpolate=true]{data/fwdDiff}
   312   \includegraphics[height=1.8in, interpolate=true]{data/fwdDiff}
   279 \end{center}
   313 \end{center}
   280 \end{frame}
   314 \end{frame}
   281 
   315 
   282 \begin{frame}[fragile]
   316 \begin{frame}[fragile]
   283 \frametitle{Example}
   317 \frametitle{Example}
   303 
   337 
   304 \begin{frame}[fragile]
   338 \begin{frame}[fragile]
   305 \frametitle{Example \ldots}
   339 \frametitle{Example \ldots}
   306 \begin{itemize}
   340 \begin{itemize}
   307 \item Read the file
   341 \item Read the file
   308 \item Obtain an array of x, y
   342 \item Obtain an array of X, Y
   309 \item Obtain velocity and acceleration
   343 \item Obtain velocity and acceleration
   310 \item use \typ{deltaT = 0.05}
   344 \item use \typ{deltaT = 0.05}
   311 \end{itemize}
   345 \end{itemize}
   312 \begin{lstlisting}
   346 \begin{lstlisting}
   313 In []: X = []
   347 In []: data = loadtxt('pos.txt')
   314 In []: Y = []
   348 In []: X,Y = data[:,0], data[:,1]
   315 In []: for line in open('location.txt'):
       
   316   ....     points = line.split()
       
   317   ....     X.append(float(points[0]))
       
   318   ....     Y.append(float(points[1]))
       
   319 In []: S = array([X, Y])
   349 In []: S = array([X, Y])
   320 \end{lstlisting}
   350 \end{lstlisting}
   321 \end{frame}
   351 \end{frame}
   322 
   352 
   323 
   353 
   324 \begin{frame}[fragile]
   354 \begin{frame}[fragile]
   325 \frametitle{Example \ldots}
   355 \frametitle{Example \ldots}
   326 \begin{itemize}
       
   327 \item use \typ{deltaT = 0.05}
       
   328 \end{itemize}
       
   329 \begin{lstlisting}
   356 \begin{lstlisting}
   330 In []: deltaT = 0.05
   357 In []: deltaT = 0.05
   331 
   358 
   332 In []: v = (S[:,1:]-S[:,:-1])/deltaT
   359 In []: v = (S[:,1:]-S[:,:-1])/deltaT
   333 
   360 
   334 In []: a = (v[:,1:]-v[:,:-1])/deltaT
   361 In []: a = (v[:,1:]-v[:,:-1])/deltaT
   335 \end{lstlisting}
   362 \end{lstlisting}
   336 Try Plotting the position, velocity \& acceleration.
   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}
   337 \end{frame}
   376 \end{frame}
   338 
   377 
   339 \section{Quadrature}
   378 \section{Quadrature}
   340 
   379 
   341 \begin{frame}[fragile]
   380 \begin{frame}[fragile]
   342 \frametitle{Quadrature}
   381 \frametitle{Quadrature}
   343 \begin{itemize}
   382 
   344 \item We wish to find area under a curve
   383 \emphbar{$\int_0^1(sin(x) + x^2)$}
   345 \item Area under $(sin(x) + x^2)$ in $(0,1)$
   384 
   346 \item scipy has functions to do that
   385 \typ{In []: from scipy.integrate import quad}
   347 \end{itemize}
   386 
   348 \begin{small}
       
   349   \typ{In []: from scipy.integrate import quad}
       
   350 \end{small}
       
   351 \begin{itemize}
   387 \begin{itemize}
   352 \item Inputs - function to integrate, limits
   388 \item Inputs - function to integrate, limits
   353 \end{itemize}
   389 \end{itemize}
   354 \begin{lstlisting}
   390 \begin{lstlisting}
       
   391 In []: quad(sin(x)+x**2, 0, 1)
       
   392 NameError: name 'x' is not defined
   355 In []: x = 0
   393 In []: x = 0
   356 In []: quad(sin(x)+x**2, 0, 1)
   394 In []: quad(sin(x)+x**2, 0, 1)
   357 \end{lstlisting}
   395 \end{lstlisting}
   358 \begin{small}
   396 \begin{small}
   359 \alert{\typ{error:}}
   397 \alert{\typ{error:}}
   399 \begin{lstlisting}
   437 \begin{lstlisting}
   400 In []: quad(f, 0, 1)
   438 In []: quad(f, 0, 1)
   401 \end{lstlisting}
   439 \end{lstlisting}
   402 Returns the integral and an estimate of the absolute error in the result.
   440 Returns the integral and an estimate of the absolute error in the result.
   403 \begin{itemize}
   441 \begin{itemize}
   404 \item Look at \typ{dblquad} for Double integrals
   442 \item \typ{dblquad}, \typ{tplquad} are available
   405 \item Use \typ{tplquad} for Triple integrals
       
   406 \end{itemize}
   443 \end{itemize}
   407 \end{frame}
   444 \end{frame}
   408 
   445 
   409 \begin{frame}
   446 \begin{frame}
   410   \frametitle{Things we have learned}
   447   \frametitle{Things we have learned}