day1/session6.tex
branchscipy2010
changeset 417 caec361e3a86
parent 389 aa392117454f
equal deleted inserted replaced
412:ca04d463c573 417:caec361e3a86
    76 \title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs}
    76 \title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs}
    77 
    77 
    78 \author[FOSSEE] {FOSSEE}
    78 \author[FOSSEE] {FOSSEE}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {30 April, 2010\\Day 1, Session 6}
    81 \date[] {SciPy 2010, Introductory tutorials\\Day 1, Session 6}
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 
    83 
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    85 %\logo{\pgfuseimage{iitmlogo}}
    85 %\logo{\pgfuseimage{iitmlogo}}
    86 
    86 
   143 
   143 
   144 \begin{frame}[fragile]
   144 \begin{frame}[fragile]
   145 \frametitle{Solving using Matrices}
   145 \frametitle{Solving using Matrices}
   146 Let us now look at how to solve this using \kwrd{matrices}
   146 Let us now look at how to solve this using \kwrd{matrices}
   147   \begin{lstlisting}
   147   \begin{lstlisting}
   148     In []: A = array([[3,2,-1],
   148 In []: A = array([[3,2,-1],
   149                       [2,-2,4],                   
   149                   [2,-2,4],                   
   150                       [-1, 0.5, -1]])
   150                   [-1, 0.5, -1]])
   151     In []: b = array([1, -2, 0])
   151 In []: b = array([1, -2, 0])
   152     In []: x = solve(A, b)
   152 In []: x = solve(A, b)
   153   \end{lstlisting}
   153   \end{lstlisting}
   154 \end{frame}
   154 \end{frame}
   155 
   155 
   156 \begin{frame}[fragile]
   156 \begin{frame}[fragile]
   157 \frametitle{Solution:}
   157 \frametitle{Solution:}
   176 \end{block}
   176 \end{block}
   177 \begin{lstlisting}
   177 \begin{lstlisting}
   178 In []: allclose(Ax, b)
   178 In []: allclose(Ax, b)
   179 Out[]: True
   179 Out[]: True
   180 \end{lstlisting}
   180 \end{lstlisting}
   181 \inctime{15}
   181 \inctime{10}
   182 \end{frame}
   182 \end{frame}
   183 
       
   184 \subsection{Exercises}
       
   185 
   183 
   186 \begin{frame}[fragile]
   184 \begin{frame}[fragile]
   187 \frametitle{Problem}
   185 \frametitle{Problem}
   188 Solve the set of equations:
   186 Solve the set of equations:
   189 \begin{align*}
   187 \begin{align*}
   190   x + y + 2z -w & = 3\\
   188   x + y + 2z -w & = 3\\
   191   2x + 5y - z - 9w & = -3\\
   189   2x + 5y - z - 9w & = -3\\
   192   2x + y -z + 3w & = -11 \\
   190   2x + y -z + 3w & = -11 \\
   193   x - 3y + 2z + 7w & = -5\\
   191   x - 3y + 2z + 7w & = -5\\
   194 \end{align*}
   192 \end{align*}
   195 \inctime{10}
   193 \inctime{5}
   196 \end{frame}
   194 \end{frame}
   197 
   195 
   198 \begin{frame}[fragile]
   196 \begin{frame}[fragile]
   199 \frametitle{Solution}
   197 \frametitle{Solution}
   200 Use \kwrd{solve()}
   198 Use \kwrd{solve()}
   207 \end{frame}
   205 \end{frame}
   208 
   206 
   209 \section{Finding Roots}
   207 \section{Finding Roots}
   210 
   208 
   211 \begin{frame}[fragile]
   209 \begin{frame}[fragile]
   212 \frametitle{Scipy Methods - \typ{roots}}
   210 \frametitle{SciPy: \typ{roots}}
   213 \begin{itemize}
   211 \begin{itemize}
   214 \item Calculates the roots of polynomials
   212 \item Calculates the roots of polynomials
   215 \item To calculate the roots of $x^2-5x+6$ 
   213 \item To calculate the roots of $x^2-5x+6$ 
   216 \end{itemize}
   214 \end{itemize}
   217 \begin{lstlisting}
   215 \begin{lstlisting}
   224 \includegraphics[height=1.6in, interpolate=true]{data/roots}    
   222 \includegraphics[height=1.6in, interpolate=true]{data/roots}    
   225 \end{center}
   223 \end{center}
   226 \end{frame}
   224 \end{frame}
   227 
   225 
   228 \begin{frame}[fragile]
   226 \begin{frame}[fragile]
   229 \frametitle{Scipy Methods - \typ{fsolve}}
   227 \frametitle{SciPy: \typ{fsolve}}
   230 \begin{small}
   228 \begin{small}
   231 \begin{lstlisting}
   229 \begin{lstlisting}
   232   In []: from scipy.optimize import fsolve
   230   In []: from scipy.optimize import fsolve
   233 \end{lstlisting}
   231 \end{lstlisting}
   234 \end{small}
   232 \end{small}
   275 \begin{lstlisting}
   273 \begin{lstlisting}
   276 In []: def g(z):
   274 In []: def g(z):
   277  ....:     return sin(z)+cos(z)*cos(z)
   275  ....:     return sin(z)+cos(z)*cos(z)
   278 \end{lstlisting}
   276 \end{lstlisting}
   279 \begin{itemize}
   277 \begin{itemize}
   280 \item \typ{def}
   278 \item \typ{def} -- keyword
   281 \item name
   279 \item name: \typ{g}
   282 \item arguments
   280 \item arguments: \typ{z}
   283 \item \typ{return}
   281 \item \typ{return} -- keyword
   284 \end{itemize}
   282 \end{itemize}
   285 \end{frame}
   283 \end{frame}
   286 
   284 
   287 \begin{frame}[fragile]
   285 \begin{frame}[fragile]
   288 \frametitle{Functions - Calling them}
   286 \frametitle{Functions - Calling them}
   348 \section{ODEs}
   346 \section{ODEs}
   349 
   347 
   350 \begin{frame}[fragile]
   348 \begin{frame}[fragile]
   351 \frametitle{Solving ODEs using SciPy}
   349 \frametitle{Solving ODEs using SciPy}
   352 \begin{itemize}
   350 \begin{itemize}
   353 \item Let's consider the spread of an epidemic in a population
   351 \item Consider the spread of an epidemic in a population
   354 \item $\frac{dy}{dt} = ky(L-y)$ gives the spread of the disease
   352 \item $\frac{dy}{dt} = ky(L-y)$ gives the spread of the disease
   355 \item L is the total population.
   353 \item $L$ is the total population.
   356 \item Use L = 250000, k = 0.00003, y(0) = 250
   354 \item Use $L = 2.5E5, k = 3E-5, y(0) = 250$
   357 \item Define a function as below
   355 \item Define a function as below
   358 \end{itemize}
   356 \end{itemize}
   359 \begin{lstlisting}
   357 \begin{lstlisting}
   360 In []: from scipy.integrate import odeint
   358 In []: from scipy.integrate import odeint
   361 In []: def epid(y, t):
   359 In []: def epid(y, t):
   362   ....     k = 0.00003
   360   ....     k = 3.0e-5
   363   ....     L = 250000
   361   ....     L = 2.5e5
   364   ....     return k*y*(L-y)
   362   ....     return k*y*(L-y)
   365   ....
   363   ....
   366 \end{lstlisting}
   364 \end{lstlisting}
   367 \end{frame}
   365 \end{frame}
   368 
   366 
   448 \begin{center}
   446 \begin{center}
   449 \includegraphics[height=2in, interpolate=true]{data/ode}  
   447 \includegraphics[height=2in, interpolate=true]{data/ode}  
   450 \end{center}
   448 \end{center}
   451 \end{frame}
   449 \end{frame}
   452 
   450 
       
   451 \section{FFTs}
       
   452 
       
   453 \begin{frame}[fragile]
       
   454 \frametitle{The FFT}
       
   455 \begin{itemize}
       
   456     \item We have a simple signal $y(t)$
       
   457     \item Find the FFT and plot it
       
   458 \end{itemize}
       
   459 \begin{lstlisting}
       
   460 In []: t = linspace(0, 2*pi, 500)
       
   461 In []: y = sin(4*pi*t)
       
   462 
       
   463 In []: f = fft(y)
       
   464 In []: freq = fftfreq(500, t[1] - t[0])
       
   465 
       
   466 In []: plot(freq[:250], abs(f)[:250])
       
   467 In []: grid()
       
   468 \end{lstlisting} 
       
   469 \end{frame}
       
   470 
       
   471 \begin{frame}[fragile]
       
   472 \frametitle{FFTs cont\dots}
       
   473 \begin{lstlisting}
       
   474 In []: y1 = ifft(f) # inverse FFT
       
   475 In []: allclose(y, y1)
       
   476 Out[]: True
       
   477 \end{lstlisting} 
       
   478 \end{frame}
       
   479 
       
   480 \begin{frame}[fragile]
       
   481 \frametitle{FFTs cont\dots}
       
   482 Let us add some noise to the signal
       
   483 \begin{lstlisting}
       
   484 In []: yr = y + random(size=500)*0.2
       
   485 In []: yn = y + normal(size=500)*0.2
       
   486 
       
   487 In []: plot(t, yr)
       
   488 In []: figure()
       
   489 In []: plot(freq[:250],
       
   490   ...:      abs(fft(yn))[:250])
       
   491 \end{lstlisting}
       
   492 \begin{itemize}
       
   493     \item \typ{random}: produces uniform deviates in $[0, 1)$
       
   494     \item \typ{normal}: draws random samples from a Gaussian
       
   495         distribution
       
   496     \item Useful to create a random matrix of any shape
       
   497 \end{itemize}
       
   498 \end{frame}
       
   499 
       
   500 \begin{frame}[fragile]
       
   501 \frametitle{FFTs cont\dots}
       
   502 Filter the noisy signal:
       
   503 \begin{lstlisting}
       
   504 In []: from scipy import signal
       
   505 In []: yc = signal.wiener(yn, 5)
       
   506 In []: clf()
       
   507 In []: plot(t, yc)
       
   508 In []: figure()
       
   509 In []: plot(freq[:250], 
       
   510   ...:      abs(fft(yc))[:250])
       
   511 \end{lstlisting}
       
   512 Only scratched the surface here \dots
       
   513 \end{frame}
       
   514 
       
   515 
   453 \begin{frame}
   516 \begin{frame}
   454   \frametitle{Things we have learned}
   517   \frametitle{Things we have learned}
   455   \begin{itemize}
   518   \begin{itemize}
   456   \item Solving Linear Equations
   519   \item Solving Linear Equations
   457   \item Defining Functions
   520   \item Defining Functions
   458   \item Finding Roots
   521   \item Finding Roots
   459   \item Solving ODEs
   522   \item Solving ODEs
       
   523   \item Random number generation
       
   524   \item FFTs and basic signal processing
   460   \end{itemize}
   525   \end{itemize}
   461 \end{frame}
   526 \end{frame}
   462 
   527 
   463 \end{document}
   528 \end{document}
   464 
   529