diff options
author | Shantanu Choudhary | 2009-11-04 09:40:28 +0530 |
---|---|---|
committer | Shantanu Choudhary | 2009-11-04 09:40:28 +0530 |
commit | 005eb6e95f1a5ab98bd658fd5cc7957fd06e39e1 (patch) | |
tree | 7e09b2a5b84a76f62703809d81e137e16a0bbe60 /day1 | |
parent | fff08abf9ea34a713d7dfc6b38a0ac7dc6711fc3 (diff) | |
download | workshops-005eb6e95f1a5ab98bd658fd5cc7957fd06e39e1.tar.gz workshops-005eb6e95f1a5ab98bd658fd5cc7957fd06e39e1.tar.bz2 workshops-005eb6e95f1a5ab98bd658fd5cc7957fd06e39e1.zip |
Changes made for Goa workshop .
Diffstat (limited to 'day1')
-rw-r--r-- | day1/data/interpolate.png | bin | 0 -> 19608 bytes | |||
-rw-r--r-- | day1/session2.tex | 1 | ||||
-rw-r--r-- | day1/session3.tex | 1 | ||||
-rw-r--r-- | day1/session4.tex | 237 | ||||
-rw-r--r-- | day1/session5.tex | 86 | ||||
-rw-r--r-- | day1/session6.tex | 36 |
6 files changed, 238 insertions, 123 deletions
diff --git a/day1/data/interpolate.png b/day1/data/interpolate.png Binary files differnew file mode 100644 index 0000000..dd91788 --- /dev/null +++ b/day1/data/interpolate.png diff --git a/day1/session2.tex b/day1/session2.tex index 150a1a3..493942b 100644 --- a/day1/session2.tex +++ b/day1/session2.tex @@ -260,6 +260,7 @@ Out[]: [2, 3] \end{lstlisting} \end{frame} +%% more on list slicing \begin{frame}[fragile] \frametitle{List operations} \begin{lstlisting} diff --git a/day1/session3.tex b/day1/session3.tex index 7019a7c..bda7d8d 100644 --- a/day1/session3.tex +++ b/day1/session3.tex @@ -581,6 +581,7 @@ In []: TSq = T*T \end{itemize} \end{frame} +%%making vander without vander, simple matrix \subsection{Van der Monde matrix generation} \begin{frame}[fragile] \frametitle{Van der Monde Matrix} diff --git a/day1/session4.tex b/day1/session4.tex index 4c0e913..7981771 100644 --- a/day1/session4.tex +++ b/day1/session4.tex @@ -128,45 +128,68 @@ \begin{frame} \frametitle{Matrices: Introduction} -We looked at the Van der Monde matrix in the previous session,\\ -let us now look at matrices in a little more detail. +Let us now look at matrices in detail.\\ +\alert{All matrix operations are done using \kwrd{arrays}} \end{frame} \subsection{Initializing} \begin{frame}[fragile] \frametitle{Matrices: Initializing} \begin{lstlisting} -In []: A = matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +In []: A = array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) In []: A Out[]: -matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) \end{lstlisting} \end{frame} +\begin{frame}[fragile] + \frametitle{Accessing elements of matrices} +\begin{small} + \begin{lstlisting} +In []: C = array([[1,1,2], + [2,4,1], + [-1,3,7]]) +In []: C[1,2] +Out[]: 1 + +In []: C[1] +Out[]: array([2, 4, 1]) + +In []: C[1,1] = -2 +In []: C +Out[]: +array([[ 1, 1, 2], + [ 2, -2, 1], + [-1, 3, 7]]) + \end{lstlisting} +\end{small} +\end{frame} + \subsection{Basic Operations} \begin{frame}[fragile] \frametitle{Transpose of a Matrix} \begin{lstlisting} -In []: linalg.transpose(A) +In []: A.T Out[]: -matrix([[ 1, 2, 2, 1], - [ 1, 5, 1, -3], - [ 2, -1, -1, 2], - [-1, -9, 3, 7]]) +array([[ 1, 2, 2, 1], + [ 1, 5, 1, -3], + [ 2, -1, -1, 2], + [-1, -9, 3, 7]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Sum of all elements} \begin{lstlisting} -In []: linalg.sum(A) +In []: sum(A) Out[]: 12 \end{lstlisting} \end{frame} @@ -174,41 +197,56 @@ Out[]: 12 \begin{frame}[fragile] \frametitle{Matrix Addition} \begin{lstlisting} -In []: B = matrix([[3,2,-1,5], - [2,-2,4,9], - [-1,0.5,-1,-7], - [9,-5,7,3]]) -In []: linalg.add(A,B) +In []: B = array([[3,2,-1,5], + [2,-2,4,9], + [-1,0.5,-1,-7], + [9,-5,7,3]]) +In []: A + B Out[]: -matrix([[ 4. , 3. , 1. , 4. ], - [ 4. , 3. , 3. , 0. ], - [ 1. , 1.5, -2. , -4. ], - [ 10. , -8. , 9. , 10. ]]) +array([[ 4. , 3. , 1. , 4. ], + [ 4. , 3. , 3. , 0. ], + [ 1. , 1.5, -2. , -4. ], + [ 10. , -8. , 9. , 10. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] +\frametitle{Elementwise Multiplication} +\begin{lstlisting} +In []: A*B +Out[]: +array([[ 3. , 2. , -2. , -5. ], + [ 4. , -10. , -4. , -81. ], + [ -2. , 0.5, 1. , -21. ], + [ 9. , 15. , 14. , 21. ]]) + +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] \frametitle{Matrix Multiplication} \begin{lstlisting} -In []: linalg.multiply(A, B) +In []: dot(A,B) Out[]: -matrix([[ 3. , 2. , -2. , -5. ], - [ 4. , -10. , -4. , -81. ], - [ -2. , 0.5, 1. , -21. ], - [ 9. , 15. , 14. , 21. ]]) +array([[ -6. , 6. , -6. , -3. ], + [-64. , 38.5, -44. , 35. ], + [ 36. , -13.5, 24. , 35. ], + [ 58. , -26. , 34. , -15. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Inverse of a Matrix} +\begin{lstlisting} +In []: inv(A) +\end{lstlisting} \begin{small} \begin{lstlisting} -In []: linalg.inv(A) Out[]: -matrix([[-0.5 , 0.55, -0.15, 0.7 ], - [ 0.75, -0.5 , 0.5 , -0.75], - [ 0.5 , -0.15, -0.05, -0.1 ], - [ 0.25, -0.25, 0.25, -0.25]]) +array([[-0.5 , 0.55, -0.15, 0.7 ], + [ 0.75, -0.5 , 0.5 , -0.75], + [ 0.5 , -0.15, -0.05, -0.1 ], + [ 0.25, -0.25, 0.25, -0.25]]) \end{lstlisting} \end{small} \end{frame} @@ -217,24 +255,25 @@ matrix([[-0.5 , 0.55, -0.15, 0.7 ], \frametitle{Determinant} \begin{lstlisting} In []: det(A) -Out[66]: 80.0 +Out[]: 80.0 \end{lstlisting} \end{frame} +%%use S=array(X,Y) \begin{frame}[fragile] -\frametitle{Eigen Values and Eigen Matrix} +\frametitle{Eigenvalues and Eigen Vectors} \begin{small} \begin{lstlisting} -In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]]) +In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) -In []: linalg.eig(E) +In []: eig(E) Out[]: (array([-1., 8., -1.]), - matrix([[-0.74535599, 0.66666667, -0.1931126 ], + array([[-0.74535599, 0.66666667, -0.1931126 ], [ 0.2981424 , 0.33333333, -0.78664085], [ 0.59628479, 0.66666667, 0.58643303]])) -In []: linalg.eigvals(E) +In []: eigvals(E) Out[]: array([-1., 8., -1.]) \end{lstlisting} \end{small} @@ -243,23 +282,23 @@ Out[]: array([-1., 8., -1.]) \begin{frame}[fragile] \frametitle{Computing Norms} \begin{lstlisting} -In []: linalg.norm(E) +In []: norm(E) Out[]: 8.1240384046359608 \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Single Value Decomposition} + \frametitle{Singular Value Decomposition} \begin{small} \begin{lstlisting} -In [76]: linalg.svd(E) -Out[76]: -(matrix( +In []: svd(E) +Out[]: +(array( [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), array([ 8., 1., 1.]), - matrix([[-0.66666667, -0.33333333, -0.66666667], + array([[-0.66666667, -0.33333333, -0.66666667], [-0. , 0.89442719, -0.4472136 ], [-0.74535599, 0.2981424 , 0.59628479]])) \end{lstlisting} @@ -289,12 +328,12 @@ Solution: \frametitle{Solving using Matrices} Let us now look at how to solve this using \kwrd{matrices} \begin{lstlisting} - In []: A = matrix([[3,2,-1], - [2,-2,4], - [-1, 0.5, -1]]) - In []: b = matrix([[1], [-2], [0]]) - In []: x = linalg.solve(A, b) - In []: Ax = dot(A, x) + In []: A = array([[3,2,-1], + [2,-2,4], + [-1, 0.5, -1]]) + In []: b = array([[1], [-2], [0]]) + In []: x = solve(A, b) + In []: Ax = dot(A,x) \end{lstlisting} \end{frame} @@ -314,9 +353,9 @@ array([[ 1.], \begin{lstlisting} In []: Ax Out[]: -matrix([[ 1.00000000e+00], - [ -2.00000000e+00], - [ 2.22044605e-16]]) +array([[ 1.00000000e+00], + [ -2.00000000e+00], + [ 2.22044605e-16]]) \end{lstlisting} \begin{block}{} The last term in the matrix is actually \alert{0}!\\ @@ -332,7 +371,74 @@ Out[]: True \subsection{Exercises} \begin{frame}[fragile] -\frametitle{Problem Set 4: Problem 4.1} +\frametitle{Problem 1} +Given the matrix:\\ +\begin{center} +$\begin{bmatrix} +-2 & 2 & 3\\ + 2 & 1 & 6\\ +-1 &-2 & 0\\ +\end{bmatrix}$ +\end{center} +Find: +\begin{itemize} + \item[i] Transpose + \item[ii]Inverse + \item[iii]Determinant + \item[iv] Eigenvalues and Eigen vectors + \item[v] Singular Value decomposition +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 2} +Given +\begin{center} +A = +$\begin{bmatrix} +-3 & 1 & 5 \\ +1 & 0 & -2 \\ +5 & -2 & 4 \\ +\end{bmatrix}$ +, B = +$\begin{bmatrix} +0 & 9 & -12 \\ +-9 & 0 & 20 \\ +12 & -20 & 0 \\ +\end{bmatrix}$ +\end{center} +Find: +\begin{itemize} + \item[i] Sum of A and B + \item[ii]Elementwise Product of A and B + \item[iii] Matrix product of A and B +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +Sum: +$\begin{bmatrix} +-3 & 10 & 7 \\ +-8 & 0 & 18 \\ +17 & -22 & 4 \\ +\end{bmatrix}$ +,\\ Elementwise Product: +$\begin{bmatrix} +0 & 9 & -60 \\ +-9 & 0 & -40 \\ +60 & 40 & 0 \\ +\end{bmatrix}$ +,\\ Matrix product: +$\begin{bmatrix} +51 & -127 & 56 \\ +-24 & 49 & -12 \\ +66 & -35 & -100 \\ +\end{bmatrix}$ +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 3} Solve the set of equations: \begin{align*} x + y + 2z -w & = 3\\ @@ -345,37 +451,30 @@ Solve the set of equations: \begin{frame}[fragile] \frametitle{Solution} -Solution: -\begin{lstlisting} +Use \kwrd{solve()} \begin{align*} x & = -5\\ y & = 2\\ z & = 3\\ w & = 0\\ \end{align*} -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Problem 4.2} - \end{frame} \section{Summary} \begin{frame} - \frametitle{Summary} -So what did we learn?? + \frametitle{What did we learn??} \begin{itemize} \item Matrices \begin{itemize} + \item Accessing elements \item Transpose \item Addition \item Multiplication \item Inverse of a matrix \item Determinant - \item Eigen values and Eigen matrix + \item Eigenvalues and Eigen vector \item Norms - \item Single Value Decomposition + \item Singular Value Decomposition \end{itemize} \item Solving linear equations \end{itemize} diff --git a/day1/session5.tex b/day1/session5.tex index 24b1282..af0848c 100644 --- a/day1/session5.tex +++ b/day1/session5.tex @@ -124,18 +124,36 @@ %% % \pausesections %% \end{frame} - \section{\typ{loadtxt}} \begin{frame}[fragile] + \frametitle{Array slicing} + \begin{lstlisting} +In []: A = array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) + +In []: A[:,0] +Out[]: array([ 1, 2, 2, 1]) + +In []: A[1:3,1:3] +Out[]: +array([[ 5, -1], + [ 1, -1]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] \frametitle{\typ{loadtxt}} \begin{itemize} \item Load data from a text file. \item Each row must have same number of values. \end{itemize} \begin{lstlisting} -In []: L, T = loadtxt('pendulum.txt', - unpack = True) +In []: data = loadtxt('pendulum.txt') +In []: x = data[:, 0] +In []: y = data[:, 1] \end{lstlisting} \end{frame} @@ -151,6 +169,7 @@ In []: L, T = loadtxt('pendulum.txt', \item It contains x,y position of particle. \item Plot the given points. %% \item Interpolate the missing region. +%% load txt the unpack part. \end{itemize} \begin{lstlisting} In []: x, y = loadtxt('points.txt', @@ -195,6 +214,8 @@ In []: plot(x, y, '.') %% \end{lstlisting} %% \end{frame} +%%better example, maybe parabola + \begin{frame}[fragile] \frametitle{Spline Interpolation} \begin{small} @@ -223,22 +244,29 @@ In []: tck = splrep(x, y) \begin{frame}[fragile] \frametitle{\typ{splev}} -To Evaluate a B-spline and it's derivatives +To Evaluate a spline and it's derivatives \begin{lstlisting} -In []: Xnew = arange(0.01,3,0.02) +In []: Xnew = linspace(0.01,3,100) In []: Ynew = splev(Xnew, tck) In []: y.shape Out[]: (40,) In []: Ynew.shape -Out[]: (150,) +Out[]: (100,) -In []: plot(Xnew, Ynew) +In []: plot(Xnew, Ynew, '+') \end{lstlisting} \end{frame} +\begin{frame} + \frametitle{Interpolated data} + \begin{center} + \includegraphics[height=2in, interpolate=true]{data/interpolate} + \end{center} +\end{frame} + %% \begin{frame}[fragile] %% \frametitle{Interpolation \ldots} %% \begin{itemize} @@ -257,8 +285,8 @@ In []: plot(Xnew, Ynew) \end{itemize} \begin{center} \begin{tabular}{l l} -$f(x+h)=f(x)+h.f^{'}(x)$ &Forward \\ -$f(x-h)=f(x)-h.f^{'}(x)$ &Backward +$f(x+h)=f(x)+hf^{'}(x)$ &Forward \\ +$f(x-h)=f(x)-hf^{'}(x)$ &Backward \end{tabular} \end{center} \end{frame} @@ -277,7 +305,10 @@ Obtain the finite forward difference of y \frametitle{Forward Difference \ldots} \begin{lstlisting} In []: fD = (y[1:] - y[:-1]) / deltax -In []: plot(x, y, x[:-1], fD) +In []: print len(fD) +Out[]: 99 +In []: plot(x, y) +In []: plot(x[:-1], fD) \end{lstlisting} \begin{center} \includegraphics[height=2in, interpolate=true]{data/fwdDiff} @@ -310,27 +341,18 @@ $X$ & $Y$ \\ \hline \frametitle{Example \ldots} \begin{itemize} \item Read the file -\item Obtain an array of x, y +\item Obtain an array of X, Y(S) \item Obtain velocity and acceleration \item use \typ{deltaT = 0.05} \end{itemize} \begin{lstlisting} -In []: X = [] -In []: Y = [] -In []: for line in open('location.txt'): - .... points = line.split() - .... X.append(float(points[0])) - .... Y.append(float(points[1])) -In []: S = array([X, Y]) +In []: S = loadtxt('location.txt') \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Example \ldots} -\begin{itemize} -\item use \typ{deltaT = 0.05} -\end{itemize} \begin{lstlisting} In []: deltaT = 0.05 @@ -345,18 +367,17 @@ Try Plotting the position, velocity \& acceleration. \begin{frame}[fragile] \frametitle{Quadrature} -\begin{itemize} -\item We wish to find area under a curve -\item Area under $(sin(x) + x^2)$ in $(0,1)$ -\item scipy has functions to do that -\end{itemize} -\begin{small} - \typ{In []: from scipy.integrate import quad} -\end{small} + +\emphbar{$\int_0^1(sin(x) + x^2)$} + +\typ{In []: from scipy.integrate import quad} + \begin{itemize} \item Inputs - function to integrate, limits \end{itemize} \begin{lstlisting} +%% In []: quad(sin(x)+x**2, 0, 1) +%% NameError: name 'x' is not defined In []: x = 0 In []: quad(sin(x)+x**2, 0, 1) \end{lstlisting} @@ -406,8 +427,7 @@ In []: quad(f, 0, 1) \end{lstlisting} Returns the integral and an estimate of the absolute error in the result. \begin{itemize} -\item Look at \typ{dblquad} for Double integrals -\item Use \typ{tplquad} for Triple integrals +\item \typ{dblquad}, \typ{tplquad} are available \end{itemize} \end{frame} @@ -419,9 +439,7 @@ Returns the integral and an estimate of the absolute error in the result. \item Functions \begin{itemize} \item Definition - \item Calling - \item Default Arguments - \item Keyword Arguments + \item Calling \end{itemize} \item Quadrature \end{itemize} diff --git a/day1/session6.tex b/day1/session6.tex index 0b5f681..2858a5e 100644 --- a/day1/session6.tex +++ b/day1/session6.tex @@ -123,6 +123,7 @@ %% % You might wish to add the option [pausesections] %% \end{frame} +%%more exercise on plotting \section{ODEs} \begin{frame}[fragile] @@ -138,7 +139,7 @@ We shall use the simple ODE of a simple pendulum. \dot{\theta} &= \omega \\ \dot{\omega} &= -\frac{g}{L}sin(\theta) \\ \text{At}\ t &= 0 : \nonumber \\ - \theta = \theta_0\quad & \&\quad \omega = 0 \nonumber + \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber \end{align} \end{frame} @@ -149,9 +150,9 @@ We shall use the simple ODE of a simple pendulum. \item Define a function as below \end{itemize} \begin{lstlisting} -In []: def pend_int(unknown, t, p): - .... theta, omega = unknown - .... g, L = p +In []: def pend_int(initial, t): + .... theta, omega = initial + .... g, L = 9.81, 0.2 .... f=[omega, -(g/L)*sin(theta)] .... return f .... @@ -162,25 +163,22 @@ In []: def pend_int(unknown, t, p): \frametitle{Solving ODEs using SciPy \ldots} \begin{itemize} \item \typ{t} is the time variable \\ -\item \typ{p} has the constants \\ \item \typ{initial} has the initial values \end{itemize} \begin{lstlisting} In []: t = linspace(0, 10, 101) -In []: p=(-9.81, 0.2) In []: initial = [10*2*pi/360, 0] -\end{lstlisting} +\end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Solving ODEs using SciPy \ldots} -\begin{small} - \typ{In []: from scipy.integrate import odeint} -\end{small} +%%\begin{small} +\typ{In []: from scipy.integrate import odeint} +%%\end{small} \begin{lstlisting} In []: pend_sol = odeint(pend_int, - initial,t, - args=(p,)) + initial,t) \end{lstlisting} \end{frame} @@ -308,14 +306,13 @@ In []: pend_sol = odeint(pend_int, \begin{frame}[fragile] \frametitle{Initial Estimates \ldots} \begin{lstlisting} - In []: def our_f(x): - ....: return cos(x)-x**2 - ....: - In []: x = linspace(-pi/2, pi/2, 11) +In []: def our_f(x): + ....: return cos(x) - x*x + ....: +In []: x = linspace(-pi/2, pi/2, 11) +In []: y = our_f(x) \end{lstlisting} -\begin{itemize} -\item Get the intervals of x, where sign changes occur -\end{itemize} +Get the intervals of x, where sign changes occur \end{frame} \begin{frame}[fragile] @@ -335,7 +332,6 @@ Now use Newton-Raphson? \frametitle{Scipy Methods - \typ{roots}} \begin{itemize} \item Calculates the roots of polynomials -\item Array of coefficients is the only parameter \end{itemize} \begin{lstlisting} In []: coeffs = [1, 6, 13] |