day1/cheatsheet6.tex
changeset 317 0eca6c542fce
parent 295 39d7c4e14585
child 321 8bf99f747817
child 323 4e44d7741c94
equal deleted inserted replaced
316:6108f2007151 317:0eca6c542fce
     1 \documentclass[12pt]{article}
     1 \documentclass[12pt]{article}
     2 \title{Solving Equations \& ODEs}
     2 \title{Solving Equations \& ODEs}
     3 \author{FOSSEE}
     3 \author{FOSSEE}
       
     4 \usepackage{listings}
       
     5 \lstset{language=Python,
       
     6     basicstyle=\ttfamily,
       
     7 commentstyle=\itshape\bfseries,
       
     8 showstringspaces=false,
       
     9 }
       
    10 \newcommand{\typ}[1]{\lstinline{#1}}
       
    11 \usepackage[english]{babel}
       
    12 \usepackage[latin1]{inputenc}
       
    13 \usepackage{times}
       
    14 \usepackage[T1]{fontenc}
       
    15 \usepackage{ae,aecompl}
       
    16 \usepackage{mathpazo,courier,euler}
       
    17 \usepackage[scaled=.95]{helvet}
     4 \begin{document}
    18 \begin{document}
     5 \date{}
    19 \date{}
     6 \vspace{-1in}
    20 \vspace{-1in}
     7 \begin{center}
    21 \begin{center}
     8 \LARGE{Solving Equations \& ODEs}\\
    22 \LARGE{Solving Equations \& ODEs}\\
     9 \large{FOSSEE}
    23 \large{FOSSEE}
    10 \end{center}
    24 \end{center}
    11 \section{Solving linear equations}
    25 \section{Solving linear equations}
    12 \begin{verbatim}
    26 Condier following sets of equations:\\
    13     In []: A = array([[3,2,-1],
    27   \begin{align*}
    14                       [2,-2,4],                   
    28     3x + 2y - z  & = 1 \\
    15                       [-1, 0.5, -1]])
    29     2x - 2y + 4z  & = -2 \\
    16     In []: b = array([[1], [-2], [0]])
    30     -x + $\frac{1}{2}$y -z & = 0
    17     In []: x = solve(A, b)
    31   \end{align*}\\
    18     In []: Ax = dot(A,x)
    32 The matrix representation is:\\
    19     In []: allclose(Ax, b)
    33 \begin{center}
    20     Out[]: True
    34 $A*x = B$
    21 \end{verbatim}
    35 \end{center}
       
    36 Where A is coefficient matrix(in this case 3x3)\\
       
    37 B is constant matrix(1x3)\\
       
    38 x is the required solution.\\
       
    39 \begin{lstlisting}
       
    40 In []: A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]])
       
    41 In []: B = array([[1], [-2], [0]])
       
    42 In []: x = solve(A, B)
       
    43 \end{lstlisting}
       
    44 Solve the equation $A x = B$ for $x$.\\
       
    45 To check whether solution is correct try this:
       
    46 \begin{lstlisting}
       
    47 In []: Ax = dot(A,x)  #Matrix multiplication of A and x(LHS)
       
    48 In []: allclose(Ax, B)
       
    49 Out[]: True
       
    50 \end{lstlisting}
       
    51 \typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. 
       
    52 \newpage
    22 \section{Finding roots}
    53 \section{Finding roots}
    23 \begin{verbatim}
    54 \subsection{Polynomials}
    24   In []: coeffs = [1, 6, 13]
    55 \begin{center}
    25   In []: roots(coeffs)
    56   $x^2+6x+13=0$
    26 \end{verbatim}
    57 \end{center}
    27 Finding the roots of a function
    58 to find roots, pylab provides \typ{roots} function.
    28 \begin{verbatim}
    59 \begin{lstlisting}
    29 In []: fsolve(sin(x)+cos(x)**2, 0)
    60 In []: coeffs = [1, 6, 13] #list of all coefficients
    30 \end{verbatim}
    61 In []: roots(coeffs)
       
    62 \end{lstlisting}
       
    63 \subsection{functions}
       
    64 Functions can be defined and used by following syntax:
       
    65 \begin{lstlisting}
       
    66 def func_name(arg1, arg2):
       
    67     #function code
       
    68     return ret_value
       
    69 \end{lstlisting}
       
    70 A simple example can be
       
    71 \begin{lstlisting}
       
    72 def expression(x):
       
    73     y = x*sin(x)
       
    74     return y
       
    75 \end{lstlisting}
       
    76 Above function when called with a argument, will return $xsin(x)$ value for that argument.
       
    77 \begin{lstlisting}
       
    78 In [95]: expression(pi/2)
       
    79 Out[95]: 1.5707963267948966
       
    80 
       
    81 In [96]: expression(pi/3)
       
    82 Out[96]: 0.90689968211710881
       
    83 \end{lstlisting}
       
    84 \subsection{Roots of non-linear eqations}
       
    85 For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\
       
    86 \typ{In []: from scipy.optimize import fsolve}\\
       
    87 \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.\\
       
    88 %\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.
       
    89 For illustration, we want to find roots of equation:
       
    90 \begin{center}
       
    91   $f(x)=sin(x)+cos(x)^2$
       
    92 \end{center}
       
    93 So just like we did above, we define a function:
       
    94 \begin{lstlisting}
       
    95 In []: def f(x):
       
    96    ....:        return sin(x)+cos(x)**2
       
    97    ....: 
       
    98 \end{lstlisting}
       
    99 Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way:
       
   100 \begin{lstlisting}
       
   101 In []: fsolve(f, 0) #arguments are function name and estimate
       
   102 Out[]: -0.66623943249251527
       
   103 \end{lstlisting}
       
   104 
    31 \section{ODE}
   105 \section{ODE}
    32 \begin{verbatim}
   106 \begin{lstlisting}
    33   In []: def epid(y, t):
   107   In []: def epid(y, t):
    34   ....     k, L = 0.00003, 25000
   108   ....     k, L = 0.00003, 25000
    35   ....     return k*y*(L-y)
   109   ....     return k*y*(L-y)
    36   ....
   110   ....
    37   
   111   
    38   In []: t = arange(0, 12, 0.2)
   112   In []: t = arange(0, 12, 0.2)
    39 
   113 
    40   In []: y = odeint(epid, 250, t)
   114   In []: y = odeint(epid, 250, t)
    41 
   115 
    42   In []: plot(t, y)
   116   In []: plot(t, y)
    43 \end{verbatim}
   117 \end{lstlisting}
    44 \end{document}
   118 \end{document}