|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 % Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[14pt,compress]{beamer} |
|
9 %\documentclass[draft]{beamer} |
|
10 %\documentclass[compress,handout]{beamer} |
|
11 %\usepackage{pgfpages} |
|
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
|
14 % Modified from: generic-ornate-15min-45min.de.tex |
|
15 \mode<presentation> |
|
16 { |
|
17 \usetheme{Warsaw} |
|
18 \useoutertheme{split} |
|
19 \setbeamercovered{transparent} |
|
20 } |
|
21 |
|
22 \usepackage[english]{babel} |
|
23 \usepackage[latin1]{inputenc} |
|
24 %\usepackage{times} |
|
25 \usepackage[T1]{fontenc} |
|
26 |
|
27 % Taken from Fernando's slides. |
|
28 \usepackage{ae,aecompl} |
|
29 \usepackage{mathpazo,courier,euler} |
|
30 \usepackage[scaled=.95]{helvet} |
|
31 \usepackage{amsmath} |
|
32 |
|
33 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
34 |
|
35 \usepackage{listings} |
|
36 \lstset{language=Python, |
|
37 basicstyle=\ttfamily\bfseries, |
|
38 commentstyle=\color{red}\itshape, |
|
39 stringstyle=\color{darkgreen}, |
|
40 showstringspaces=false, |
|
41 keywordstyle=\color{blue}\bfseries} |
|
42 |
|
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
44 % Macros |
|
45 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
46 \newcommand{\emphbar}[1] |
|
47 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
48 {#1} |
|
49 \end{beamercolorbox} |
|
50 } |
|
51 \newcounter{time} |
|
52 \setcounter{time}{0} |
|
53 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
54 |
|
55 \newcommand{\typ}[1]{\lstinline{#1}} |
|
56 |
|
57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
58 |
|
59 %%% This is from Fernando's setup. |
|
60 % \usepackage{color} |
|
61 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
62 % % Use and configure listings package for nicely formatted code |
|
63 % \usepackage{listings} |
|
64 % \lstset{ |
|
65 % language=Python, |
|
66 % basicstyle=\small\ttfamily, |
|
67 % commentstyle=\ttfamily\color{blue}, |
|
68 % stringstyle=\ttfamily\color{orange}, |
|
69 % showstringspaces=false, |
|
70 % breaklines=true, |
|
71 % postbreak = \space\dots |
|
72 % } |
|
73 |
|
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 % Title page |
|
76 \title[]{Arrays \& Least Squares Fit} |
|
77 |
|
78 \author[FOSSEE] {FOSSEE} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {31, October 2009} |
|
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
83 |
|
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
85 %\logo{\pgfuseimage{iitmlogo}} |
|
86 |
|
87 |
|
88 %% Delete this, if you do not want the table of contents to pop up at |
|
89 %% the beginning of each subsection: |
|
90 \AtBeginSubsection[] |
|
91 { |
|
92 \begin{frame}<beamer> |
|
93 \frametitle{Outline} |
|
94 \tableofcontents[currentsection,currentsubsection] |
|
95 \end{frame} |
|
96 } |
|
97 |
|
98 \AtBeginSection[] |
|
99 { |
|
100 \begin{frame}<beamer> |
|
101 \frametitle{Outline} |
|
102 \tableofcontents[currentsection,currentsubsection] |
|
103 \end{frame} |
|
104 } |
|
105 |
|
106 \newcommand{\num}{\texttt{numpy}} |
|
107 |
|
108 |
|
109 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
110 % the following command: |
|
111 %\beamerdefaultoverlayspecification{<+->} |
|
112 |
|
113 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
114 |
|
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
116 % DOCUMENT STARTS |
|
117 \begin{document} |
|
118 |
|
119 \begin{frame} |
|
120 \maketitle |
|
121 \end{frame} |
|
122 |
|
123 %% \begin{frame} |
|
124 %% \frametitle{Outline} |
|
125 %% \tableofcontents |
|
126 %% % You might wish to add the option [pausesections] |
|
127 %% \end{frame} |
|
128 |
|
129 \begin{frame} |
|
130 \frametitle{Least Squares Fit} |
|
131 In this session - |
|
132 \begin{itemize} |
|
133 \item We shall plot a least squares fit curve for time-period(T) squared vs. length(L) plot of a Simple Pendulum. |
|
134 \item Given a file containing L and T values |
|
135 \end{itemize} |
|
136 \end{frame} |
|
137 |
|
138 \begin{frame}[fragile] |
|
139 \frametitle{Least Squares Fit \ldots} |
|
140 Machinery Required - |
|
141 \begin{itemize} |
|
142 \item Reading files and parsing data |
|
143 \item Plotting points, lines |
|
144 \item Calculating the Coefficients of the Least Squares Fit curve |
|
145 \begin{itemize} |
|
146 \item Arrays |
|
147 \end{itemize} |
|
148 \end{itemize} |
|
149 \end{frame} |
|
150 |
|
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] |
|
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} |
|
252 \end{itemize} |
|
253 \end{frame} |
|
254 |
|
255 \end{document} |
|
256 |
|
257 Least squares: Smooth curve fit. |
|
258 Array Operations: Mean, average (etc region wise like district wise and state wise from SSLC.txt) |
|
259 Subject wise average. Introduce idea of dictionary. |
|
260 |
|
261 Session 3 |
|
262 |
|
263 import scipy |
|
264 from scipy import linalg. |
|
265 |
|
266 choose some meaningful plot. ?? |
|
267 Newton's law of cooling. |
|
268 u, v, f - optics |
|
269 hooke's law |
|
270 Least fit curves. |
|
271 |
|
272 |
|
273 Choose a named problem. |
|
274 ODE - first order. Whatever. |
|
275 |
|
276 |
|
277 arrays, etc etc. |
|
278 sum, average, mean. whatever. statistical |
|
279 sslc data |
|
280 numpy load text?? |