day1/session4.tex
changeset 172 72bd110ab42f
parent 141 fccf675946bf
child 178 8a3a9d98fa84
equal deleted inserted replaced
169:bb3739afce10 172:72bd110ab42f
   208     In []: allclose(Ax, b)
   208     In []: allclose(Ax, b)
   209     Out[]: True
   209     Out[]: True
   210   \end{lstlisting}
   210   \end{lstlisting}
   211 \end{frame}
   211 \end{frame}
   212 
   212 
       
   213 
   213 \section{Integration}
   214 \section{Integration}
       
   215 
       
   216 \subsection{Quadrature}
       
   217 
       
   218 \begin{frame}[fragile]
       
   219 \frametitle{Quadrature}
       
   220 \begin{itemize}
       
   221 \item We wish to find area under a curve
       
   222 \item Area under $(sin(x) + x^2)$ in $(0,1)$
       
   223 \item scipy has functions to do that
       
   224 \end{itemize}
       
   225 \small{\typ{In []: from scipy.integrate import quad}}
       
   226 \begin{itemize}
       
   227 \item Inputs - function to integrate, limits
       
   228 \end{itemize}
       
   229 \begin{lstlisting}
       
   230 In []: x = 0
       
   231 In []: integrate.quad(sin(x)+x**2, 0, 1)
       
   232 \end{lstlisting}
       
   233 \alert{\typ{error:}}
       
   234 \typ{First argument must be a callable function.}
       
   235 \end{frame}
       
   236 
       
   237 \begin{frame}[fragile]
       
   238 \frametitle{Functions - Definition}
       
   239 \begin{lstlisting}
       
   240 In []: def f(x):
       
   241            return sin(x)+x**2
       
   242 In []: integrate.quad(f, 0, 1)
       
   243 \end{lstlisting}
       
   244 \begin{itemize}
       
   245 \item \typ{def}
       
   246 \item arguments
       
   247 \item \typ{return}
       
   248 \end{itemize}
       
   249 \end{frame}
       
   250 
       
   251 \begin{frame}[fragile]
       
   252   \frametitle{More on functions}
       
   253   \begin{itemize}
       
   254   \item Support default and keyword arguments
       
   255   \item Scope of variables in the function is local
       
   256   \item Mutable items are \alert{passed by reference}
       
   257   \item First line after definition may be a documentation string
       
   258     (\alert{recommended!})
       
   259   \item Function definition and execution defines a name bound to the
       
   260     function
       
   261   \item You \emph{can} assign a variable to a function!
       
   262   \end{itemize}
       
   263 \end{frame}
       
   264 
   214 
   265 
   215 \subsection{ODEs}
   266 \subsection{ODEs}
   216 
   267 
   217 \begin{frame}[fragile]
   268 \begin{frame}[fragile]
   218 \frametitle{ODE Integration}
   269 \frametitle{ODE Integration}
   270                          initial,t, 
   321                          initial,t, 
   271                          args=(p,))
   322                          args=(p,))
   272 \end{lstlisting}
   323 \end{lstlisting}
   273 \end{frame}
   324 \end{frame}
   274 
   325 
   275 \subsection{Quadrature}
       
   276 
       
   277 \begin{frame}[fragile]
       
   278 \frametitle{Quadrature}
       
   279 Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$
       
   280 \small{\typ{In []: from scipy.integrate import quad}}
       
   281   \begin{lstlisting}
       
   282 In []: f(x):
       
   283         return sin(x)+x**2
       
   284 In []: integrate.quad(f, 0, 1)
       
   285   \end{lstlisting}
       
   286 \end{frame}
       
   287 
       
   288 \end{document}
   326 \end{document}
   289 
   327