day1/session2.tex
changeset 301 49bdffe4dca5
parent 300 f87f2a310abe
child 319 cef948318842
equal deleted inserted replaced
300:f87f2a310abe 301:49bdffe4dca5
   124 \end{frame}
   124 \end{frame}
   125 
   125 
   126 \section{Plotting Points}
   126 \section{Plotting Points}
   127 \begin{frame}[fragile]
   127 \begin{frame}[fragile]
   128 \frametitle{Why would I plot f(x)?}
   128 \frametitle{Why would I plot f(x)?}
   129 How often do we plot analytical functions?\\We plot experimental data more.
   129 Do we plot analytical functions or experimental data?
   130 \begin{small}
   130 \begin{small}
   131 \begin{lstlisting}
   131 \begin{lstlisting}
   132 In []: x = [0, 1, 2, 3]
   132 In []: x = [0, 1, 2, 3]
   133 
   133 
   134 In []: y = [7, 11, 15, 19]
   134 In []: y = [7, 11, 15, 19]
   202 \end{frame}
   202 \end{frame}
   203 
   203 
   204 \begin{frame}[fragile]
   204 \begin{frame}[fragile]
   205 \frametitle{Lists: Initializing \& accessing elements}
   205 \frametitle{Lists: Initializing \& accessing elements}
   206 \begin{lstlisting}
   206 \begin{lstlisting}
   207 In []: mtlist = [] #Empty List
   207 In []: mtlist = [] 
   208 
   208 \end{lstlisting}
       
   209 \emphbar{Empty List}
       
   210 \begin{lstlisting}
   209 In []: a = [ 1, 2, 3, 4, 5] 
   211 In []: a = [ 1, 2, 3, 4, 5] 
   210 
   212 
   211 In []: a[0]+a[1]+a[-1]
   213 In []: a[0]+a[1]+a[-1]
   212 Out[]: 8
   214 Out[]: 8
   213 \end{lstlisting}
   215 \end{lstlisting}
   217   \frametitle{List: Slicing}
   219   \frametitle{List: Slicing}
   218   \begin{block}{Remember\ldots}
   220   \begin{block}{Remember\ldots}
   219 	\kwrd{In []: a = [ 1, 2, 3, 4, 5]}
   221 	\kwrd{In []: a = [ 1, 2, 3, 4, 5]}
   220   \end{block}
   222   \end{block}
   221 \begin{lstlisting}
   223 \begin{lstlisting}
   222 In []: a[1:3]  # A slice.
   224 In []: a[1:3]
   223 Out[]: [2, 3]
   225 Out[]: [2, 3]
   224 
   226 \end{lstlisting}
       
   227 \emphbar{A slice}
       
   228 \begin{lstlisting}
   225 In []: a[1:-1]
   229 In []: a[1:-1]
   226 Out[]: [2, 3, 4]
   230 Out[]: [2, 3, 4]
   227 \end{lstlisting}
   231 \end{lstlisting}
   228 \alert{\typ{list[initial:final]}}
   232 \alert{\typ{list[initial:final]}}
   229 \end{frame}
   233 \end{frame}
   252 \begin{center}
   256 \begin{center}
   253 \begin{small}
   257 \begin{small}
   254 \begin{tabular}{| c | c | c |}
   258 \begin{tabular}{| c | c | c |}
   255 \hline
   259 \hline
   256 $L$ & $T$ & $T^2$ \\ \hline
   260 $L$ & $T$ & $T^2$ \\ \hline
   257 0.1 & 0.6900 & \\ \hline
   261 0.1 & 0.69 & \\ \hline
   258 0.2 & 0.8989 & \\ \hline
   262 0.2 & 0.90 & \\ \hline
   259 0.3 & 1.1867 & \\ \hline
   263 0.3 & 1.19 & \\ \hline
   260 0.4 & 1.2991 & \\ \hline
   264 0.4 & 1.30 & \\ \hline
   261 0.5 & 1.4656 & \\ \hline
   265 0.5 & 1.47 & \\ \hline
   262 0.6 & 1.5843 & \\ \hline
   266 0.6 & 1.58 & \\ \hline
   263 0.7 & 1.7706 & \\ \hline
   267 0.7 & 1.77 & \\ \hline
   264 0.8 & 1.8296 & \\ \hline
   268 0.8 & 1.83 & \\ \hline
   265 0.9 & 1.9440 & \\ \hline
   269 0.9 & 1.94 & \\ \hline
   266 \end{tabular}
   270 \end{tabular}
   267 \end{small}\\
   271 \end{small}\\
   268 \alert{$L \alpha T^2$}
   272 \alert{$L \alpha T^2$}
   269 \end{center}
   273 \end{center}
   270 \end{frame}
   274 \end{frame}
   273 \frametitle{Lets use lists}
   277 \frametitle{Lets use lists}
   274 \begin{lstlisting}
   278 \begin{lstlisting}
   275 In []: l = [0.1, 0.2, 0.3, 0.4, 0.5, 
   279 In []: l = [0.1, 0.2, 0.3, 0.4, 0.5, 
   276             0.6, 0.7, 0.8, 0.9]
   280             0.6, 0.7, 0.8, 0.9]
   277 
   281 
   278 In []: t = [0.69, 0.8989, 1.1867, 
   282 In []: t = [0.69, 0.90, 1.19, 
   279             1.2991, 1.4656, 1.5843, 
   283             1.30, 1.47, 1.58, 
   280             1.7706, 1.8296, 1.9440]
   284             1.77, 1.83, 1.94]
   281 \end{lstlisting}
   285 \end{lstlisting}
   282 \end{frame}
   286 \end{frame}
   283 
   287 
   284 \begin{frame}[fragile]
   288 \begin{frame}[fragile]
   285 \frametitle{Plotting $L$ vs $T^2$}
   289 \frametitle{Plotting $L$ vs $T^2$}
   303 \end{lstlisting}
   307 \end{lstlisting}
   304 This gives \kwrd{tsq} which is the list of squares of \typ{t} values.
   308 This gives \kwrd{tsq} which is the list of squares of \typ{t} values.
   305 \end{frame}
   309 \end{frame}
   306 
   310 
   307 \begin{frame}[fragile]
   311 \begin{frame}[fragile]
   308   \frametitle{How to come out of the \textt{for} loop?}
   312   \frametitle{How to come out of the \texttt{for} loop?}
   309   Hit the ``enter'' key twice to come to the previous indentation level
   313   Hit the ``ENTER'' key twice to come to the previous indentation level
   310   \begin{lstlisting}
   314   \begin{lstlisting}
   311     In []: for time in t:
   315     In []: for time in t:
   312      ....:     tsq.append(time*time)
   316      ....:     tsq.append(time*time)
   313      ....:     
   317      ....:     
   314      ....:     
   318      ....:     
   315 
   319 
   316     In []:
   320     In []: print tsq
   317   \end{lstlisting}
   321   \end{lstlisting}
   318 \end{frame}
   322 \end{frame}
   319 
   323 
   320 \begin{frame}[fragile]
   324 \begin{frame}[fragile]
   321 \begin{figure}
   325 \begin{figure}
   336 1.4000e-01 8.3568e-01
   340 1.4000e-01 8.3568e-01
   337 1.5000e-01 8.6789e-01
   341 1.5000e-01 8.6789e-01
   338 \end{lstlisting}
   342 \end{lstlisting}
   339 \ldots
   343 \ldots
   340 \begin{block}{Windows users:}
   344 \begin{block}{Windows users:}
   341   > type pendulum.txt
   345   C:> type pendulum.txt
   342 \end{block}
   346 \end{block}
   343 \end{frame}
   347 \end{frame}
   344 
   348 
   345 \begin{frame}[fragile]
   349 \begin{frame}[fragile]
   346 \frametitle{Reading \typ{pendulum.txt}}
   350 \frametitle{Reading \typ{pendulum.txt}}
   356 Open a new script and type the following:
   360 Open a new script and type the following:
   357 \begin{lstlisting}
   361 \begin{lstlisting}
   358 l = []
   362 l = []
   359 t = []
   363 t = []
   360 for line in open('pendulum.txt'):
   364 for line in open('pendulum.txt'):
   361     points = line.split()
   365     point = line.split()
   362     l.append(float(points[0]))
   366     l.append(float(point[0]))
   363     t.append(float(points[1]))
   367     t.append(float(point[1]))
   364 tsq = []
   368 tsq = []
   365 for time in t:
   369 for time in t:
   366     tsq.append(time*time)
   370     tsq.append(time*time)
   367 plot(l, tsq, '.')
   371 plot(l, tsq, '.')
   368 \end{lstlisting}
   372 \end{lstlisting}
   414   \end{lstlisting}
   418   \end{lstlisting}
   415 This is what happens with \typ{line}
   419 This is what happens with \typ{line}
   416   \begin{lstlisting}
   420   \begin{lstlisting}
   417 In []: line = '1.2000e-01 7.4252e-01'
   421 In []: line = '1.2000e-01 7.4252e-01'
   418 
   422 
   419 In []: line.split()
   423 In []: point = line.split()
       
   424 
       
   425 In []: point
   420 Out[]: ['1.2000e-01', '7.4252e-01']
   426 Out[]: ['1.2000e-01', '7.4252e-01']
   421   \end{lstlisting}
   427   \end{lstlisting}
   422 \end{frame}
   428 \end{frame}
   423 
   429 
   424 \begin{frame}[fragile]
   430 \begin{frame}[fragile]
   425 \frametitle{Getting floats from strings}
   431 \frametitle{Getting floats from strings}
   426   \begin{lstlisting}
   432   \begin{lstlisting}
   427 In []: type(points[0])
   433 In []: type(point[0])
   428 Out[]: <type 'str'>
   434 Out[]: <type 'str'>
   429   \end{lstlisting}
   435   \end{lstlisting}
   430 But, we need floating point numbers
   436 But, we need floating point numbers
   431   \begin{lstlisting}
   437   \begin{lstlisting}
   432 In []: t = float(points[0])
   438 In []: t = float(point[0])
   433 
   439 
   434 In []: type(t)
   440 In []: type(t)
   435 Out[]: <type 'float'>
   441 Out[]: <type 'float'>
   436   \end{lstlisting}
   442   \end{lstlisting}
   437 \end{frame}
   443 \end{frame}
   440 \frametitle{Let's review the code}
   446 \frametitle{Let's review the code}
   441 \begin{lstlisting}
   447 \begin{lstlisting}
   442 l = []
   448 l = []
   443 t = []
   449 t = []
   444 for line in open('pendulum.txt'):
   450 for line in open('pendulum.txt'):
   445     points = line.split()
   451     point = line.split()
   446     l.append(float(points[0]))
   452     l.append(float(point[0]))
   447     t.append(float(points[1]))
   453     t.append(float(point[1]))
   448 tsq = []
   454 tsq = []
   449 for time in t:
   455 for time in t:
   450     tsq.append(time*time)
   456     tsq.append(time*time)
   451 plot(l, tsq, '.')
   457 plot(l, tsq, '.')
   452 \end{lstlisting}
   458 \end{lstlisting}
   465   \item Plotting points
   471   \item Plotting points
   466   \item Plot attributes
   472   \item Plot attributes
   467   \item Lists
   473   \item Lists
   468   \item \kwrd{for}
   474   \item \kwrd{for}
   469   \item Reading files
   475   \item Reading files
       
   476   \item Tokenizing
   470   \item Strings
   477   \item Strings
   471 \end{itemize}
   478 \end{itemize}
   472 \end{frame}
   479 \end{frame}
   473 
   480 
   474 \end{document}
   481 \end{document}