77 \title[Matrices \& Curve Fitting]{Python for Science and Engg: Matrices \& Least Square Fit} |
77 \title[Matrices \& Curve Fitting]{Python for Science and Engg: Matrices \& Least Square Fit} |
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[] {7 November, 2009\\Day 1, Session 4} |
82 \date[] {14 December, 2009\\Day 1, Session 4} |
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
84 |
84 |
85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
86 %\logo{\pgfuseimage{iitmlogo}} |
86 %\logo{\pgfuseimage{iitmlogo}} |
87 |
87 |
132 \end{frame} |
132 \end{frame} |
133 |
133 |
134 \begin{frame}[fragile] |
134 \begin{frame}[fragile] |
135 \frametitle{Matrices: Initializing} |
135 \frametitle{Matrices: Initializing} |
136 \begin{lstlisting} |
136 \begin{lstlisting} |
137 In []: A = array([[ 1, 1, 2, -1], |
137 In []: c = array([[1,1,2], |
138 [ 2, 5, -1, -9], |
138 [2,4,1], |
139 [ 2, 1, -1, 3], |
139 [-1,3,7]]) |
140 [ 1, -3, 2, 7]]) |
140 |
141 In []: A |
141 In []: c |
142 Out[]: |
142 Out[]: |
143 array([[ 1, 1, 2, -1], |
143 array([[1,1,2], |
144 [ 2, 5, -1, -9], |
144 [2,4,1], |
145 [ 2, 1, -1, 3], |
145 [-1,3,7]]) |
146 [ 1, -3, 2, 7]]) |
|
147 \end{lstlisting} |
146 \end{lstlisting} |
148 \end{frame} |
147 \end{frame} |
149 |
148 |
150 \begin{frame}[fragile] |
149 \begin{frame}[fragile] |
151 \frametitle{Initializing some special matrices} |
150 \frametitle{Initializing some special matrices} |
155 Out[]: |
154 Out[]: |
156 array([[ 1., 1., 1., 1., 1.], |
155 array([[ 1., 1., 1., 1., 1.], |
157 [ 1., 1., 1., 1., 1.], |
156 [ 1., 1., 1., 1., 1.], |
158 [ 1., 1., 1., 1., 1.]]) |
157 [ 1., 1., 1., 1., 1.]]) |
159 |
158 |
160 In []: ones_like([1, 2, 3, 4, 5]) |
159 In []: ones_like([1, 2, 3, 4]) |
161 Out[]: array([1, 1, 1, 1, 1]) |
160 Out[]: array([1, 1, 1, 1]) |
162 |
161 |
163 In []: identity(2) |
162 In []: identity(2) |
164 Out[]: |
163 Out[]: |
165 array([[ 1., 0.], |
164 array([[ 1., 0.], |
166 [ 0., 1.]]) |
165 [ 0., 1.]]) |
171 |
170 |
172 |
171 |
173 \begin{frame}[fragile] |
172 \begin{frame}[fragile] |
174 \frametitle{Accessing elements} |
173 \frametitle{Accessing elements} |
175 \begin{lstlisting} |
174 \begin{lstlisting} |
176 In []: C = array([[1,1,2], |
175 In []: c |
177 [2,4,1], |
176 Out[]: |
178 [-1,3,7]]) |
177 array([[1,1,2], |
179 |
178 [2,4,1], |
180 In []: C[1][2] |
179 [-1,3,7]]) |
|
180 |
|
181 In []: c[1][2] |
181 Out[]: 1 |
182 Out[]: 1 |
182 |
183 In []: c[1,2] |
183 In []: C[1,2] |
|
184 Out[]: 1 |
184 Out[]: 1 |
185 |
185 In []: c[1] |
186 In []: C[1] |
|
187 Out[]: array([2, 4, 1]) |
186 Out[]: array([2, 4, 1]) |
188 \end{lstlisting} |
187 \end{lstlisting} |
189 \end{frame} |
188 \end{frame} |
190 |
189 |
191 \begin{frame}[fragile] |
190 \begin{frame}[fragile] |
192 \frametitle{Changing elements} |
191 \frametitle{Changing elements} |
193 \begin{small} |
192 \begin{small} |
194 \begin{lstlisting} |
193 \begin{lstlisting} |
195 In []: C[1,1] = -2 |
194 In []: c[1,1] = -2 |
196 In []: C |
195 In []: c |
197 Out[]: |
196 Out[]: |
198 array([[ 1, 1, 2], |
197 array([[ 1, 1, 2], |
199 [ 2, -2, 1], |
198 [ 2, -2, 1], |
200 [-1, 3, 7]]) |
199 [-1, 3, 7]]) |
201 |
200 |
202 In []: C[1] = [0,0,0] |
201 In []: c[1] = [0,0,0] |
203 In []: C |
202 In []: c |
204 Out[]: |
203 Out[]: |
205 array([[ 1, 1, 2], |
204 array([[ 1, 1, 2], |
206 [ 0, 0, 0], |
205 [ 0, 0, 0], |
207 [-1, 3, 7]]) |
206 [-1, 3, 7]]) |
208 \end{lstlisting} |
207 \end{lstlisting} |
301 |
298 |
302 \begin{frame}[fragile] |
299 \begin{frame}[fragile] |
303 \frametitle{Solutions} |
300 \frametitle{Solutions} |
304 \begin{small} |
301 \begin{small} |
305 \begin{lstlisting} |
302 \begin{lstlisting} |
306 In []: imshow(A[:256,:256]) |
303 In []: imshow(a[:256,:256]) |
307 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
304 Out[]: <matplotlib.image.AxesImage object at 0xb6f658c> |
308 |
305 |
309 In []: imshow(A[200:400,200:400]) |
306 In []: imshow(a[200:400,200:400]) |
310 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
307 Out[]: <matplotlib.image.AxesImage object at 0xb757c2c> |
311 |
308 |
312 In []: imshow(A[::2,::2]) |
309 In []: imshow(a[::2,::2]) |
313 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
310 Out[]: <matplotlib.image.AxesImage object at 0xb765c8c> |
314 \end{lstlisting} |
311 \end{lstlisting} |
315 \end{small} |
312 \end{small} |
316 \end{frame} |
313 \end{frame} |
317 |
314 |
318 \begin{frame}[fragile] |
315 \begin{frame}[fragile] |
319 \frametitle{Transpose of a Matrix} |
316 \frametitle{Transpose of a Matrix} |
320 \begin{lstlisting} |
317 \begin{lstlisting} |
321 In []: A.T |
318 In []: a = array([[ 1, 1, 2, -1], |
|
319 ...: [ 2, 5, -1, -9], |
|
320 ...: [ 2, 1, -1, 3], |
|
321 ...: [ 1, -3, 2, 7]]) |
|
322 |
|
323 In []: a.T |
322 Out[]: |
324 Out[]: |
323 array([[ 1, 2, 2, 1], |
325 array([[ 1, 2, 2, 1], |
324 [ 1, 5, 1, -3], |
326 [ 1, 5, 1, -3], |
325 [ 2, -1, -1, 2], |
327 [ 2, -1, -1, 2], |
326 [-1, -9, 3, 7]]) |
328 [-1, -9, 3, 7]]) |
328 \end{frame} |
330 \end{frame} |
329 |
331 |
330 \begin{frame}[fragile] |
332 \begin{frame}[fragile] |
331 \frametitle{Sum of all elements} |
333 \frametitle{Sum of all elements} |
332 \begin{lstlisting} |
334 \begin{lstlisting} |
333 In []: sum(A) |
335 In []: sum(a) |
334 Out[]: 12 |
336 Out[]: 12 |
335 \end{lstlisting} |
337 \end{lstlisting} |
336 \end{frame} |
338 \end{frame} |
337 |
339 |
338 \begin{frame}[fragile] |
340 \begin{frame}[fragile] |
339 \frametitle{Matrix Addition} |
341 \frametitle{Matrix Addition} |
340 \begin{lstlisting} |
342 \begin{lstlisting} |
341 In []: B = array([[3,2,-1,5], |
343 In []: b = array([[3,2,-1,5], |
342 [2,-2,4,9], |
344 [2,-2,4,9], |
343 [-1,0.5,-1,-7], |
345 [-1,0.5,-1,-7], |
344 [9,-5,7,3]]) |
346 [9,-5,7,3]]) |
345 In []: A + B |
347 In []: a + b |
346 Out[]: |
348 Out[]: |
347 array([[ 4. , 3. , 1. , 4. ], |
349 array([[ 4. , 3. , 1. , 4. ], |
348 [ 4. , 3. , 3. , 0. ], |
350 [ 4. , 3. , 3. , 0. ], |
349 [ 1. , 1.5, -2. , -4. ], |
351 [ 1. , 1.5, -2. , -4. ], |
350 [ 10. , -8. , 9. , 10. ]]) |
352 [ 10. , -8. , 9. , 10. ]]) |
352 \end{frame} |
354 \end{frame} |
353 |
355 |
354 \begin{frame}[fragile] |
356 \begin{frame}[fragile] |
355 \frametitle{Elementwise Multiplication} |
357 \frametitle{Elementwise Multiplication} |
356 \begin{lstlisting} |
358 \begin{lstlisting} |
357 In []: A*B |
359 In []: a*b |
358 Out[]: |
360 Out[]: |
359 array([[ 3. , 2. , -2. , -5. ], |
361 array([[ 3. , 2. , -2. , -5. ], |
360 [ 4. , -10. , -4. , -81. ], |
362 [ 4. , -10. , -4. , -81. ], |
361 [ -2. , 0.5, 1. , -21. ], |
363 [ -2. , 0.5, 1. , -21. ], |
362 [ 9. , 15. , 14. , 21. ]]) |
364 [ 9. , 15. , 14. , 21. ]]) |
365 \end{frame} |
367 \end{frame} |
366 |
368 |
367 \begin{frame}[fragile] |
369 \begin{frame}[fragile] |
368 \frametitle{Matrix Multiplication} |
370 \frametitle{Matrix Multiplication} |
369 \begin{lstlisting} |
371 \begin{lstlisting} |
370 In []: dot(A,B) |
372 In []: dot(a, b) |
371 Out[]: |
373 Out[]: |
372 array([[ -6. , 6. , -6. , -3. ], |
374 array([[ -6. , 6. , -6. , -3. ], |
373 [-64. , 38.5, -44. , 35. ], |
375 [-64. , 38.5, -44. , 35. ], |
374 [ 36. , -13.5, 24. , 35. ], |
376 [ 36. , -13.5, 24. , 35. ], |
375 [ 58. , -26. , 34. , -15. ]]) |
377 [ 58. , -26. , 34. , -15. ]]) |
393 \end{frame} |
395 \end{frame} |
394 |
396 |
395 \begin{frame}[fragile] |
397 \begin{frame}[fragile] |
396 \frametitle{Determinant} |
398 \frametitle{Determinant} |
397 \begin{lstlisting} |
399 \begin{lstlisting} |
398 In []: det(A) |
400 In []: det(a) |
399 Out[]: 80.0 |
401 Out[]: 80.0 |
400 \end{lstlisting} |
402 \end{lstlisting} |
401 \end{frame} |
403 \end{frame} |
402 |
404 |
403 %%use S=array(X,Y) |
405 %%use S=array(X,Y) |
404 \begin{frame}[fragile] |
406 \begin{frame}[fragile] |
405 \frametitle{Eigenvalues and Eigen Vectors} |
407 \frametitle{Eigenvalues and Eigen Vectors} |
406 \begin{small} |
408 \begin{small} |
407 \begin{lstlisting} |
409 \begin{lstlisting} |
408 In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) |
410 In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) |
409 |
411 |
410 In []: eig(E) |
412 In []: eig(e) |
411 Out[]: |
413 Out[]: |
412 (array([-1., 8., -1.]), |
414 (array([-1., 8., -1.]), |
413 array([[-0.74535599, 0.66666667, -0.1931126 ], |
415 array([[-0.74535599, 0.66666667, -0.1931126 ], |
414 [ 0.2981424 , 0.33333333, -0.78664085], |
416 [ 0.2981424 , 0.33333333, -0.78664085], |
415 [ 0.59628479, 0.66666667, 0.58643303]])) |
417 [ 0.59628479, 0.66666667, 0.58643303]])) |
416 |
418 |
417 In []: eigvals(E) |
419 In []: eigvals(e) |
418 Out[]: array([-1., 8., -1.]) |
420 Out[]: array([-1., 8., -1.]) |
419 \end{lstlisting} |
421 \end{lstlisting} |
420 \end{small} |
422 \end{small} |
421 \end{frame} |
423 \end{frame} |
422 |
424 |
423 %% \begin{frame}[fragile] |
425 %% \begin{frame}[fragile] |
424 %% \frametitle{Computing Norms} |
426 %% \frametitle{Computing Norms} |
425 %% \begin{lstlisting} |
427 %% \begin{lstlisting} |
426 %% In []: norm(E) |
428 %% In []: norm(e) |
427 %% Out[]: 8.1240384046359608 |
429 %% Out[]: 8.1240384046359608 |
428 %% \end{lstlisting} |
430 %% \end{lstlisting} |
429 %% \end{frame} |
431 %% \end{frame} |
430 |
432 |
431 %% \begin{frame}[fragile] |
433 %% \begin{frame}[fragile] |
432 %% \frametitle{Singular Value Decomposition} |
434 %% \frametitle{Singular Value Decomposition} |
433 %% \begin{small} |
435 %% \begin{small} |
434 %% \begin{lstlisting} |
436 %% \begin{lstlisting} |
435 %% In []: svd(E) |
437 %% In []: svd(e) |
436 %% Out[]: |
438 %% Out[]: |
437 %% (array( |
439 %% (array( |
438 %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
440 %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], |
439 %% [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
441 %% [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], |
440 %% [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
442 %% [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), |
501 \item We need to find $p$ to plot the line |
503 \item We need to find $p$ to plot the line |
502 \end{itemize} |
504 \end{itemize} |
503 \end{frame} |
505 \end{frame} |
504 |
506 |
505 \begin{frame}[fragile] |
507 \begin{frame}[fragile] |
|
508 \frametitle{Getting $L$ and $T^2$} |
|
509 If you \alert{closed} IPython after session 2 |
|
510 \begin{lstlisting} |
|
511 In []: l = [] |
|
512 In []: t = [] |
|
513 In []: for line in open('pendulum.txt'): |
|
514 .... point = line.split() |
|
515 .... l.append(float(point[0])) |
|
516 .... t.append(float(point[1])) |
|
517 .... |
|
518 .... |
|
519 \end{lstlisting} |
|
520 \end{frame} |
|
521 |
|
522 \begin{frame}[fragile] |
|
523 \frametitle{Getting $L$ and $T^2$ \dots} |
|
524 \begin{lstlisting} |
|
525 In []: l = array(l) |
|
526 In []: t = array(t) |
|
527 \end{lstlisting} |
|
528 \alert{\typ{In []: tsq = t*t}} |
|
529 \end{frame} |
|
530 |
|
531 \begin{frame}[fragile] |
506 \frametitle{Generating $A$} |
532 \frametitle{Generating $A$} |
507 \begin{lstlisting} |
533 \begin{lstlisting} |
508 In []: A = array([L, ones_like(L)]) |
534 In []: A = array([l, ones_like(l)]) |
509 In []: A = A.T |
535 In []: A = A.T |
510 \end{lstlisting} |
536 \end{lstlisting} |
511 %% \begin{itemize} |
537 %% \begin{itemize} |
512 %% \item A is also called a Van der Monde matrix |
538 %% \item A is also called a Van der Monde matrix |
513 %% \item It can also be generated using \typ{vander} |
539 %% \item It can also be generated using \typ{vander} |
522 \begin{itemize} |
548 \begin{itemize} |
523 \item Now use the \typ{lstsq} function |
549 \item Now use the \typ{lstsq} function |
524 \item Along with a lot of things, it returns the least squares solution |
550 \item Along with a lot of things, it returns the least squares solution |
525 \end{itemize} |
551 \end{itemize} |
526 \begin{lstlisting} |
552 \begin{lstlisting} |
527 In []: result = lstsq(A,TSq) |
553 In []: result = lstsq(A,tsq) |
528 In []: coef = result[0] |
554 In []: coef = result[0] |
529 \end{lstlisting} |
555 \end{lstlisting} |
530 \end{frame} |
556 \end{frame} |
531 |
557 |
532 \begin{frame}[fragile] |
558 \begin{frame}[fragile] |
533 \frametitle{Least Square Fit Line \ldots} |
559 \frametitle{Least Square Fit Line \ldots} |
534 We get the points of the line from \typ{coef} |
560 We get the points of the line from \typ{coef} |
535 \begin{lstlisting} |
561 \begin{lstlisting} |
536 In []: Tline = coef[0]*L + coef[1] |
562 In []: Tline = coef[0]*l + coef[1] |
537 \end{lstlisting} |
563 \end{lstlisting} |
538 \begin{itemize} |
564 \begin{itemize} |
539 \item Now plot Tline vs. L, to get the Least squares fit line. |
565 \item Now plot \typ{Tline} vs. \typ{l}, to get the Least squares fit line. |
540 \end{itemize} |
566 \end{itemize} |
541 \begin{lstlisting} |
567 \begin{lstlisting} |
542 In []: plot(L, Tline) |
568 In []: plot(l, Tline) |
543 \end{lstlisting} |
569 \end{lstlisting} |
544 \end{frame} |
570 \end{frame} |
545 |
571 |
546 \begin{frame}[fragile] |
572 \begin{frame}[fragile] |
547 \frametitle{Least Squares Fit} |
573 \frametitle{Least Squares Fit} |