day1/cheatsheet6.tex
changeset 334 2214b5dba4d4
parent 330 46533051b9d3
child 341 7ae88b9da553
equal deleted inserted replaced
333:25b18b51be41 334:2214b5dba4d4
     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}
       
    18 \usepackage{amsmath}
     4 \begin{document}
    19 \begin{document}
     5 \date{}
    20 \date{}
     6 \vspace{-1in}
    21 \vspace{-1in}
     7 \begin{center}
    22 \begin{center}
     8 \LARGE{Solving Equations \& ODEs}\\
    23 \LARGE{Solving Equations \& ODEs}\\
     9 \large{FOSSEE}
    24 \large{FOSSEE}
    10 \end{center}
    25 \end{center}
    11 \section{Solving linear equations}
    26 \section{Solving linear equations}
    12 \begin{verbatim}
    27 Condier following sets of equations:\\
    13     In []: A = array([[3,2,-1],
    28   \begin{align*}
    14                       [2,-2,4],                   
    29     3x + 2y - z  & = 1 \\
    15                       [-1, 0.5, -1]])
    30     2x - 2y + 4z  & = -2 \\
    16     In []: b = array([[1], [-2], [0]])
    31     -x + \frac{1}{2}y -z & = 0
    17     In []: x = solve(A, b)
    32   \end{align*}\\
    18     In []: Ax = dot(A,x)
    33 The matrix representation is:\\
    19     In []: allclose(Ax, b)
    34 \begin{center}
    20     Out[]: True
    35 $A*x = B$
    21 \end{verbatim}
    36 \end{center}
       
    37 Where A is coefficient matrix(in this case 3x3)\\
       
    38 B is constant matrix(1x3)\\
       
    39 x is the required solution.\\
       
    40 \begin{lstlisting}
       
    41 In []: A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]])
       
    42 In []: B = array([[1], [-2], [0]])
       
    43 In []: x = solve(A, B)
       
    44 \end{lstlisting}
       
    45 Solve the equation $A x = B$ for $x$.\\
       
    46 To check whether solution is correct try this:
       
    47 \begin{lstlisting}
       
    48 In []: Ax = dot(A,x)  #Matrix multiplication of A and x(LHS)
       
    49 In []: allclose(Ax, B)
       
    50 Out[]: True
       
    51 \end{lstlisting}
       
    52 \typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. 
       
    53 \newpage
    22 \section{Finding roots}
    54 \section{Finding roots}
    23 \begin{verbatim}
    55 \subsection{Polynomials}
    24   In []: coeffs = [1, 6, 13]
    56 \begin{center}
    25   In []: roots(coeffs)
    57   $x^2+6x+13=0$
    26 \end{verbatim}
    58 \end{center}
    27 Finding the roots of a function
    59 to find roots, pylab provides \typ{roots} function.
    28 \begin{verbatim}
    60 \begin{lstlisting}
    29 In []: fsolve(sin(x)+cos(x)**2, 0)
    61 In []: coeffs = [1, 6, 13] #list of all coefficients
    30 \end{verbatim}
    62 In []: roots(coeffs)
       
    63 \end{lstlisting}
       
    64 \subsection{functions}
       
    65 Functions can be defined and used by following syntax:
       
    66 \begin{lstlisting}
       
    67 def func_name(arg1, arg2):
       
    68     #function code
       
    69     return ret_value
       
    70 \end{lstlisting}
       
    71 A simple example can be
       
    72 \begin{lstlisting}
       
    73 def expression(x):
       
    74     y = x*sin(x)
       
    75     return y
       
    76 \end{lstlisting}
       
    77 Above function when called with a argument, will return $xsin(x)$ value for that argument.
       
    78 \begin{lstlisting}
       
    79 In [95]: expression(pi/2)
       
    80 Out[95]: 1.5707963267948966
       
    81 
       
    82 In [96]: expression(pi/3)
       
    83 Out[96]: 0.90689968211710881
       
    84 \end{lstlisting}
       
    85 \subsection{Roots of non-linear equations}
       
    86 For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\
       
    87 \typ{In []: from scipy.optimize import fsolve}\\
       
    88 \typ{fsolve} is not a part of \typ{pylab}, instead is a function in the \textbf{optimize} module of \textbf{scipy}, and hence we \textbf{import} it.\\
       
    89 %\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.
       
    90 For illustration, we want to find roots of equation:
       
    91 \begin{center}
       
    92   $f(x)=sin(x)+cos(x)^2$
       
    93 \end{center}
       
    94 So just like we did above, we define a function:
       
    95 \begin{lstlisting}
       
    96 In []: def f(x):
       
    97    ....:        return sin(x)+cos(x)**2
       
    98    ....: 
       
    99 \end{lstlisting}
       
   100 Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way:
       
   101 \begin{lstlisting}
       
   102 In []: fsolve(f, 0) #arguments are function name and estimate
       
   103 Out[]: -0.66623943249251527
       
   104 \end{lstlisting}
       
   105 
    31 \section{ODE}
   106 \section{ODE}
    32 \begin{verbatim}
   107 
    33   In []: def epid(y, t):
   108 We wish to solve an (a system of) Ordinary Differential Equation. For this purpose, we shall use \typ{odeint}.\\
       
   109 \typ{In []: from scipy.integrate import odeint}\\
       
   110 \typ{odeint} is a function in the \textbf{integrate} module of \textbf{scipy}.\\
       
   111 As an illustration, let us solve the ODE below:\\
       
   112 $\frac{dy}{dt} = ky(L-y)$, L = 25000, k = 0.00003, y(0) = 250\\
       
   113 We define a function (as below) that takes $y$ and time as arguments and returns the right hand side of the ODE.
       
   114 \begin{lstlisting}
       
   115 In []: def f(y, t):
    34   ....     k, L = 0.00003, 25000
   116   ....     k, L = 0.00003, 25000
    35   ....     return k*y*(L-y)
   117   ....     return k*y*(L-y)
    36   ....
   118   ....
    37   
   119 \end{lstlisting}
    38   In []: t = arange(0, 12, 0.2)
   120 Next we define the time over which we wish to solve the ODE. We also note the initial conditions given to us.
    39 
   121 \begin{lstlisting}
    40   In []: y = odeint(epid, 250, t)
   122 In []: t = linspace(0, 12, 61)
    41 
   123 In []: y0 = 250
    42   In []: plot(t, y)
   124 \end{lstlisting}
    43 \end{verbatim}
   125 To solve the ODE, we call the \typ{odeint} function with three arguments - the function \typ{f}, initial conditions and the time vector. 
       
   126 \begin{lstlisting}
       
   127 In []: y = odeint(f, y0, t)
       
   128 \end{lstlisting}
       
   129 Note: To solve a system of ODEs, we need to change the function to return the right hand side of all the equations and the system and the pass the required number of initial conditions to the \typ{odeint} function.
       
   130 \section{Links and References}
       
   131 \begin{itemize}
       
   132 \item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/
       
   133   \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \\ http://www.scipy.org/Cookbook/
       
   134 \end{itemize}
    44 \end{document}
   135 \end{document}