Added Least Square Fitting to Session3.
--- a/day1/session3.tex Wed Oct 14 12:52:42 2009 +0530
+++ b/day1/session3.tex Wed Oct 14 16:34:30 2009 +0530
@@ -28,6 +28,7 @@
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}
+\usepackage{amsmath}
\definecolor{darkgreen}{rgb}{0,0.5,0}
@@ -147,8 +148,107 @@
\end{itemize}
\end{frame}
-\begin{frame}
+\begin{frame}[fragile]
+\frametitle{Reading pendulum.txt}
+\begin{itemize}
+ \item The file has two columns
+ \item Column1 - L; Column2 - T
+\end{itemize}
+\begin{lstlisting}
+In []: L = []
+In []: T = []
+In []: for line in open('pendulum.txt'):
+ .... len, t = line.split()
+ .... L.append(float(len))
+ .... T.append(float(t))
+\end{lstlisting}
+We now have two lists L and T
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Calculating T^2}
+\begin{itemize}
+\item Each element of the list T must be squared
+\item Iterating over each element of the list works
+\item But very slow \ldots
+\item Instead, we use arrays
+\end{itemize}
+\begin{lstlisting}
+In []: array(L)
+In []: T = array(T)
+In []: Tsq = T*T
+In []: plot(L, Tsq, 'o')
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
\frametitle{Arrays}
+\begin{itemize}
+\item T is now a \typ{numpy array}
+\item \typ{numpy} arrays are very efficient and powerful
+\item Very easy to perform element-wise operations
+\item \typ{+, -, *, /, \%}
+\item More about arrays later
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Least Square Polynomial}
+\begin{enumerate}
+\item $T^2 = \frac{4\pi^2}{g}L$
+\item $T^2$ and $L$ have a linear relationship
+\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.
+\end{enumerate}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Van der Monde Matrix}
+Van der Monde matrix of order M
+\begin{equation*}
+ \begin{bmatrix}
+ l_1^{M-1} & \ldots & l_1 & 1 \\
+ l_2^{M-1} & \ldots &l_2 & 1 \\
+ \vdots & \ldots & \vdots & \vdots\\
+ l_N^{M-1} & \ldots & l_N & 1 \\
+ \end{bmatrix}
+\end{equation*}
+\begin{lstlisting}
+In []: A=vander(L,2)
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Least Square Fit Line}
+\begin{itemize}
+\item We use the \typ{lstsq} function of pylab
+\item It returns the
+\begin{enumerate}
+\item Least squares solution
+\item Sum of residues
+\item Rank of matrix A
+\item Singular values of A
+\end{enumerate}
+\end{itemize}
+\begin{lstlisting}
+coeffs, res, rank, sing = lstsq(A,Tsq)
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Least Square Fit Line \ldots}
+\begin{itemize}
+\item Use the poly1d function of pylab, to create a function for the line equation using the coefficients obtained
+\begin{lstlisting}
+p=poly1d(coeffs)
+\end{lstlisting}
+\item Get new T^2 values using the function \typ{p} obtained
+\begin{lstlisting}
+Tline = p(L)
+\end{lstlisting}
+\item Now plot Tline vs. L, to get the Least squares fit line.
+\begin{lstlisting}
+plot(L, Tline)
+\end{lstlisting}
\end{frame}
\end{document}