day1/session3.tex
changeset 116 cb91f43d2cbb
parent 115 66709e58f765
child 117 958e3e090c6b
equal deleted inserted replaced
115:66709e58f765 116:cb91f43d2cbb
    26 
    26 
    27 % Taken from Fernando's slides.
    27 % Taken from Fernando's slides.
    28 \usepackage{ae,aecompl}
    28 \usepackage{ae,aecompl}
    29 \usepackage{mathpazo,courier,euler}
    29 \usepackage{mathpazo,courier,euler}
    30 \usepackage[scaled=.95]{helvet}
    30 \usepackage[scaled=.95]{helvet}
       
    31 \usepackage{amsmath}
    31 
    32 
    32 \definecolor{darkgreen}{rgb}{0,0.5,0}
    33 \definecolor{darkgreen}{rgb}{0,0.5,0}
    33 
    34 
    34 \usepackage{listings}
    35 \usepackage{listings}
    35 \lstset{language=Python,
    36 \lstset{language=Python,
   145   \item Arrays
   146   \item Arrays
   146 \end{itemize}
   147 \end{itemize}
   147 \end{itemize}
   148 \end{itemize}
   148 \end{frame}
   149 \end{frame}
   149 
   150 
   150 \begin{frame}
   151 \begin{frame}[fragile]
       
   152 \frametitle{Reading pendulum.txt}
       
   153 \begin{itemize}
       
   154   \item The file has two columns
       
   155   \item Column1 - L; Column2 - T
       
   156 \end{itemize}
       
   157 \begin{lstlisting}
       
   158 In []: L = []
       
   159 In []: T = []
       
   160 In []: for line in open('pendulum.txt'):
       
   161   ....     len, t = line.split()
       
   162   ....     L.append(float(len))
       
   163   ....     T.append(float(t))
       
   164 \end{lstlisting}
       
   165 We now have two lists L and T
       
   166 \end{frame}
       
   167 
       
   168 \begin{frame}[fragile]
       
   169 \frametitle{Calculating T^2}
       
   170 \begin{itemize}
       
   171 \item Each element of the list T must be squared
       
   172 \item Iterating over each element of the list works
       
   173 \item But very slow \ldots
       
   174 \item Instead, we use arrays
       
   175 \end{itemize}
       
   176 \begin{lstlisting}
       
   177 In []: array(L)
       
   178 In []: T = array(T)
       
   179 In []: Tsq = T*T
       
   180 In []: plot(L, Tsq, 'o')
       
   181 \end{lstlisting}
       
   182 \end{frame}
       
   183 
       
   184 \begin{frame}[fragile]
   151 \frametitle{Arrays}
   185 \frametitle{Arrays}
       
   186 \begin{itemize}
       
   187 \item T is now a \typ{numpy array}
       
   188 \item \typ{numpy} arrays are very efficient and powerful 
       
   189 \item Very easy to perform element-wise operations
       
   190 \item \typ{+, -, *, /, \%}
       
   191 \item More about arrays later
       
   192 \end{itemize}
       
   193 \end{frame}
       
   194 
       
   195 \begin{frame}[fragile]
       
   196 \frametitle{Least Square Polynomial}
       
   197 \begin{enumerate}
       
   198 \item $T^2 = \frac{4\pi^2}{g}L$
       
   199 \item $T^2$ and $L$ have a linear relationship
       
   200 \item We find an approximate solution to $Ax = y$, where A is the Van der Monde matrix to get coefficients of the least squares fit line. 
       
   201 \end{enumerate}
       
   202 \end{frame}
       
   203 
       
   204 \begin{frame}[fragile]
       
   205 \frametitle{Van der Monde Matrix}
       
   206 Van der Monde matrix of order M
       
   207 \begin{equation*}
       
   208   \begin{bmatrix}
       
   209   l_1^{M-1} & \ldots & l_1 & 1 \\
       
   210   l_2^{M-1} & \ldots &l_2 & 1 \\
       
   211   \vdots & \ldots & \vdots & \vdots\\
       
   212   l_N^{M-1} & \ldots & l_N & 1 \\
       
   213   \end{bmatrix}
       
   214 \end{equation*}
       
   215 \begin{lstlisting}
       
   216 In []: A=vander(L,2)
       
   217 \end{lstlisting}
       
   218 \end{frame}
       
   219 
       
   220 \begin{frame}[fragile]
       
   221 \frametitle{Least Square Fit Line}
       
   222 \begin{itemize}
       
   223 \item We use the \typ{lstsq} function of pylab
       
   224 \item It returns the 
       
   225 \begin{enumerate}
       
   226 \item Least squares solution
       
   227 \item Sum of residues
       
   228 \item Rank of matrix A
       
   229 \item Singular values of A
       
   230 \end{enumerate}
       
   231 \end{itemize}
       
   232 \begin{lstlisting}
       
   233 coeffs, res, rank, sing = lstsq(A,Tsq)
       
   234 \end{lstlisting}
       
   235 \end{frame}
       
   236 
       
   237 \begin{frame}[fragile]
       
   238 \frametitle{Least Square Fit Line \ldots}
       
   239 \begin{itemize}
       
   240 \item Use the poly1d function of pylab, to create a function for the line equation using the coefficients obtained
       
   241 \begin{lstlisting}
       
   242 p=poly1d(coeffs)
       
   243 \end{lstlisting}
       
   244 \item Get new T^2 values using the function \typ{p} obtained
       
   245 \begin{lstlisting}
       
   246 Tline = p(L)
       
   247 \end{lstlisting}
       
   248 \item Now plot Tline vs. L, to get the Least squares fit line. 
       
   249 \begin{lstlisting}
       
   250 plot(L, Tline)
       
   251 \end{lstlisting}
   152 \end{frame}
   252 \end{frame}
   153 
   253 
   154 \end{document}
   254 \end{document}
   155 
   255 
   156 Least squares: Smooth curve fit. 
   256 Least squares: Smooth curve fit.