day1/session5.tex
changeset 263 8a4a1e5aec85
parent 262 9664eddee075
child 266 28d4714a9702
equal deleted inserted replaced
262:9664eddee075 263:8a4a1e5aec85
   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}}
   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.
       
   172 %%  load txt the unpack part.
   154 \end{itemize}
   173 \end{itemize}
   155 \begin{lstlisting}
   174 \begin{lstlisting}
   156 In []: x, y = loadtxt('points.txt',
   175 In []: x, y = loadtxt('points.txt',
   157                        unpack = True)
   176                        unpack = True)
   158 In []: plot(x, y, '.')
   177 In []: plot(x, y, '.')
   193 %% # 'nearest', 'zero', 
   212 %% # 'nearest', 'zero', 
   194 %% # 'slinear', 'quadratic'
   213 %% # 'slinear', 'quadratic'
   195 %% \end{lstlisting}
   214 %% \end{lstlisting}
   196 %% \end{frame}
   215 %% \end{frame}
   197 
   216 
       
   217 %%better example, maybe parabola
       
   218 
   198 \begin{frame}[fragile]
   219 \begin{frame}[fragile]
   199 \frametitle{Spline Interpolation}
   220 \frametitle{Spline Interpolation}
   200 \begin{small}
   221 \begin{small}
   201 \begin{lstlisting}
   222 \begin{lstlisting}
   202 In []: from scipy.interpolate import splrep
   223 In []: from scipy.interpolate import splrep
   221 \typ{tck} contains parameters required for representing the spline curve!
   242 \typ{tck} contains parameters required for representing the spline curve!
   222 \end{frame}
   243 \end{frame}
   223 
   244 
   224 \begin{frame}[fragile]
   245 \begin{frame}[fragile]
   225 \frametitle{\typ{splev}}
   246 \frametitle{\typ{splev}}
   226 To Evaluate a B-spline and it's derivatives
   247 To Evaluate a spline and it's derivatives
   227 \begin{lstlisting}
   248 \begin{lstlisting}
   228 In []: Xnew = arange(0.01,3,0.02)
   249 In []: Xnew = linspace(0.01,3,100)
   229 In []: Ynew = splev(Xnew, tck)
   250 In []: Ynew = splev(Xnew, tck)
   230 
   251 
   231 In []: y.shape
   252 In []: y.shape
   232 Out[]: (40,)
   253 Out[]: (40,)
   233 
   254 
   234 In []: Ynew.shape
   255 In []: Ynew.shape
   235 Out[]: (150,)
   256 Out[]: (100,)
   236  
   257  
   237 In []: plot(Xnew, Ynew)
   258 In []: plot(Xnew, Ynew, '+')
   238 \end{lstlisting}
   259 \end{lstlisting}
   239 
   260 
       
   261 \end{frame}
       
   262 
       
   263 \begin{frame}
       
   264   \frametitle{Interpolated data}
       
   265   \begin{center}
       
   266     \includegraphics[height=2in, interpolate=true]{data/interpolate}
       
   267   \end{center}
   240 \end{frame}
   268 \end{frame}
   241 
   269 
   242 %% \begin{frame}[fragile]
   270 %% \begin{frame}[fragile]
   243 %% \frametitle{Interpolation \ldots}
   271 %% \frametitle{Interpolation \ldots}
   244 %% \begin{itemize}
   272 %% \begin{itemize}
   255 \item We wish to calculate $f^{'}(x)$ at points $x$
   283 \item We wish to calculate $f^{'}(x)$ at points $x$
   256 \item Taylor series - finite difference approximations
   284 \item Taylor series - finite difference approximations
   257 \end{itemize}
   285 \end{itemize}
   258 \begin{center}
   286 \begin{center}
   259 \begin{tabular}{l l}
   287 \begin{tabular}{l l}
   260 $f(x+h)=f(x)+h.f^{'}(x)$ &Forward \\
   288 $f(x+h)=f(x)+hf^{'}(x)$ &Forward \\
   261 $f(x-h)=f(x)-h.f^{'}(x)$ &Backward
   289 $f(x-h)=f(x)-hf^{'}(x)$ &Backward
   262 \end{tabular}
   290 \end{tabular}
   263 \end{center}
   291 \end{center}
   264 \end{frame}
   292 \end{frame}
   265 
   293 
   266 \begin{frame}[fragile]
   294 \begin{frame}[fragile]
   275 
   303 
   276 \begin{frame}[fragile]
   304 \begin{frame}[fragile]
   277 \frametitle{Forward Difference \ldots}
   305 \frametitle{Forward Difference \ldots}
   278 \begin{lstlisting}
   306 \begin{lstlisting}
   279 In []: fD = (y[1:] - y[:-1]) / deltax
   307 In []: fD = (y[1:] - y[:-1]) / deltax
   280 In []: plot(x, y, x[:-1], fD)
   308 In []: print len(fD)
       
   309 Out[]: 99
       
   310 In []: plot(x, y) 
       
   311 In []: plot(x[:-1], fD)
   281 \end{lstlisting}
   312 \end{lstlisting}
   282 \begin{center}
   313 \begin{center}
   283   \includegraphics[height=2in, interpolate=true]{data/fwdDiff}
   314   \includegraphics[height=2in, interpolate=true]{data/fwdDiff}
   284 \end{center}
   315 \end{center}
   285 \end{frame}
   316 \end{frame}
   308 
   339 
   309 \begin{frame}[fragile]
   340 \begin{frame}[fragile]
   310 \frametitle{Example \ldots}
   341 \frametitle{Example \ldots}
   311 \begin{itemize}
   342 \begin{itemize}
   312 \item Read the file
   343 \item Read the file
   313 \item Obtain an array of x, y
   344 \item Obtain an array of X, Y(S)
   314 \item Obtain velocity and acceleration
   345 \item Obtain velocity and acceleration
   315 \item use \typ{deltaT = 0.05}
   346 \item use \typ{deltaT = 0.05}
   316 \end{itemize}
   347 \end{itemize}
   317 \begin{lstlisting}
   348 \begin{lstlisting}
   318 In []: X = []
   349 In []: S = loadtxt('location.txt')
   319 In []: Y = []
       
   320 In []: for line in open('location.txt'):
       
   321   ....     points = line.split()
       
   322   ....     X.append(float(points[0]))
       
   323   ....     Y.append(float(points[1]))
       
   324 In []: S = array([X, Y])
       
   325 \end{lstlisting}
   350 \end{lstlisting}
   326 \end{frame}
   351 \end{frame}
   327 
   352 
   328 
   353 
   329 \begin{frame}[fragile]
   354 \begin{frame}[fragile]
   330 \frametitle{Example \ldots}
   355 \frametitle{Example \ldots}
   331 \begin{itemize}
       
   332 \item use \typ{deltaT = 0.05}
       
   333 \end{itemize}
       
   334 \begin{lstlisting}
   356 \begin{lstlisting}
   335 In []: deltaT = 0.05
   357 In []: deltaT = 0.05
   336 
   358 
   337 In []: v = (S[:,1:]-S[:,:-1])/deltaT
   359 In []: v = (S[:,1:]-S[:,:-1])/deltaT
   338 
   360 
   343 
   365 
   344 \section{Quadrature}
   366 \section{Quadrature}
   345 
   367 
   346 \begin{frame}[fragile]
   368 \begin{frame}[fragile]
   347 \frametitle{Quadrature}
   369 \frametitle{Quadrature}
   348 \begin{itemize}
   370 
   349 \item We wish to find area under a curve
   371 \emphbar{$\int_0^1(sin(x) + x^2)$}
   350 \item Area under $(sin(x) + x^2)$ in $(0,1)$
   372 
   351 \item scipy has functions to do that
   373 \typ{In []: from scipy.integrate import quad}
   352 \end{itemize}
   374 
   353 \begin{small}
       
   354   \typ{In []: from scipy.integrate import quad}
       
   355 \end{small}
       
   356 \begin{itemize}
   375 \begin{itemize}
   357 \item Inputs - function to integrate, limits
   376 \item Inputs - function to integrate, limits
   358 \end{itemize}
   377 \end{itemize}
   359 \begin{lstlisting}
   378 \begin{lstlisting}
       
   379 %% In []: quad(sin(x)+x**2, 0, 1)
       
   380 %% NameError: name 'x' is not defined
   360 In []: x = 0
   381 In []: x = 0
   361 In []: quad(sin(x)+x**2, 0, 1)
   382 In []: quad(sin(x)+x**2, 0, 1)
   362 \end{lstlisting}
   383 \end{lstlisting}
   363 \begin{small}
   384 \begin{small}
   364 \alert{\typ{error:}}
   385 \alert{\typ{error:}}
   404 \begin{lstlisting}
   425 \begin{lstlisting}
   405 In []: quad(f, 0, 1)
   426 In []: quad(f, 0, 1)
   406 \end{lstlisting}
   427 \end{lstlisting}
   407 Returns the integral and an estimate of the absolute error in the result.
   428 Returns the integral and an estimate of the absolute error in the result.
   408 \begin{itemize}
   429 \begin{itemize}
   409 \item Look at \typ{dblquad} for Double integrals
   430 \item \typ{dblquad}, \typ{tplquad} are available
   410 \item Use \typ{tplquad} for Triple integrals
       
   411 \end{itemize}
   431 \end{itemize}
   412 \end{frame}
   432 \end{frame}
   413 
   433 
   414 \begin{frame}
   434 \begin{frame}
   415   \frametitle{Things we have learned}
   435   \frametitle{Things we have learned}
   417   \item Interpolation
   437   \item Interpolation
   418   \item Differentiation
   438   \item Differentiation
   419   \item Functions
   439   \item Functions
   420     \begin{itemize}
   440     \begin{itemize}
   421     \item Definition
   441     \item Definition
   422     \item Calling
   442     \item Calling    
   423     \item Default Arguments
       
   424     \item Keyword Arguments
       
   425     \end{itemize}
   443     \end{itemize}
   426   \item Quadrature
   444   \item Quadrature
   427   \end{itemize}
   445   \end{itemize}
   428 \end{frame}
   446 \end{frame}
   429 
   447