72 % } |
72 % } |
73 |
73 |
74 |
74 |
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
76 % Title page |
76 % Title page |
77 \title[Matrices \& Equations]{Python for Science and Engg: Matrices \& Solution of equations} |
77 \title[Matrices \& Equations]{Python for Science and Engg: Matrices, Least Square Fit, \& Solution of equations} |
78 |
78 |
79 \author[FOSSEE] {FOSSEE} |
79 \author[FOSSEE] {FOSSEE} |
80 |
80 |
81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
82 \date[] {31, October 2009\\Day 1, Session 4} |
82 \date[] {31, October 2009\\Day 1, Session 4} |
126 |
126 |
127 \section{Matrices} |
127 \section{Matrices} |
128 |
128 |
129 \begin{frame} |
129 \begin{frame} |
130 \frametitle{Matrices: Introduction} |
130 \frametitle{Matrices: Introduction} |
131 Let us now look at matrices in detail.\\ |
|
132 \alert{All matrix operations are done using \kwrd{arrays}} |
131 \alert{All matrix operations are done using \kwrd{arrays}} |
133 \end{frame} |
132 \end{frame} |
134 |
133 |
135 \subsection{Initializing} |
|
136 \begin{frame}[fragile] |
134 \begin{frame}[fragile] |
137 \frametitle{Matrices: Initializing} |
135 \frametitle{Matrices: Initializing} |
138 \begin{lstlisting} |
136 \begin{lstlisting} |
139 In []: A = array([[ 1, 1, 2, -1], |
137 In []: A = array([[ 1, 1, 2, -1], |
140 [ 2, 5, -1, -9], |
138 [ 2, 5, -1, -9], |
148 [ 1, -3, 2, 7]]) |
146 [ 1, -3, 2, 7]]) |
149 \end{lstlisting} |
147 \end{lstlisting} |
150 \end{frame} |
148 \end{frame} |
151 |
149 |
152 \begin{frame}[fragile] |
150 \begin{frame}[fragile] |
153 \frametitle{Accessing elements of matrices} |
151 \frametitle{Accessing elements} |
154 \begin{small} |
|
155 \begin{lstlisting} |
152 \begin{lstlisting} |
156 In []: C = array([[1,1,2], |
153 In []: C = array([[1,1,2], |
157 [2,4,1], |
154 [2,4,1], |
158 [-1,3,7]]) |
155 [-1,3,7]]) |
|
156 |
|
157 In []: C[1][2] |
|
158 Out[]: 1 |
|
159 |
159 In []: C[1,2] |
160 In []: C[1,2] |
160 Out[]: 1 |
161 Out[]: 1 |
161 |
162 |
162 In []: C[1] |
163 In []: C[1] |
163 Out[]: array([2, 4, 1]) |
164 Out[]: array([2, 4, 1]) |
164 |
165 \end{lstlisting} |
|
166 \end{frame} |
|
167 |
|
168 \begin{frame}[fragile] |
|
169 \frametitle{Changing elements} |
|
170 \begin{small} |
|
171 \begin{lstlisting} |
165 In []: C[1,1] = -2 |
172 In []: C[1,1] = -2 |
166 In []: C |
173 In []: C |
167 Out[]: |
174 Out[]: |
168 array([[ 1, 1, 2], |
175 array([[ 1, 1, 2], |
169 [ 2, -2, 1], |
176 [ 2, -2, 1], |
170 [-1, 3, 7]]) |
177 [-1, 3, 7]]) |
|
178 |
|
179 In []: C[1] = [0,0,0] |
|
180 In []: C |
|
181 Out[]: |
|
182 array([[ 1, 1, 2], |
|
183 [ 0, 0, 0], |
|
184 [-1, 3, 7]]) |
|
185 \end{lstlisting} |
|
186 \end{small} |
|
187 How to change one \alert{column}? |
|
188 \end{frame} |
|
189 |
|
190 \begin{frame}[fragile] |
|
191 \frametitle{Slicing} |
|
192 \begin{small} |
|
193 \begin{lstlisting} |
|
194 In []: C[:,1] |
|
195 Out[]: array([1, 0, 3]) |
|
196 |
|
197 In []: C[1,:] |
|
198 Out[]: array([0, 0, 0]) |
|
199 |
|
200 In []: C[0:2,:] |
|
201 Out[]: |
|
202 array([[1, 1, 2], |
|
203 [0, 0, 0]]) |
|
204 |
|
205 In []: C[1:3,:] |
|
206 Out[]: |
|
207 array([[ 0, 0, 0], |
|
208 [-1, 3, 7]]) |
171 \end{lstlisting} |
209 \end{lstlisting} |
172 \end{small} |
210 \end{small} |
|
211 \end{frame} |
|
212 |
|
213 \begin{frame}[fragile] |
|
214 \frametitle{Slicing \ldots} |
|
215 \begin{small} |
|
216 \begin{lstlisting} |
|
217 In []: C[:2,:] |
|
218 Out[]: |
|
219 array([[1, 1, 2], |
|
220 [0, 0, 0]]) |
|
221 |
|
222 In []: C[1:,:] |
|
223 Out[]: |
|
224 array([[ 0, 0, 0], |
|
225 [-1, 3, 7]]) |
|
226 |
|
227 In []: C[1:,:2] |
|
228 Out[]: |
|
229 array([[ 0, 0], |
|
230 [-1, 3]]) |
|
231 \end{lstlisting} |
|
232 |
|
233 \end{small} |
|
234 \end{frame} |
|
235 |
|
236 \begin{frame}[fragile] |
|
237 \frametitle{Striding} |
|
238 \begin{lstlisting} |
|
239 \end{lstlisting} |
|
240 \end{frame} |
|
241 |
|
242 \begin{frame}[fragile] |
|
243 \frametitle{Slicing \& Striding Exercises} |
|
244 \begin{lstlisting} |
|
245 \end{lstlisting} |
173 \end{frame} |
246 \end{frame} |
174 |
247 |
175 \subsection{Basic Operations} |
248 \subsection{Basic Operations} |
176 |
249 |
177 \begin{frame}[fragile] |
250 \begin{frame}[fragile] |
277 Out[]: array([-1., 8., -1.]) |
350 Out[]: array([-1., 8., -1.]) |
278 \end{lstlisting} |
351 \end{lstlisting} |
279 \end{small} |
352 \end{small} |
280 \end{frame} |
353 \end{frame} |
281 |
354 |
282 \begin{frame}[fragile] |
355 %% \begin{frame}[fragile] |
283 \frametitle{Computing Norms} |
356 %% \frametitle{Computing Norms} |
284 \begin{lstlisting} |
357 %% \begin{lstlisting} |
285 In []: norm(E) |
358 %% In []: norm(E) |
286 Out[]: 8.1240384046359608 |
359 %% Out[]: 8.1240384046359608 |
287 \end{lstlisting} |
360 %% \end{lstlisting} |
288 \end{frame} |
361 %% \end{frame} |
289 |
362 |
290 \begin{frame}[fragile] |
363 %% \begin{frame}[fragile] |
291 \frametitle{Singular Value Decomposition} |
364 %% \frametitle{Singular Value Decomposition} |
292 \begin{small} |
365 %% \begin{small} |
293 \begin{lstlisting} |
366 %% \begin{lstlisting} |
294 In []: svd(E) |
367 %% In []: svd(E) |
295 Out[]: |
368 %% Out[]: |
296 (array( |
369 %% (array( |
297 [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
370 %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
298 [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
371 %% [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
299 [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
372 %% [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
300 array([ 8., 1., 1.]), |
373 %% array([ 8., 1., 1.]), |
301 array([[-0.66666667, -0.33333333, -0.66666667], |
374 %% array([[-0.66666667, -0.33333333, -0.66666667], |
302 [-0. , 0.89442719, -0.4472136 ], |
375 %% [-0. , 0.89442719, -0.4472136 ], |
303 [-0.74535599, 0.2981424 , 0.59628479]])) |
376 %% [-0.74535599, 0.2981424 , 0.59628479]])) |
304 \end{lstlisting} |
377 %% \end{lstlisting} |
305 \end{small} |
378 %% \end{small} |
306 \inctime{15} |
379 %% \inctime{15} |
307 \end{frame} |
380 %% \end{frame} |
308 |
381 |
309 \section{Least Squares Fit} |
382 \section{Least Squares Fit} |
310 \begin{frame}[fragile] |
383 \begin{frame}[fragile] |
311 \frametitle{Least Squares Fit} |
384 \frametitle{$L$ vs. $T^2$} |
|
385 \vspace{-0.15in} |
|
386 \begin{figure} |
|
387 \includegraphics[width=4in]{data/L-Tsq-points.png} |
|
388 \end{figure} |
|
389 \end{frame} |
|
390 |
|
391 \begin{frame}[fragile] |
|
392 \frametitle{$L$ vs. $T^2$} |
312 \vspace{-0.15in} |
393 \vspace{-0.15in} |
313 \begin{figure} |
394 \begin{figure} |
314 \includegraphics[width=4in]{data/L-Tsq-Line.png} |
395 \includegraphics[width=4in]{data/L-Tsq-Line.png} |
315 \end{figure} |
|
316 \end{frame} |
|
317 |
|
318 \begin{frame}[fragile] |
|
319 \frametitle{Least Squares Fit} |
|
320 \vspace{-0.15in} |
|
321 \begin{figure} |
|
322 \includegraphics[width=4in]{data/L-Tsq-points.png} |
|
323 \end{figure} |
396 \end{figure} |
324 \end{frame} |
397 \end{frame} |
325 |
398 |
326 \begin{frame}[fragile] |
399 \begin{frame}[fragile] |
327 \frametitle{Least Squares Fit} |
400 \frametitle{Least Squares Fit} |
381 \begin{itemize} |
454 \begin{itemize} |
382 \item Now use the \typ{lstsq} function |
455 \item Now use the \typ{lstsq} function |
383 \item Along with a lot of things, it returns the least squares solution |
456 \item Along with a lot of things, it returns the least squares solution |
384 \end{itemize} |
457 \end{itemize} |
385 \begin{lstlisting} |
458 \begin{lstlisting} |
386 In []: coef, res, r, s = lstsq(A,TSq) |
459 In []: result = lstsq(A,TSq) |
|
460 In []: coef = result[0] |
387 \end{lstlisting} |
461 \end{lstlisting} |
388 \end{frame} |
462 \end{frame} |
389 |
463 |
390 \subsection{Plotting} |
464 \subsection{Plotting} |
391 \begin{frame}[fragile] |
465 \begin{frame}[fragile] |