day1/cheatsheet6.tex
changeset 323 4e44d7741c94
parent 317 0eca6c542fce
child 324 2361df479844
equal deleted inserted replaced
320:d592e3a874f5 323:4e44d7741c94
    13 \usepackage{times}
    13 \usepackage{times}
    14 \usepackage[T1]{fontenc}
    14 \usepackage[T1]{fontenc}
    15 \usepackage{ae,aecompl}
    15 \usepackage{ae,aecompl}
    16 \usepackage{mathpazo,courier,euler}
    16 \usepackage{mathpazo,courier,euler}
    17 \usepackage[scaled=.95]{helvet}
    17 \usepackage[scaled=.95]{helvet}
       
    18 \usepackage{amsmath}
    18 \begin{document}
    19 \begin{document}
    19 \date{}
    20 \date{}
    20 \vspace{-1in}
    21 \vspace{-1in}
    21 \begin{center}
    22 \begin{center}
    22 \LARGE{Solving Equations \& ODEs}\\
    23 \LARGE{Solving Equations \& ODEs}\\
    25 \section{Solving linear equations}
    26 \section{Solving linear equations}
    26 Condier following sets of equations:\\
    27 Condier following sets of equations:\\
    27   \begin{align*}
    28   \begin{align*}
    28     3x + 2y - z  & = 1 \\
    29     3x + 2y - z  & = 1 \\
    29     2x - 2y + 4z  & = -2 \\
    30     2x - 2y + 4z  & = -2 \\
    30     -x + $\frac{1}{2}$y -z & = 0
    31     -x + \frac{1}{2}y -z & = 0
    31   \end{align*}\\
    32   \end{align*}\\
    32 The matrix representation is:\\
    33 The matrix representation is:\\
    33 \begin{center}
    34 \begin{center}
    34 $A*x = B$
    35 $A*x = B$
    35 \end{center}
    36 \end{center}
    82 Out[96]: 0.90689968211710881
    83 Out[96]: 0.90689968211710881
    83 \end{lstlisting}
    84 \end{lstlisting}
    84 \subsection{Roots of non-linear eqations}
    85 \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 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{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} 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.\\
    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 %\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 For illustration, we want to find roots of equation:
    90 \begin{center}
    91 \begin{center}
    91   $f(x)=sin(x)+cos(x)^2$
    92   $f(x)=sin(x)+cos(x)^2$
    92 \end{center}
    93 \end{center}
   101 In []: fsolve(f, 0) #arguments are function name and estimate
   102 In []: fsolve(f, 0) #arguments are function name and estimate
   102 Out[]: -0.66623943249251527
   103 Out[]: -0.66623943249251527
   103 \end{lstlisting}
   104 \end{lstlisting}
   104 
   105 
   105 \section{ODE}
   106 \section{ODE}
       
   107 
       
   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.
   106 \begin{lstlisting}
   114 \begin{lstlisting}
   107   In []: def epid(y, t):
   115 In []: def f(y, t):
   108   ....     k, L = 0.00003, 25000
   116   ....     k, L = 0.00003, 25000
   109   ....     return k*y*(L-y)
   117   ....     return k*y*(L-y)
   110   ....
   118   ....
   111   
       
   112   In []: t = arange(0, 12, 0.2)
       
   113 
       
   114   In []: y = odeint(epid, 250, t)
       
   115 
       
   116   In []: plot(t, y)
       
   117 \end{lstlisting}
   119 \end{lstlisting}
       
   120 Next we define the time over which we wish to solve the ODE. We also note the initial conditions given to us.
       
   121 \begin{lstlisting}
       
   122 In []: t = linspace(0, 12, 61)
       
   123 In []: y0 = 250
       
   124 \end{lstlisting}
       
   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.
   118 \end{document}
   130 \end{document}