diff options
author | vattam@chiquitita | 2009-11-05 13:51:00 +0530 |
---|---|---|
committer | vattam@chiquitita | 2009-11-05 13:51:00 +0530 |
commit | 3df535b0cce08da4fe8b3be49c86c65b8173b48f (patch) | |
tree | 6e8fabd0c984e6c1791081972cc45c7c547dd5bd /day1 | |
parent | 77c59d319fa3012914c0c48a42377fa22ea69b4b (diff) | |
parent | 441ce6cd2742c5fba9aed4166a0b0e80804ea6ee (diff) | |
download | workshops-more-scipy-3df535b0cce08da4fe8b3be49c86c65b8173b48f.tar.gz workshops-more-scipy-3df535b0cce08da4fe8b3be49c86c65b8173b48f.tar.bz2 workshops-more-scipy-3df535b0cce08da4fe8b3be49c86c65b8173b48f.zip |
Merged branches.
Diffstat (limited to 'day1')
-rw-r--r-- | day1/cheatsheet1.tex | 63 | ||||
-rw-r--r-- | day1/data/annotate.png | bin | 30194 -> 29482 bytes | |||
-rwxr-xr-x | day1/data/interpolate.png | bin | 0 -> 19608 bytes | |||
-rwxr-xr-x | day1/data/missing_points.png | bin | 0 -> 15363 bytes | |||
-rwxr-xr-x | day1/data/pos_vel_accel.png | bin | 0 -> 31691 bytes | |||
-rw-r--r-- | day1/links.tex | 5 | ||||
-rw-r--r-- | day1/session1.tex | 124 | ||||
-rw-r--r-- | day1/session2.tex | 1 | ||||
-rw-r--r-- | day1/session3.tex | 111 | ||||
-rw-r--r-- | day1/session5.tex | 303 | ||||
-rwxr-xr-x[-rw-r--r--] | day1/session6.tex | 366 |
11 files changed, 286 insertions, 687 deletions
diff --git a/day1/cheatsheet1.tex b/day1/cheatsheet1.tex index 583e098..4a71028 100644 --- a/day1/cheatsheet1.tex +++ b/day1/cheatsheet1.tex @@ -2,54 +2,43 @@ \title{Interactive Plotting} \author{FOSSEE} \begin{document} -\maketitle - +\date{} +\vspace{-1in} +\begin{center} +\LARGE{Interactive Plotting}\\ +\large{FOSSEE} +\end{center} \section{Starting up...} + \begin{verbatim} $ ipython -pylab \end{verbatim} -Exiting Ipython +Exiting \begin{verbatim} - In [1]: print "Hello, World!" - In [2]: ^D - Do you really want to exit ([y]/n)? y +In [2]: (Ctrl-D)^D +Do you really want to exit ([y]/n)? y \end{verbatim} Breaking out of loops \begin{verbatim} - In [1]: while True: - ...: print "Hello, World!" - ...: - Hello, World! - Hello, World!^C -\end{verbatim} - -\section{First Plot} -\begin{verbatim} - In [2]: x=linspace(0, 2*pi, 50) - - In [3]: plot(x,sin(x)) - Out[3]: [<matplotlib.lines.Line2D object at 0xb6e5874c>] +In [1]: while True: + ...: print "Hello, World!" + ...: +Hello, World! +Hello, World!(Ctrl-C)^C \end{verbatim} -\section{Labeling} +\section{Plotting} \begin{verbatim} - In [4]: xlabel('x') - - In [5]: ylabel('sin(x)') +In [1]: x = linspace(0, 2*pi, 50) +In [2]: plot(x,sin(x)) +In [3]: xlabel('x') +In [4]: ylabel('sin(x)') +In [5]: legend(['x', '-x', 'sin(x)', 'xsin(x)']) +In [6]: annotate('origin', xy=(0, 0), xytext=(0, -7), + arrowprops=dict(shrink=0.05)) +In [7]: xlim(5*pi, 5*pi) +In [8]: ylim(5*pi, 5*pi) +In [9]: clf() #Clears the Plot area \end{verbatim} - -\section{Another example} -\begin{verbatim} -In [6]: clf() - -In [7]: y = linspace(0,10,101) - -In [8]: plot(y, exp(-y)) - -In [9]: xlabel('y') - -In [10]: ylabel(r'$e^{-y}$') -\end{verbatim} - \end{document} diff --git a/day1/data/annotate.png b/day1/data/annotate.png Binary files differindex a9f9a04..368e1ef 100644 --- a/day1/data/annotate.png +++ b/day1/data/annotate.png diff --git a/day1/data/interpolate.png b/day1/data/interpolate.png Binary files differnew file mode 100755 index 0000000..dd91788 --- /dev/null +++ b/day1/data/interpolate.png diff --git a/day1/data/missing_points.png b/day1/data/missing_points.png Binary files differnew file mode 100755 index 0000000..ff74050 --- /dev/null +++ b/day1/data/missing_points.png diff --git a/day1/data/pos_vel_accel.png b/day1/data/pos_vel_accel.png Binary files differnew file mode 100755 index 0000000..b495123 --- /dev/null +++ b/day1/data/pos_vel_accel.png diff --git a/day1/links.tex b/day1/links.tex index 7734d47..c820d22 100644 --- a/day1/links.tex +++ b/day1/links.tex @@ -1,6 +1,7 @@ \documentclass[12pt]{article} \title{Links and References} -\author{Asokan Pichai\\Prabhu Ramachandran} +\author{FOSSEE} +\date{} \begin{document} \maketitle \begin{itemize} @@ -8,7 +9,9 @@ \item ``may be one of the thinnest programming language books on my shelf, but it's also one of the best.'' -- \emph{Slashdot, AccordianGuy, September 8, 2004}- available at \url{http://diveintopython.org/} \item How to Think Like a Computer Scientist: Learning with Python available at \url{http://www.openbookproject.net/thinkcs/python/english/}\\``The concepts covered here apply to all programming languages and to problem solving in general.'' -- \emph{Guido van Rossum, creator of Python} \item Some assorted articles related to Python \url{http://effbot.org/zone/index.htm} + \item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} \item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods} + \item Some coding conventions for using Python language are available at \\ \url{http://www.python.org/dev/peps/pep-0008/} \item For documentation on IPython refer: \\ \url{http://ipython.scipy.org/moin/Documentation} \item Documentation for Numpy and Scipy is available at: \url{http://docs.scipy.org/doc/} \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \url{http://www.scipy.org/Cookbook/} diff --git a/day1/session1.tex b/day1/session1.tex index 85a8f48..fbaadc3 100644 --- a/day1/session1.tex +++ b/day1/session1.tex @@ -49,7 +49,7 @@ } \newcounter{time} \setcounter{time}{0} -\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} +\newcommand{%\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} \newcommand{\typ}[1]{\lstinline{#1}} @@ -77,7 +77,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {31, October 2009\\Day 1, Session 1} +\date[] {31 October, 2009\\Day 1, Session 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -106,7 +106,7 @@ % the following command: %\beamerdefaultoverlayspecification{<+->} -%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} +%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % DOCUMENT STARTS @@ -129,10 +129,10 @@ \item[Session 2] Sat 10:05--11:05 \item[Session 3] Sat 11:20--12:20 \item[Session 4] Sat 12:25--13:25 - \item[Quiz -1] Sat 14:25--14:40 + \item[Quiz 1] Sat 14:25--14:40 \item[Session 5] Sat 14:40--15:40 \item[Session 6] Sat 15:55--16:55 - \item[Quiz -2] Sat 17:00--17:15 + \item[Quiz 2] Sat 17:00--17:15 \end{description} \end{frame} @@ -143,10 +143,10 @@ \item[Session 2] Sun 10:05--11:05 \item[Session 3] Sun 11:20--12:20 \item[Session 4] Sun 12:25--13:25 - \item[Quiz -1] Sun 14:25--14:40 + \item[Quiz 1] Sun 14:25--14:40 \item[Session 5] Sun 14:40--15:40 \item[Session 6] Sun 15:55--16:55 - \item[Quiz -2] Sun 17:00--17:15 + \item[Quiz 2] Sun 17:00--17:15 \end{description} \end{frame} @@ -161,9 +161,9 @@ \begin{block}{Goal: Successful participants will be able to} \begin{itemize} - \item Use Python as plotting, computational toolkit - \item Understand how Python can be used as scripting and problem solving language. - \item Train the students to use Python for the same + \item Use Python as plotting, computational tool + \item Understand how Python can be used as a scripting and problem solving language. + \item Train students for the same \end{itemize} \end{block} \end{frame} @@ -172,14 +172,25 @@ \begin{frame} \frametitle{Checklist} \begin{enumerate} - \item IPython: Type ipython at the command line. Is it available? + \item IPython \item Editor: We recommend scite. - \item Data files: Make sure you have all data files. + \item Data files: + \begin{itemize} + \item \typ{sslc1.txt} + \item \typ{pendulum.txt} + \item \typ{points.txt} + \item \typ{pos.txt} + \end{itemize} + \item Python scripts: + \begin{itemize} + \item \typ{sslc_allreg.py} + \item \typ{sslc_science.py} + \end{itemize} \end{enumerate} \end{frame} \begin{frame}[fragile] -\frametitle{Starting up...} +\frametitle{Starting up \ldots} \begin{block}{} \begin{verbatim} $ ipython -pylab @@ -196,20 +207,20 @@ Exiting \end{lstlisting} \end{frame} -\begin{frame}[fragile] -\frametitle{Loops} -Breaking out of loops -\begin{lstlisting} - In []: while True: - ...: print "Hello, World!" - ...: - Hello, World! - Hello, World!^C(Ctrl-C) - ------------------------------------ - KeyboardInterrupt - -\end{lstlisting} -\end{frame} +%% \begin{frame}[fragile] +%% \frametitle{Loops} +%% Breaking out of loops +%% \begin{lstlisting} +%% In []: while True: +%% ...: print "Hello, World!" +%% ...: +%% Hello, World! +%% Hello, World!^C(Ctrl-C) +%% ------------------------------------ +%% KeyboardInterrupt + +%% \end{lstlisting} +%% \end{frame} \section{Plotting} @@ -224,7 +235,7 @@ Breaking out of loops \begin{block}{} \begin{small} \begin{lstlisting} -In []: x = linspace(0, 2*pi, 51) +In []: x = linspace(0, 2*pi, 50) In []: plot(x, sin(x)) \end{lstlisting} \end{small} @@ -246,6 +257,7 @@ x[num - 1] = end \begin{block}{\typ{plot(x, y)}} plots \typ{x} and \typ{y} using default line style and color \end{block} +%\inctime{10} \end{frame} \subsection{Decoration} @@ -279,12 +291,11 @@ In []: ylabel('sin(x)') \begin{lstlisting} In []: clf() #Clears the plot area. -In []: y = linspace(0, 2*pi, 51) +In []: y = linspace(0, 2*pi, 50) In []: plot(y, sin(2*y)) In []: xlabel('y') In []: ylabel('sin(2y)') \end{lstlisting} -\emphbar{By default plots would be overlaid!} \end{frame} \subsection{More decoration} @@ -308,11 +319,10 @@ In []: legend(['sin(2y)']) \begin{frame}[fragile] \frametitle{Legend Placement} - \begin{block}{} \small \begin{lstlisting} -In []: legend(['sin(2y)'], loc='center') +In []: legend(['sin(2y)'], loc = 'center') \end{lstlisting} \end{block} @@ -348,6 +358,7 @@ In []: legend(['sin(2y)'], loc=(.8,.1)) \begin{center} \includegraphics[height=2in, interpolate=true]{data/loc} \end{center} +%\inctime{10} \end{frame} \begin{frame}[fragile] @@ -361,8 +372,22 @@ In []: close() \section{Multiple plots} \begin{frame}[fragile] +\frametitle{Overlaid Plots} +\begin{lstlisting} +In []: clf() +In []: plot(y, sin(y)) +In []: plot(y, cos(y)) +In []: xlabel('y') +In []: ylabel('f(y)') +In []: legend(['sin(y)', 'cos(y)']) +\end{lstlisting} +\emphbar{By default plots would be overlaid!} +\end{frame} + +\begin{frame}[fragile] \frametitle{Plotting separate figures} \begin{lstlisting} +In []: clf() In []: figure(1) In []: plot(y, sin(y)) In []: figure(2) @@ -381,23 +406,20 @@ In []: close() In []: plot(y, sin(y), 'g') In []: clf() -In []: plot(y, sin(y), linewidth=2) +In []: plot(y, sin(y), 'g', linewidth=2) \end{lstlisting} \vspace*{-0.2in} \begin{center} \includegraphics[height=2.2in, interpolate=true]{data/green} \end{center} +%\inctime{10} \end{frame} \begin{frame}[fragile] \frametitle{Annotating} \vspace*{-0.15in} \begin{lstlisting} -In []: annotate('local max', - xy=(1.5, 1), - xytext=(2.5, .8), - arrowprops=dict( - shrink=0.05),) +In []: annotate('local max', xy=(1.5, 1)) \end{lstlisting} \vspace*{-0.2in} \begin{center} @@ -415,7 +437,7 @@ In []: ymin, ymax = ylim() In []: xmax = 2*pi #Set the axes limits In []: xlim(xmin, xmax) -In []: ylim(ymin, ymax) +In []: ylim(ymin-0.2, ymax+0.2) \end{lstlisting} \end{frame} @@ -428,34 +450,29 @@ In []: ylim(ymin, ymax) \item Set axis limits to the range of x \end{enumerate} \begin{lstlisting} -In []: x=linspace(-5*pi, 5*pi, 501) +In []: x=linspace(-5*pi, 5*pi, 500) In []: plot(x, x, 'b') In []: plot(x, -x, 'b') \end{lstlisting} $\vdots$ \end{frame} -\section{Exercises} \begin{frame}[fragile] \frametitle{Review Problem \ldots} -\small{ \begin{lstlisting} In []: plot(x, sin(x), 'g', linewidth=2) -In []: plot(x, x*sin(x), 'r', linewidth=3) +In []: plot(x, x*sin(x), 'r', + linewidth=3) \end{lstlisting} - \begin{lstlisting} -In []: legend(['x', '-x', 'sin(x)', 'xsin(x)']) -In []: annotate('origin', - xy=(0, 0), - xytext=(0, -7), - arrowprops=dict( - shrink=0.05)) -In []: xlim(5*pi, 5*pi) -In []: ylim(5*pi, 5*pi) +In []: legend(['x', '-x', 'sin(x)', + 'xsin(x)']) +In []: annotate('origin', xy = (0, 0)) +In []: xlim(-5*pi, 5*pi) +In []: ylim(-5*pi, 5*pi) \end{lstlisting} -} \end{frame} + \begin{frame} \frametitle{What did we learn?} \begin{itemize} @@ -467,6 +484,7 @@ In []: ylim(5*pi, 5*pi) \begin{block}{Note} \centerline{\alert{Don't Close \typ{IPython}}} \end{block} +%%\inctime{10} \end{frame} \end{document} diff --git a/day1/session2.tex b/day1/session2.tex index 6ebd58a..41e71d7 100644 --- a/day1/session2.tex +++ b/day1/session2.tex @@ -265,6 +265,7 @@ Out[]: [2, 3, 4] \alert{\typ{list[initial:final]}} \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..a6ed0fd 100644 --- a/day1/session3.tex +++ b/day1/session3.tex @@ -527,115 +527,4 @@ In []: TSq = T*T \end{lstlisting} \end{frame} -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-Line.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/L-Tsq-points.png} -\end{figure} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Least Squares Fit} -\vspace{-0.15in} -\begin{figure} -\includegraphics[width=4in]{data/least-sq-fit.png} -\end{figure} -\end{frame} - -\begin{frame} -\frametitle{Least Square Fit Curve} -\begin{itemize} -\item $T^2$ and $L$ have a linear relationship -\item Hence, Least Square Fit Curve is a line -\item we shall use the \typ{lstsq} function -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{lstsq}} -\begin{itemize} -\item We need to fit a line through points for the equation $T^2 = m \cdot L+c$ -\item The equation can be re-written as $T^2 = A \cdot p$ -\item where A is - $\begin{bmatrix} - L_1 & 1 \\ - L_2 & 1 \\ - \vdots & \vdots\\ - L_N & 1 \\ - \end{bmatrix}$ - and p is - $\begin{bmatrix} - m\\ - c\\ - \end{bmatrix}$ -\item We need to find $p$ to plot the line -\end{itemize} -\end{frame} - -\subsection{Van der Monde matrix generation} -\begin{frame}[fragile] -\frametitle{Van der Monde Matrix} -\begin{itemize} -\item A is also called a Van der Monde matrix -\item It can be generated using \typ{vander} -\end{itemize} -\begin{lstlisting} -In []: A = vander(L, 2) -\end{lstlisting} -Gives the required Van der Monde matrix -\begin{equation*} - \begin{bmatrix} - l_1 & 1 \\ - l_2 & 1 \\ - \vdots & \vdots\\ - l_N & 1 \\ - \end{bmatrix} -\end{equation*} - -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{lstsq} \ldots} -\begin{itemize} -\item Now use the \typ{lstsq} function -\item Along with a lot of things, it returns the least squares solution -\end{itemize} -\begin{lstlisting} -In []: coef, res, r, s = lstsq(A,TSq) -\end{lstlisting} -\end{frame} - -\subsection{Plotting} -\begin{frame}[fragile] -\frametitle{Least Square Fit Line \ldots} -We get the points of the line from \typ{coef} -\begin{lstlisting} -In []: Tline = coef[0]*L + coef[1] -\end{lstlisting} -\begin{itemize} -\item Now plot Tline vs. L, to get the Least squares fit line. -\end{itemize} -\begin{lstlisting} -In []: plot(L, Tline) -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] - \frametitle{What did we learn?} - \begin{itemize} - \item Least square fit - \item Van der Monde matrix generation - \item Plotting the least square fit curve - \end{itemize} -\end{frame} - \end{document} diff --git a/day1/session5.tex b/day1/session5.tex index ac1d8b6..22a274d 100644 --- a/day1/session5.tex +++ b/day1/session5.tex @@ -74,7 +74,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Calculus]{Python for Science and Engg: Interpolation, Differentiation and Integration} +\title[]{} \author[FOSSEE] {FOSSEE} @@ -125,306 +125,5 @@ %% \end{frame} -\section{\typ{loadtxt}} - -\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 []: data = loadtxt('pendulum.txt') -In []: L = data[:,0] -In []: T = data[:,1] -\end{lstlisting} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{\typ{loadtxt}} -%% \end{frame} - -\section{Interpolation} -\begin{frame}[fragile] -\frametitle{Interpolation} -\begin{itemize} - \item Given data file \typ{points.txt}. - \item It contains x,y position of particle. - \item Plot the given points. -%% \item Interpolate the missing region. -\end{itemize} -\emphbar{Loading data (revisited)} -\begin{lstlisting} -In []: data = loadtxt('points.txt') -In []: data.shape -Out[]: (40, 2) -In []: x = data[:,0] -In []: y = data[:,1] -In []: plot(x, y, '.') -\end{lstlisting} -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{small} -%% \typ{In []: from scipy.interpolate import interp1d} -%% \end{small} -%% \begin{itemize} -%% \item The \typ{interp1d} function returns a function -%% \begin{lstlisting} -%% In []: f = interp1d(L, T) -%% \end{lstlisting} -%% \item Functions can be assigned to variables -%% \item This function interpolates between known data values to obtain unknown -%% \end{itemize} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{lstlisting} -%% In []: Ln = arange(0.1,0.99,0.005) -%% # Interpolating! -%% # The new values in range of old data -%% In []: plot(L, T, 'o', Ln, f(Ln), '-') -%% In []: f = interp1d(L, T, kind='cubic') -%% # When kind not specified, it's linear -%% # Others are ... -%% # 'nearest', 'zero', -%% # 'slinear', 'quadratic' -%% \end{lstlisting} -%% \end{frame} - -\begin{frame}[fragile] -\frametitle{Spline Interpolation} -\begin{small} -\begin{lstlisting} -In []: from scipy.interpolate import splrep -In []: from scipy.interpolate import splev -\end{lstlisting} -\end{small} -\begin{itemize} -\item Involves two steps - \begin{enumerate} - \item Find out the spline curve, coefficients - \item Evaluate the spline at new points - \end{enumerate} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{splrep}} -To find the spline curve -\begin{lstlisting} -In []: tck = splrep(x, y) -\end{lstlisting} -\typ{tck} contains parameters required for representing the spline curve! -\end{frame} - -\begin{frame}[fragile] -\frametitle{\typ{splev}} -To Evaluate a B-spline and it's derivatives -\begin{lstlisting} -In []: Xnew = arange(0.01,3,0.02) -In []: Ynew = splev(Xnew, tck) - -In []: y.shape -Out[]: (40,) - -In []: Ynew.shape -Out[]: (150,) - -In []: plot(Xnew, Ynew) -\end{lstlisting} - -\end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Interpolation \ldots} -%% \begin{itemize} -%% \item -%% \end{itemize} -%% \end{frame} - -\section{Differentiation} - -\begin{frame}[fragile] -\frametitle{Numerical Differentiation} -\begin{itemize} -\item Given function $f(x)$ or data points $y=f(x)$ -\item We wish to calculate $f^{'}(x)$ at points $x$ -\item Taylor series - finite difference approximations -\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 -\end{tabular} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Forward Difference} -\begin{lstlisting} -In []: x = linspace(0, 2*pi, 100) -In []: y = sin(x) -In []: deltax = x[1] - x[0] -\end{lstlisting} -Obtain the finite forward difference of y -\end{frame} - -\begin{frame}[fragile] -\frametitle{Forward Difference \ldots} -\begin{lstlisting} -In []: fD = (y[1:] - y[:-1]) / deltax -In []: plot(x, y, x[:-1], fD) -\end{lstlisting} -\begin{center} - \includegraphics[height=2in, interpolate=true]{data/fwdDiff} -\end{center} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Example} -\begin{itemize} -\item Given x, y positions of a particle in \typ{pos.txt} -\item Find velocity \& acceleration in x, y directions -\end{itemize} -\small{ -\begin{center} -\begin{tabular}{| c | c | c |} -\hline -$X$ & $Y$ \\ \hline -0. & 0.\\ \hline -0.25 & 0.47775\\ \hline -0.5 & 0.931\\ \hline -0.75 & 1.35975\\ \hline -1. & 1.764\\ \hline -1.25 & 2.14375\\ \hline -\vdots & \vdots\\ \hline -\end{tabular} -\end{center}} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Example \ldots} -\begin{itemize} -\item Read the file -\item Obtain an array of x, y -\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]) -\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 - -In []: v = (S[:,1:]-S[:,:-1])/deltaT - -In []: a = (v[:,1:]-v[:,:-1])/deltaT -\end{lstlisting} -Try Plotting the position, velocity \& acceleration. -\end{frame} - -\section{Quadrature} - -\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} -\begin{itemize} -\item Inputs - function to integrate, limits -\end{itemize} -\begin{lstlisting} -In []: x = 0 -In []: quad(sin(x)+x**2, 0, 1) -\end{lstlisting} -\begin{small} -\alert{\typ{error:}} -\typ{First argument must be a callable function.} -\end{small} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Functions - Definition} -We have been using them all along. Now let's see how to define them. -\begin{lstlisting} -In []: def f(x): - return sin(x)+x**2 -In []: quad(f, 0, 1) -\end{lstlisting} -\begin{itemize} -\item \typ{def} -\item name -\item arguments -\item \typ{return} -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Functions - Calling them} -\begin{lstlisting} -In [15]: f() ---------------------------------------- -\end{lstlisting} -\alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument} -\typ{(0 given)} -\begin{lstlisting} -In []: f(0) -Out[]: 0.0 -In []: f(1) -Out[]: 1.8414709848078965 -\end{lstlisting} -More on Functions later \ldots -\end{frame} - -\begin{frame}[fragile] -\frametitle{Quadrature \ldots} -\begin{lstlisting} -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 -\end{itemize} -\end{frame} - -\begin{frame} - \frametitle{Things we have learned} - \begin{itemize} - \item Interpolation - \item Differentiation - \item Functions - \begin{itemize} - \item Definition - \item Calling - \item Default Arguments - \item Keyword Arguments - \end{itemize} - \item Quadrature - \end{itemize} -\end{frame} - \end{document} diff --git a/day1/session6.tex b/day1/session6.tex index 0b5f681..01b0fc9 100644..100755 --- a/day1/session6.tex +++ b/day1/session6.tex @@ -73,7 +73,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[ODEs \& Root Finding]{Python for Science and Engg:\\ODEs \& Finding Roots} +\title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs} \author[FOSSEE] {FOSSEE} @@ -123,219 +123,166 @@ %% % You might wish to add the option [pausesections] %% \end{frame} -\section{ODEs} +\section{Solving linear equations} \begin{frame}[fragile] -\frametitle{ODE Integration} -We shall use the simple ODE of a simple pendulum. -\begin{equation*} -\ddot{\theta} = -\frac{g}{L}sin(\theta) -\end{equation*} -\begin{itemize} -\item This equation can be written as a system of two first order ODEs -\end{itemize} -\begin{align} -\dot{\theta} &= \omega \\ -\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ - \text{At}\ t &= 0 : \nonumber \\ - \theta = \theta_0\quad & \&\quad \omega = 0 \nonumber -\end{align} +\frametitle{Solution of equations} +Consider, + \begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 + \end{align*} +Solution: + \begin{align*} + x & = 1 \\ + y & = -2 \\ + z & = -2 + \end{align*} \end{frame} \begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy} -\begin{itemize} -\item We use the \typ{odeint} function from scipy to do the integration -\item Define a function as below -\end{itemize} -\begin{lstlisting} -In []: def pend_int(unknown, t, p): - .... theta, omega = unknown - .... g, L = p - .... f=[omega, -(g/L)*sin(theta)] - .... return f - .... -\end{lstlisting} +\frametitle{Solving using Matrices} +Let us now look at how to solve this using \kwrd{matrices} + \begin{lstlisting} + 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} \begin{frame}[fragile] -\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} +\frametitle{Solution:} \begin{lstlisting} -In []: t = linspace(0, 10, 101) -In []: p=(-9.81, 0.2) -In []: initial = [10*2*pi/360, 0] +In []: x +Out[]: +array([[ 1.], + [-2.], + [-2.]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] -\frametitle{Solving ODEs using SciPy \ldots} -\begin{small} - \typ{In []: from scipy.integrate import odeint} -\end{small} +\frametitle{Let's check!} \begin{lstlisting} -In []: pend_sol = odeint(pend_int, - initial,t, - args=(p,)) +In []: Ax +Out[]: +array([[ 1.00000000e+00], + [ -2.00000000e+00], + [ 2.22044605e-16]]) +\end{lstlisting} +\begin{block}{} +The last term in the matrix is actually \alert{0}!\\ +We can use \kwrd{allclose()} to check. +\end{block} +\begin{lstlisting} +In []: allclose(Ax, b) +Out[]: True \end{lstlisting} +\inctime{15} \end{frame} -\section{Finding Roots} +\subsection{Exercises} \begin{frame}[fragile] -\frametitle{Roots of $f(x)=0$} +\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 Roots --- values of $x$ satisfying $f(x)=0$ -\item $f(x)=0$ may have real or complex roots -\item Presently, let's look at real roots + \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{Roots of $f(x)=0$} +\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 Given function $cosx-x^2$ -\item First we find \alert{a} root in $(-\pi/2, \pi/2)$ -\item Then we find \alert{all} roots in $(-\pi/2, \pi/2)$ + \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{Fixed Point Method} -%% \begin{enumerate} -%% \item Convert $f(x)=0$ to the form $x=g(x)$ -%% \item Start with an initial value of $x$ and iterate successively -%% \item $x_{n+1}=g(x_n)$ and $x_0$ is our initial guess -%% \item Iterate until $x_{n+1}-x_n \le tolerance$ -%% \end{enumerate} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Fixed Point \dots} -%% \begin{lstlisting} -%% In []: def our_g(x): -%% ....: return sqrt(cos(x)) -%% ....: -%% In []: tolerance = 1e-5 -%% In []: while abs(x1-x0)>tolerance: -%% ....: x0 = x1 -%% ....: x1 = our_g(x1) -%% ....: -%% In []: x0 -%% \end{lstlisting} -%% \end{frame} - \begin{frame}[fragile] -\frametitle{Bisection Method} -\begin{enumerate} -\item Start with the given interval $(-\pi/2, \pi/2)$ ($(a, b)$) -\item $f(a)\cdot f(b) < 0$ -\item Bisect the interval. $c = \frac{a+b}{2}$ -\item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$ -\item Else, change the interval to $(c, b)$ -\item Go back to 3 until $(b-a) \le$ tolerance -\end{enumerate} -\alert{\typ{tolerance = 1e-5}} +\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{Bisection \dots} -%% \begin{lstlisting} -%% In []: tolerance = 1e-5 -%% In []: a = -pi/2 -%% In []: b = 0 -%% In []: while b-a > tolerance: -%% ....: c = (a+b)/2 -%% ....: if our_f(a)*our_f(c) < 0: -%% ....: b = c -%% ....: else: -%% ....: a = c -%% ....: -%% \end{lstlisting} -%% \end{frame} - \begin{frame}[fragile] -\frametitle{Newton-Raphson Method} -\begin{enumerate} -\item Start with an initial guess of x for the root -\item $\Delta x = -f(x)/f^{'}(x)$ -\item $ x \leftarrow x + \Delta x$ -\item Go back to 2 until $|\Delta x| \le$ tolerance -\end{enumerate} +\frametitle{Problem 3} +Solve the set of equations: +\begin{align*} + x + y + 2z -w & = 3\\ + 2x + 5y - z - 9w & = -3\\ + 2x + y -z + 3w & = -11 \\ + x - 3y + 2z + 7w & = -5\\ +\end{align*} +\inctime{10} \end{frame} -%% \begin{frame}[fragile] -%% \frametitle{Newton-Raphson \dots} -%% \begin{lstlisting} -%% In []: def our_df(x): -%% ....: return -sin(x)-2*x -%% ....: -%% In []: delx = 10*tolerance -%% In []: while delx > tolerance: -%% ....: delx = -our_f(x)/our_df(x) -%% ....: x = x + delx -%% ....: -%% ....: -%% \end{lstlisting} -%% \end{frame} - -%% \begin{frame}[fragile] -%% \frametitle{Newton-Raphson \ldots} -%% \begin{itemize} -%% \item What if $f^{'}(x) = 0$? -%% \item Can you write a better version of the Newton-Raphson? -%% \item What if the differential is not easy to calculate? -%% \item Look at Secant Method -%% \end{itemize} -%% \end{frame} - \begin{frame}[fragile] -\frametitle{Initial Estimates} -\begin{itemize} -\item Given an interval -\item How to find \alert{all} the roots? -\end{itemize} -\begin{enumerate} -\item Check for change of signs of $f(x)$ in the given interval -\item A root lies in the interval where the sign change occurs -\end{enumerate} -\end{frame} - -\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) -\end{lstlisting} -\begin{itemize} -\item Get the intervals of x, where sign changes occur -\end{itemize} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Initial Estimates \ldots} -\begin{lstlisting} -In []: pos = y[:-1]*y[1:] < 0 -In []: rpos = zeros(x.shape, dtype=bool) -In []: rpos[:-1] = pos -In []: rpos[1:] += pos -In []: rts = x[rpos] -\end{lstlisting} -Now use Newton-Raphson? +\frametitle{Solution} +Use \kwrd{solve()} +\begin{align*} + x & = -5\\ + y & = 2\\ + z & = 3\\ + w & = 0\\ +\end{align*} \end{frame} +\section{Finding Roots} \begin{frame}[fragile] \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] @@ -360,29 +307,82 @@ Now use Newton-Raphson? \end{lstlisting} \end{frame} +%% \begin{frame}[fragile] +%% \frametitle{Scipy Methods \dots} +%% \begin{small} +%% \begin{lstlisting} +%% In []: from scipy.optimize import fixed_point + +%% In []: from scipy.optimize import bisect + +%% In []: from scipy.optimize import newton +%% \end{lstlisting} +%% \end{small} +%% \end{frame} + +\section{ODEs} \begin{frame}[fragile] -\frametitle{Scipy Methods \dots} -\begin{small} +\frametitle{ODE Integration} +We shall use the simple ODE of a simple pendulum. +\begin{equation*} +\ddot{\theta} = -\frac{g}{L}sin(\theta) +\end{equation*} +\begin{itemize} +\item This equation can be written as a system of two first order ODEs +\end{itemize} +\begin{align} +\dot{\theta} &= \omega \\ +\dot{\omega} &= -\frac{g}{L}sin(\theta) \\ + \text{At}\ t &= 0 : \nonumber \\ + \theta = \theta_0(10^o)\quad & \&\quad \omega = 0\ (Initial\ values)\nonumber +\end{align} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy} +\begin{itemize} +\item We use the \typ{odeint} function from scipy to do the integration +\item Define a function as below +\end{itemize} \begin{lstlisting} -In []: from scipy.optimize import fixed_point +In []: def pend_int(initial, t): + .... theta, omega = initial + .... g, L = -9.81, 0.2 + .... f=[omega, -(g/L)*sin(theta)] + .... return f + .... +\end{lstlisting} +\end{frame} -In []: from scipy.optimize import bisect +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy \ldots} +\begin{itemize} +\item \typ{t} is the time variable \\ +\item \typ{initial} has the initial values +\end{itemize} +\begin{lstlisting} +In []: t = linspace(0, 10, 101) +In []: initial = [10*2*pi/360, 0] +\end{lstlisting} +\end{frame} -In []: from scipy.optimize import newton +\begin{frame}[fragile] +\frametitle{Solving ODEs using SciPy \ldots} +%%\begin{small} +\typ{In []: from scipy.integrate import odeint} +%%\end{small} +\begin{lstlisting} +In []: pend_sol = odeint(pend_int, + initial,t) \end{lstlisting} -\end{small} \end{frame} + \begin{frame} \frametitle{Things we have learned} \begin{itemize} \item Solving ODEs \item Finding Roots - \begin{itemize} - \item Estimating Interval - \item Newton-Raphson - \item Scipy methods - \end{itemize} \end{itemize} \end{frame} |