day1/session3.tex
changeset 167 5f13be28532d
parent 166 ddfd95133adc
parent 164 b03c0d1be31f
child 170 36ed5cdf5fde
equal deleted inserted replaced
166:ddfd95133adc 167:5f13be28532d
    71 %    postbreak = \space\dots
    71 %    postbreak = \space\dots
    72 % }
    72 % }
    73 
    73 
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 % Title page
    75 % Title page
    76 \title[]{Arrays \& Least Squares Fit}
    76 \title[]{Least Squares Fit\\Statistical Plotting}
    77 
    77 
    78 \author[FOSSEE] {FOSSEE}
    78 \author[FOSSEE] {FOSSEE}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {31, October 2009\\Day 1, Session 3}
    81 \date[] {31, October 2009\\Day 1, Session 3}
   124 %%   \frametitle{Outline}
   124 %%   \frametitle{Outline}
   125 %%   \tableofcontents
   125 %%   \tableofcontents
   126 %%   % You might wish to add the option [pausesections]
   126 %%   % You might wish to add the option [pausesections]
   127 %% \end{frame}
   127 %% \end{frame}
   128 
   128 
   129 
   129 \begin{frame}[fragile]
   130 \begin{frame}
       
   131 \frametitle{Least Squares Fit}
   130 \frametitle{Least Squares Fit}
   132 \begin{itemize}
   131 \vspace{-0.15in}
   133 \item Plot a least squares fit line
   132 \begin{figure}
   134 \end{itemize}
   133 \includegraphics[width=4in]{data/least-sq-fit.png}
   135 \end{frame}
   134 \end{figure}
   136 
   135 \end{frame}
   137 \begin{frame}[fragile]
   136 
   138 \frametitle{Least Squares Fit \ldots}
   137 \begin{frame}[fragile]
   139 Machinery Required -
   138 \frametitle{Calculating $T^2$ Efficiently}
   140 \begin{itemize}
   139 \begin{lstlisting}
   141 \item Reading files and parsing data
   140 In []: for t in T:
   142 \item Plotting points, lines
   141  ....:     Tsq.append(t*t)
   143 \item Calculating the Coefficients of the Least Squares Fit curve
   142 \end{lstlisting}
   144 \begin{itemize}
   143 \begin{itemize}
   145   \item Arrays
   144 \item This is not very efficient
   146 \end{itemize}
   145 \item We use arrays to make it efficient
   147 \end{itemize}
       
   148 \end{frame}
       
   149 
       
   150 
       
   151 \begin{frame}[fragile]
       
   152 \frametitle{Calculating $T^2$}
       
   153 \begin{itemize}
       
   154 \item Each element of the list T must be squared
       
   155 \item Iterating over each element of the list works
       
   156 \item But very slow \ldots
       
   157 \item Instead, we use arrays
       
   158 \end{itemize}
   146 \end{itemize}
   159 \begin{lstlisting}
   147 \begin{lstlisting}
   160 In []: L = array(L)
   148 In []: L = array(L)
   161 In []: T = array(T)
   149 In []: T = array(T)
   162 In []: Tsq = T*T
   150 In []: Tsq = T*T
   165 \end{frame}
   153 \end{frame}
   166 
   154 
   167 \begin{frame}[fragile]
   155 \begin{frame}[fragile]
   168 \frametitle{Arrays}
   156 \frametitle{Arrays}
   169 \begin{itemize}
   157 \begin{itemize}
   170 \item T is now a \typ{numpy array}
   158 \item \typ{T} and \typ{L} are now arrays
   171 \item \typ{numpy} arrays are very efficient and powerful 
   159 \item arrays are very efficient and powerful 
   172 \item Very easy to perform element-wise operations
   160 \item Very easy to perform element-wise operations
   173 \item \typ{+, -, *, /, \%}
   161 \item \typ{+, -, *, /, \%}
   174 \item More about arrays later
   162 \item More about arrays later
   175 \end{itemize}
   163 \end{itemize}
   176 \end{frame}
   164 \end{frame}
   177 
   165 
   178 \begin{frame}[fragile]
   166 \begin{frame}
   179 \frametitle{Least Square Polynomial}
   167 \frametitle{Least Square Fit Curve}
   180 \begin{enumerate}
   168 \begin{itemize}
   181 \item $T^2 = \frac{4\pi^2}{g}L$
       
   182 \item $T^2$ and $L$ have a linear relationship
   169 \item $T^2$ and $L$ have a linear relationship
   183 \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. 
   170 \item Hence, Least Square Fit Curve is a line
   184 \end{enumerate}
   171 \item we shall use the \typ{lstsq} function
       
   172 \end{itemize}
       
   173 \end{frame}
       
   174 
       
   175 \begin{frame}[fragile]
       
   176 \frametitle{\typ{lstsq}}
       
   177 \begin{itemize}
       
   178 \item We need to fit a line through points for the equation $T^2 = m \cdot L+c$
       
   179 \item The equation can be re-written as $T^2 = A \cdot p$
       
   180 \item where A is   
       
   181   $\begin{bmatrix}
       
   182   L_1 & 1 \\
       
   183   L_2 & 1 \\
       
   184   \vdots & \vdots\\
       
   185   L_N & 1 \\
       
   186   \end{bmatrix}$
       
   187   and p is 
       
   188   $\begin{bmatrix}
       
   189   m\\
       
   190   c\\
       
   191   \end{bmatrix}$
       
   192 \item We need to find $p$ to plot the line
       
   193 \end{itemize}
   185 \end{frame}
   194 \end{frame}
   186 
   195 
   187 \begin{frame}[fragile]
   196 \begin{frame}[fragile]
   188 \frametitle{Van der Monde Matrix}
   197 \frametitle{Van der Monde Matrix}
       
   198 \begin{itemize}
       
   199 \item A is also called a Van der Monde matrix
       
   200 \item It can be generated using \typ{vander}
       
   201 \end{itemize}
   189 Van der Monde matrix of order M
   202 Van der Monde matrix of order M
   190 \begin{equation*}
   203 \begin{equation*}
   191   \begin{bmatrix}
   204   \begin{bmatrix}
   192   l_1^{M-1} & \ldots & l_1 & 1 \\
   205   l_1^{M-1} & \ldots & l_1 & 1 \\
   193   l_2^{M-1} & \ldots &l_2 & 1 \\
   206   l_2^{M-1} & \ldots &l_2 & 1 \\
   194   \vdots & \ldots & \vdots & \vdots\\
   207   \vdots & \ldots & \vdots & \vdots\\
   195   l_N^{M-1} & \ldots & l_N & 1 \\
   208   l_N^{M-1} & \ldots & l_N & 1 \\
   196   \end{bmatrix}
   209   \end{bmatrix}
   197 \end{equation*}
   210 \end{equation*}
   198 \begin{lstlisting}
   211 \begin{lstlisting}
   199 In []: A=vander(L,2)
   212 In []: A = vander(L,2)
   200 \end{lstlisting}
   213 \end{lstlisting}
   201 \end{frame}
   214 \end{frame}
   202 
   215 
   203 \begin{frame}[fragile]
   216 \begin{frame}[fragile]
   204 \frametitle{Least Square Fit Line}
   217 \frametitle{\typ{lstsq} \ldots}
   205 \begin{itemize}
   218 \begin{itemize}
   206 \item We use the \typ{lstsq} function of pylab
   219 \item Now use the \typ{lstsq} function
   207 \item It returns the 
   220 \item Along with a lot of things, it returns the least squares solution
   208 \begin{enumerate}
       
   209 \item Least squares solution
       
   210 \item Sum of residues
       
   211 \item Rank of matrix A
       
   212 \item Singular values of A
       
   213 \end{enumerate}
       
   214 \end{itemize}
   221 \end{itemize}
   215 \begin{lstlisting}
   222 \begin{lstlisting}
   216 In []: coef, res, r, s = lstsq(A,Tsq)
   223 In []: coef, res, r, s = lstsq(A,Tsq)
   217 \end{lstlisting}
   224 \end{lstlisting}
   218 \end{frame}
   225 \end{frame}
   219 
   226 
   220 \begin{frame}[fragile]
   227 \begin{frame}[fragile]
   221 \frametitle{Least Square Fit Line \ldots}
   228 \frametitle{Least Square Fit Line \ldots}
   222 \begin{itemize}
   229 We get the points of the line from \typ{coef}
   223 \item Use the poly1d function of pylab, to create a function for the line equation using the coefficients obtained
   230 \begin{lstlisting}
   224 \begin{lstlisting}
   231 In []: Tline = coef[0]*L + coef[1]
   225 In []: p=poly1d(coef)
   232 \end{lstlisting}
   226 \end{lstlisting}
   233 \begin{itemize}
   227 \item Get new $T^2$ values using the function \typ{p} obtained
       
   228 \begin{lstlisting}
       
   229 In []: Tline = p(L)
       
   230 \end{lstlisting}
       
   231 \item Now plot Tline vs. L, to get the Least squares fit line. 
   234 \item Now plot Tline vs. L, to get the Least squares fit line. 
       
   235 \end{itemize}
   232 \begin{lstlisting}
   236 \begin{lstlisting}
   233 In []: plot(L, Tline)
   237 In []: plot(L, Tline)
   234 \end{lstlisting}
   238 \end{lstlisting}
   235 \end{itemize}
       
   236 \end{frame}
   239 \end{frame}
   237 
   240 
   238 \begin{frame}
   241 \begin{frame}
   239   \frametitle{Statistical Analysis and Parsing}
   242   \frametitle{Statistical Analysis and Parsing}
   240   Read the data supplied in \emph{sslc1.txt} and obtain the following statistics:
   243   Read the data supplied in \emph{sslc1.txt} and obtain the following statistics: