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{infolines} |
|
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 |
|
32 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
33 |
|
34 \usepackage{listings} |
|
35 \lstset{language=Python, |
|
36 basicstyle=\ttfamily\bfseries, |
|
37 commentstyle=\color{red}\itshape, |
|
38 stringstyle=\color{darkgreen}, |
|
39 showstringspaces=false, |
|
40 keywordstyle=\color{blue}\bfseries} |
|
41 |
|
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
43 % Macros |
|
44 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
45 \newcommand{\emphbar}[1] |
|
46 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
47 {#1} |
|
48 \end{beamercolorbox} |
|
49 } |
|
50 \newcounter{time} |
|
51 \setcounter{time}{0} |
|
52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
53 |
|
54 \newcommand{\typ}[1]{\texttt{#1}} |
|
55 |
|
56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
57 |
|
58 %%% This is from Fernando's setup. |
|
59 % \usepackage{color} |
|
60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
61 % % Use and configure listings package for nicely formatted code |
|
62 % \usepackage{listings} |
|
63 % \lstset{ |
|
64 % language=Python, |
|
65 % basicstyle=\small\ttfamily, |
|
66 % commentstyle=\ttfamily\color{blue}, |
|
67 % stringstyle=\ttfamily\color{orange}, |
|
68 % showstringspaces=false, |
|
69 % breaklines=true, |
|
70 % postbreak = \space\dots |
|
71 % } |
|
72 |
|
73 |
|
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 % Title page |
|
76 \title[Exercises]{Exercises} |
|
77 |
|
78 \author[FOSSEE] {FOSSEE} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {11 January, 2010\\Day 1, Session 5} |
|
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 |
|
99 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
100 % the following command: |
|
101 %\beamerdefaultoverlayspecification{<+->} |
|
102 |
|
103 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
104 |
|
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
106 % DOCUMENT STARTS |
|
107 \begin{document} |
|
108 |
|
109 \begin{frame} |
|
110 \titlepage |
|
111 \end{frame} |
|
112 |
|
113 |
|
114 \begin{frame}[fragile] |
|
115 \frametitle{Problem 1} |
|
116 \begin{columns} |
|
117 \column{0.5\textwidth} |
|
118 \hspace*{-0.5in} |
|
119 \includegraphics[height=2in, interpolate=true]{data/L-Tsq.png} |
|
120 \column{0.45\textwidth} |
|
121 \begin{block}{Example code} |
|
122 \tiny |
|
123 \begin{lstlisting} |
|
124 l = [] |
|
125 t = [] |
|
126 for line in open('pendulum.txt'): |
|
127 point = line.split() |
|
128 l.append(float(point[0])) |
|
129 t.append(float(point[1])) |
|
130 tsq = [] |
|
131 for time in t: |
|
132 tsq.append(time*time) |
|
133 plot(l, tsq, '.') |
|
134 \end{lstlisting} |
|
135 \end{block} |
|
136 \end{columns} |
|
137 \begin{block}{Problem Statement} |
|
138 Tweak above code to plot data in file 'location.txt'. |
|
139 \end{block} |
|
140 \end{frame} |
|
141 |
|
142 \begin{frame} |
|
143 \frametitle{Problem 1 cont...} |
|
144 \begin{itemize} |
|
145 \item Label both the axes. |
|
146 \item What kind of motion is this? |
|
147 \item Title the graph accordingly. |
|
148 \item Annotate the position where vertical velocity is zero. |
|
149 \end{itemize} |
|
150 \end{frame} |
|
151 |
|
152 \begin{frame}[fragile] |
|
153 \frametitle{Problem 2} |
|
154 \begin{columns} |
|
155 \column{0.5\textwidth} |
|
156 \hspace*{-0.5in} |
|
157 \includegraphics[height=2in, interpolate=true]{data/points} |
|
158 \column{0.45\textwidth} |
|
159 \begin{block}{Line between two points} |
|
160 \tiny |
|
161 \begin{lstlisting} |
|
162 In []: x = [1, 5] |
|
163 In []: y = [1, 4] |
|
164 In []: plot(x, y) |
|
165 \end{lstlisting} |
|
166 \end{block} |
|
167 \end{columns} |
|
168 Line can be plotted using arrays of coordinates. |
|
169 \pause |
|
170 \begin{block}{Problem statement} |
|
171 Write a Program that plots a regular n-gon(Let n = 5). |
|
172 \end{block} |
|
173 \end{frame} |
|
174 |
|
175 |
|
176 \begin{frame}[fragile] |
|
177 \frametitle{Problem 3} |
|
178 \begin{columns} |
|
179 \column{0.5\textwidth} |
|
180 \hspace*{-0.5in} |
|
181 \includegraphics[height=2in, interpolate=true]{data/damp} |
|
182 \column{0.45\textwidth} |
|
183 \begin{block}{Damped Oscillation} |
|
184 \tiny |
|
185 \begin{lstlisting} |
|
186 In []: x = linspace(0, 4*pi) |
|
187 In []: plot(x, exp(x/10)*sin(x)) |
|
188 \end{lstlisting} |
|
189 \end{block} |
|
190 \end{columns} |
|
191 \end{frame} |
|
192 |
|
193 \begin{frame}[fragile] |
|
194 \frametitle{Problem 3 cont...} |
|
195 Create a sequence of images in which the damped oscillator($e^{x/10}sin(x)$) slowly evolves over time. |
|
196 \begin{columns} |
|
197 \column{0.35\textwidth} |
|
198 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot2} |
|
199 \column{0.35\textwidth} |
|
200 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot4} |
|
201 \column{0.35\textwidth} |
|
202 \includegraphics[width=1.5in,height=1.5in, interpolate=true]{data/plot6} |
|
203 \end{columns} |
|
204 \begin{block}{Hint} |
|
205 \small |
|
206 \begin{lstlisting} |
|
207 savefig('plot'+str(i)+'.png') #i is int variable |
|
208 \end{lstlisting} |
|
209 \end{block} |
|
210 \end{frame} |
|
211 |
|
212 \begin{frame}[fragile] |
|
213 \frametitle{Problem 4} |
|
214 \begin{lstlisting} |
|
215 In []: x = imread('smoothing.png') |
|
216 In []: x.shape |
|
217 Out[]: (256, 256) |
|
218 In []: imshow(x,cmap=cm.gray) |
|
219 \end{lstlisting} |
|
220 \emphbar{Replace each pixel with mean of neighboring pixels} |
|
221 \begin{center} |
|
222 \includegraphics[height=1in, interpolate=true]{data/neighbour} |
|
223 \end{center} |
|
224 \end{frame} |
|
225 |
|
226 \begin{frame} |
|
227 \begin{center} |
|
228 \includegraphics[height=3in, interpolate=true]{data/smoothing} |
|
229 \end{center} |
|
230 \end{frame} |
|
231 |
|
232 \begin{frame}[fragile] |
|
233 \frametitle{Problem 4: Approach} |
|
234 For \typ{y} being resultant image: |
|
235 \begin{lstlisting} |
|
236 y[1, 1] = x[0, 1]/4 + x[1, 0]/4 |
|
237 + x[2, 1]/4 + x[1, 2]/4 |
|
238 \end{lstlisting} |
|
239 \begin{columns} |
|
240 \column{0.45\textwidth} |
|
241 \hspace*{-0.5in} |
|
242 \includegraphics[height=1.5in, interpolate=true]{data/smoothing} |
|
243 \column{0.45\textwidth} |
|
244 \hspace*{-0.5in} |
|
245 \includegraphics[height=1.5in, interpolate=true]{data/after-filter} |
|
246 \end{columns} |
|
247 \begin{block}{Hint:} |
|
248 Use array Slicing. |
|
249 \end{block} |
|
250 \end{frame} |
|
251 |
|
252 \begin{frame}[fragile] |
|
253 \frametitle{Solution} |
|
254 \begin{lstlisting} |
|
255 In []: y = zeros_like(x) |
|
256 In []: y[1:-1,1:-1] = x[:-2,1:-1]/4+ |
|
257 x[2:,1:-1]/4+ |
|
258 x[1:-1,2:]/4+ |
|
259 x[1:-1,:-2]/4 |
|
260 In []: imshow(y,cmap=cm.gray) |
|
261 \end{lstlisting} |
|
262 \end{frame} |
|
263 |
|
264 |
|
265 \end{document} |
|
266 |
|
267 %% \begin{frame} |
|
268 %% \frametitle{Problem 4} |
|
269 %% Legendre polynomials $P_n(x)$ are defined by the following recurrence relation |
|
270 |
|
271 %% \center{$(n+1)P_{n+1}(x) - (2n+1)xP_n(x) + nP_{n-1}(x) = 0$}\\ |
|
272 |
|
273 %% with $P_0(x) = 1$, $P_1(x) = x$ and $P_2(x) = (3x^2 - 1)/2$. Compute the next three |
|
274 %% Legendre polynomials and plot all 6 over the interval [-1,1]. |
|
275 %% \end{frame} |
|
276 |
|
277 %% \begin{frame}[fragile] |
|
278 %% \frametitle{Problem Set 5} |
|
279 %% \begin{columns} |
|
280 %% \column{0.6\textwidth} |
|
281 %% \small{ |
|
282 %% \begin{itemize} |
|
283 %% \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. |
|
284 %% \end{itemize}} |
|
285 %% \column{0.35\textwidth} |
|
286 %% \hspace*{-0.5in} |
|
287 %% \includegraphics[height=1.6in, interpolate=true]{data/cobweb} |
|
288 %% \end{columns} |
|
289 %% \end{frame} |
|
290 |
|
291 %% \begin{frame} |
|
292 %% \frametitle{Problem Set 5.3} |
|
293 %% Plot the cobweb plot as follows: |
|
294 %% \begin{enumerate} |
|
295 %% \item Start at $(x_0, 0)$ ($\implies$ i=0) |
|
296 %% \item Draw a line to $(x_i, f(x_i))$ |
|
297 %% \item Set $x_{i+1} = f(x_i)$ |
|
298 %% \item Draw a line to $(x_{i+1}, x_{i+1})$ |
|
299 %% \item $(i\implies i+1)$ |
|
300 %% \item Repeat from 2 for as long as you want |
|
301 %% \end{enumerate} |
|
302 %% \inctime{20} |
|
303 %% \end{frame} |
|