Modified cheat sheet of session 1 and session 6 day 1.
authorShantanu <shantanu@fossee.in>
Thu, 19 Nov 2009 22:26:00 +0530
changeset 317 0eca6c542fce
parent 316 6108f2007151
child 320 d592e3a874f5
child 321 8bf99f747817
Modified cheat sheet of session 1 and session 6 day 1.
day1/cheatsheet1.tex
day1/cheatsheet6.tex
--- a/day1/cheatsheet1.tex	Thu Nov 19 11:04:11 2009 +0530
+++ b/day1/cheatsheet1.tex	Thu Nov 19 22:26:00 2009 +0530
@@ -37,19 +37,52 @@
 \end{lstlisting} %$
 
 \section{Plotting}
+\subsection{linspace}
+\typ{In []: x = linspace(start, stop, num)}\\
+\typ{linspace} returns array of length \typ{num}, for which \typ{x[0] = start} and \typ{x[num-1] = stop} \\
+\emph{Please note indices of array starts from zero(0)}
 
-\subsection{Label}
+\subsection{plot}
+\typ{In []: plot(X, Y)}\\
+For given arrays of equal length(above case X and Y), \typ{plot} plots the correspoding *x* and *y* pairs taken from X and Y.
+
+\subsection{Colors of plots}
+\typ{In []: plot(y, sin(y), 'g')}\\
+Plots graph with green color. Other options available are:
+\begin{lstlisting}
+  'r' ---> Red
+  'b' ---> Blue
+  'r' ---> Red 
+  'c' ---> Cyan 
+  'm' ---> Magenta
+  'y' ---> Yellow
+  'k' ---> Black 
+  'w' ---> White
+\end{lstlisting}
+One can also set the line width of plot using optional argument \typ{linewidth}. For example:\\
+\typ{In []: plot(x, cos(x), 'r', linewidth=2)}\\
+Plots the line with linewidth = 2
+\subsection{label and title}
+\typ{In []: xlabel('Length') #sets *x* axis label to Length}\\ 
+\typ{In []: ylabel('Time') #sets *y* axis label to Time.}\\
+\typ{In []: title('Sinusoids') #sets title of plot}\\
+\\
+\textbf{Additionally}\\
 Pylab accepts TeX equation expressions in any text expression. To get something like:\\
 $\sigma_i=15$ \\
 on title of figure use: 
 \begin{lstlisting}
-  title('$\sigma_i=15$')
+In []: title('$\sigma_i=15$')
 \end{lstlisting}  
 Same way one can have TeX expression on xlabel, ylabel etc.
 
 \subsection{legends}
+\typ{In []: legend('sin(x)',loc=center)} \\
+Placec a legend on the current plot at location *loc*.\\
 Apart from \kwrd{center}, some other \kwrd{loc} which can be specified are:
 \begin{lstlisting}
+'best'
+'right'
 'upper right'
 'upper left'      
 'lower left'      
@@ -64,26 +97,20 @@
 \begin{lstlisting}
 In []: legend(['sin(2y)'], loc=(.8,.1)) 
 \end{lstlisting}
-\typ{loc = 0, 1} (left top position of graph)\\
+\typ{loc = 0, 1} (top left position of graph)\\
 \typ{loc = 0.5, 0.5} (center of graph).
 
+\subsection{Annotate}
+\typ{In []: annotate('local max', xy=(1.5, 1))}\\
+Annotates current plot with text, 'local max', at position specified to \typ{xy}.
+
 \subsection{Saving figures}
-One can save figure in any of these formats: png, pdf, ps, eps and svg.
+\typ{In []: savefig('sinusoids.png')}\\
+Saves the current figure with file name 'sinusoids.png' in current working directory. One can save figure in any of these formats: png, pdf, ps, eps and svg.
 
-\subsection{Colors of plots}
-\typ{In []: plot(y, sin(y), 'g')}\\
-Plots graph with green color. Other options available are:
-\begin{lstlisting}
-  'r' ---> Red
-  'b' ---> Blue
-  'r' ---> Red 
-  'c' ---> Cyan 
-  'm' ---> Magenta
-  'y' ---> Yellow
-  'k' ---> Black 
-  'w' ---> White
-\end{lstlisting}
-
+\subsection{Miscellaneous}
+\typ{In []: clf() #Clears the current plot area}\\
+\typ{In []: close() #Closes the figure}
 \section{Saving and running scripts}
 \begin{itemize}
   \item \typ{\%hist}\\
@@ -105,14 +132,13 @@
 In []: title('Sinusoidal Waves')
 In []: legend(['sin(x)', 'cos(x)'])
 In []: annotate('origin', xy=(0, 0))
-In []: xmin, xman = xlim()           # Without arguments gets
-In []: ymin, ymax = ylim()           # values
-
-In []: xlim(0, 2 * pi)               # With values, sets the
-In []: ylim(ymin - 0.2, ymax + 0.2)  # specified values
+In []: xmin, xman = xlim()  # returns current X axis limits.
+In []: ymin, ymax = ylim()
+In []: xlim(0, 2 * pi) # sets the X axis limits to passed values
+In []: ylim(ymin - 0.2, ymax + 0.2) 
 
 In []: savefig('sin.png')   # Save figure
-In []: close()              # Closes the figure
+In []: close()              
   \end{lstlisting}
 
 \section{References}
--- a/day1/cheatsheet6.tex	Thu Nov 19 11:04:11 2009 +0530
+++ b/day1/cheatsheet6.tex	Thu Nov 19 22:26:00 2009 +0530
@@ -1,6 +1,20 @@
 \documentclass[12pt]{article}
 \title{Solving Equations \& ODEs}
 \author{FOSSEE}
+\usepackage{listings}
+\lstset{language=Python,
+    basicstyle=\ttfamily,
+commentstyle=\itshape\bfseries,
+showstringspaces=false,
+}
+\newcommand{\typ}[1]{\lstinline{#1}}
+\usepackage[english]{babel}
+\usepackage[latin1]{inputenc}
+\usepackage{times}
+\usepackage[T1]{fontenc}
+\usepackage{ae,aecompl}
+\usepackage{mathpazo,courier,euler}
+\usepackage[scaled=.95]{helvet}
 \begin{document}
 \date{}
 \vspace{-1in}
@@ -9,27 +23,87 @@
 \large{FOSSEE}
 \end{center}
 \section{Solving linear equations}
-\begin{verbatim}
-    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)
-    In []: allclose(Ax, b)
-    Out[]: True
-\end{verbatim}
+Condier following sets of equations:\\
+  \begin{align*}
+    3x + 2y - z  & = 1 \\
+    2x - 2y + 4z  & = -2 \\
+    -x + $\frac{1}{2}$y -z & = 0
+  \end{align*}\\
+The matrix representation is:\\
+\begin{center}
+$A*x = B$
+\end{center}
+Where A is coefficient matrix(in this case 3x3)\\
+B is constant matrix(1x3)\\
+x is the required solution.\\
+\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)
+\end{lstlisting}
+Solve the equation $A x = B$ for $x$.\\
+To check whether solution is correct try this:
+\begin{lstlisting}
+In []: Ax = dot(A,x)  #Matrix multiplication of A and x(LHS)
+In []: allclose(Ax, B)
+Out[]: True
+\end{lstlisting}
+\typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. 
+\newpage
 \section{Finding roots}
-\begin{verbatim}
-  In []: coeffs = [1, 6, 13]
-  In []: roots(coeffs)
-\end{verbatim}
-Finding the roots of a function
-\begin{verbatim}
-In []: fsolve(sin(x)+cos(x)**2, 0)
-\end{verbatim}
+\subsection{Polynomials}
+\begin{center}
+  $x^2+6x+13=0$
+\end{center}
+to find roots, pylab provides \typ{roots} function.
+\begin{lstlisting}
+In []: coeffs = [1, 6, 13] #list of all coefficients
+In []: roots(coeffs)
+\end{lstlisting}
+\subsection{functions}
+Functions can be defined and used by following syntax:
+\begin{lstlisting}
+def func_name(arg1, arg2):
+    #function code
+    return ret_value
+\end{lstlisting}
+A simple example can be
+\begin{lstlisting}
+def expression(x):
+    y = x*sin(x)
+    return y
+\end{lstlisting}
+Above function when called with a argument, will return $xsin(x)$ value for that argument.
+\begin{lstlisting}
+In [95]: expression(pi/2)
+Out[95]: 1.5707963267948966
+
+In [96]: expression(pi/3)
+Out[96]: 0.90689968211710881
+\end{lstlisting}
+\subsection{Roots of non-linear eqations}
+For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\
+\typ{In []: from scipy.optimize import fsolve}\\
+\typ{fsolve} is not part of \typ{pylab}, instead it is part of \textbf{optimize} package of \textbf{scipy}, and hence we \textbf{import} it.\\
+%\typ{fsolve} takes first argument as name of function, which evaluates $f(x)$, whose roots one wants to find. And second argument is starting estimate, around which roots are found.
+For illustration, we want to find roots of equation:
+\begin{center}
+  $f(x)=sin(x)+cos(x)^2$
+\end{center}
+So just like we did above, we define a function:
+\begin{lstlisting}
+In []: def f(x):
+   ....:        return sin(x)+cos(x)**2
+   ....: 
+\end{lstlisting}
+Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way:
+\begin{lstlisting}
+In []: fsolve(f, 0) #arguments are function name and estimate
+Out[]: -0.66623943249251527
+\end{lstlisting}
+
 \section{ODE}
-\begin{verbatim}
+\begin{lstlisting}
   In []: def epid(y, t):
   ....     k, L = 0.00003, 25000
   ....     return k*y*(L-y)
@@ -40,5 +114,5 @@
   In []: y = odeint(epid, 250, t)
 
   In []: plot(t, y)
-\end{verbatim}
+\end{lstlisting}
 \end{document}