day1/session6.tex
changeset 382 41c34770d63a
parent 379 682b6f66fe11
child 385 c70118cdde66
equal deleted inserted replaced
381:b797cd67982b 382:41c34770d63a
    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[] {08 March, 2010\\Day 1, Session 6}
    81 \date[] {02 April, 2010\\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 
   237 \end{itemize}
   237 \end{itemize}
   238 \end{frame}
   238 \end{frame}
   239 
   239 
   240 \begin{frame}[fragile]
   240 \begin{frame}[fragile]
   241 \frametitle{\typ{fsolve}}
   241 \frametitle{\typ{fsolve}}
   242 Find the root of $sin(x)+cos^2(x)$ nearest to $0$
   242 Find the root of $sin(z)+cos^2(z)$ nearest to $0$
   243 \vspace{-0.1in}
   243 \vspace{-0.1in}
   244 \begin{center}
   244 \begin{center}
   245 \includegraphics[height=2.8in, interpolate=true]{data/fsolve}    
   245 \includegraphics[height=2.8in, interpolate=true]{data/fsolve}    
   246 \end{center}
   246 \end{center}
   247 \end{frame}
   247 \end{frame}
   248 
   248 
   249 \begin{frame}[fragile]
   249 \begin{frame}[fragile]
   250 \frametitle{\typ{fsolve}}
   250 \frametitle{\typ{fsolve}}
   251 Root of $sin(x)+cos^2(x)$ nearest to $0$
   251 Root of $sin(z)+cos^2(z)$ nearest to $0$
   252 \begin{lstlisting}
   252 \begin{lstlisting}
   253 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   253 In []: fsolve(sin(z)+cos(z)*cos(z), 0)
   254 NameError: name 'x' is not defined
   254 NameError: name 'z' is not defined
   255 \end{lstlisting}
   255 \end{lstlisting}
   256 \end{frame}
   256 \end{frame}
   257 
   257 
   258 \begin{frame}[fragile]
   258 \begin{frame}[fragile]
   259 \frametitle{\typ{fsolve}}
   259 \frametitle{\typ{fsolve}}
   260 \begin{lstlisting}
   260 \begin{lstlisting}
   261 In []: x = linspace(-pi, pi)
   261 In []: z = linspace(-pi, pi)
   262 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   262 In []: fsolve(sin(z)+cos(z)*cos(z), 0)
   263 \end{lstlisting}
   263 \end{lstlisting}
   264 \begin{small}
   264 \begin{small}
   265 \alert{\typ{TypeError:}}
   265 \alert{\typ{TypeError:}}
   266 \typ{'numpy.ndarray' object is not callable}
   266 \typ{'numpy.ndarray' object is not callable}
   267 \end{small}
   267 \end{small}
   269 
   269 
   270 \begin{frame}[fragile]
   270 \begin{frame}[fragile]
   271 \frametitle{Functions - Definition}
   271 \frametitle{Functions - Definition}
   272 We have been using them all along. Now let's see how to define them.
   272 We have been using them all along. Now let's see how to define them.
   273 \begin{lstlisting}
   273 \begin{lstlisting}
   274 In []: def f(x):
   274 In []: def f(z):
   275            return sin(x)+cos(x)*cos(x)
   275            return sin(z)+cos(z)*cos(z)
   276 \end{lstlisting}
   276 \end{lstlisting}
   277 \begin{itemize}
   277 \begin{itemize}
   278 \item \typ{def}
   278 \item \typ{def}
   279 \item name
   279 \item name
   280 \item arguments
   280 \item arguments
   283 \end{frame}
   283 \end{frame}
   284 
   284 
   285 \begin{frame}[fragile]
   285 \begin{frame}[fragile]
   286 \frametitle{Functions - Calling them}
   286 \frametitle{Functions - Calling them}
   287 \begin{lstlisting}
   287 \begin{lstlisting}
   288 In [15]: f()
   288 In []: f()
   289 ---------------------------------------
   289 ---------------------------------------
   290 \end{lstlisting}
   290 \end{lstlisting}
   291 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
   291 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument}
   292 \typ{(0 given)}
   292 \typ{(0 given)}
   293 \begin{lstlisting}
   293 \begin{lstlisting}
   299 More on Functions later \ldots
   299 More on Functions later \ldots
   300 \end{frame}
   300 \end{frame}
   301 
   301 
   302 \begin{frame}[fragile]
   302 \begin{frame}[fragile]
   303 \frametitle{\typ{fsolve} \ldots}
   303 \frametitle{\typ{fsolve} \ldots}
   304 Find the root of $sin(x)+cos^2(x)$ nearest to $0$
   304 Find the root of $sin(z)+cos^2(z)$ nearest to $0$
   305 \begin{lstlisting}
   305 \begin{lstlisting}
   306 In []: fsolve(f, 0)
   306 In []: fsolve(f, 0)
   307 Out[]: -0.66623943249251527
   307 Out[]: -0.66623943249251527
   308 \end{lstlisting}
   308 \end{lstlisting}
   309 \begin{center}
   309 \begin{center}
   311 \end{center}
   311 \end{center}
   312 \end{frame}
   312 \end{frame}
   313 
   313 
   314 \begin{frame}[fragile]
   314 \begin{frame}[fragile]
   315   \frametitle{Exercise Problem}
   315   \frametitle{Exercise Problem}
   316   Find the root of the equation $x^2 - sin(x) + cos^2(x)$ nearest to $0$
   316   Find the root of the equation $x^2 - sin(x) + cos^2(x) == tan(x)$ nearest to $0$
   317 \end{frame}
   317 \end{frame}
   318 
   318 
   319 \begin{frame}[fragile]
   319 \begin{frame}[fragile]
   320   \frametitle{Solution}
   320   \frametitle{Solution}
   321   \begin{small}
   321   \begin{small}
   322   \begin{lstlisting}
   322   \begin{lstlisting}
   323     def f(x):
   323 def f(x):
   324         return x**2 - sin(x) + cos(x)*cos(x)
   324     return x**2 - sin(x) + cos(x)*cos(x) - tan(x)
   325     fsolve(f, 0)
   325 fsolve(f, 0)
   326   \end{lstlisting}
   326   \end{lstlisting}
   327   \end{small}
   327   \end{small}
   328   \begin{center}
   328   \begin{center}
   329 \includegraphics[height=2in, interpolate=true]{data/fsolve_tanx}
   329 \includegraphics[height=2in, interpolate=true]{data/fsolve_tanx}
   330   \end{center}
   330   \end{center}