day1/session6.tex
changeset 359 cb17c87b090e
parent 358 162e3e453920
child 366 ec4cb3ba7f09
equal deleted inserted replaced
358:162e3e453920 359:cb17c87b090e
   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(x)+cos^2(x)$ nearest to $0$
   252 \begin{lstlisting}
   252 \begin{lstlisting}
   253 In []: fsolve(sin(x)+cos(x)**2, 0)
   253 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   254 NameError: name 'x' is not defined
   254 NameError: name 'x' is not defined
       
   255 \end{lstlisting}
       
   256 \end{frame}
       
   257 
       
   258 \begin{frame}[fragile]
       
   259 \frametitle{\typ{fsolve}}
       
   260 \begin{lstlisting}
   255 In []: x = linspace(-pi, pi)
   261 In []: x = linspace(-pi, pi)
   256 In []: fsolve(sin(x)+cos(x)**2, 0)
   262 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   257 \end{lstlisting}
   263 \end{lstlisting}
   258 \begin{small}
   264 \begin{small}
   259 \alert{\typ{TypeError:}}
   265 \alert{\typ{TypeError:}}
   260 \typ{'numpy.ndarray' object is not callable}
   266 \typ{'numpy.ndarray' object is not callable}
   261 \end{small}
   267 \end{small}
   264 \begin{frame}[fragile]
   270 \begin{frame}[fragile]
   265 \frametitle{Functions - Definition}
   271 \frametitle{Functions - Definition}
   266 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.
   267 \begin{lstlisting}
   273 \begin{lstlisting}
   268 In []: def f(x):
   274 In []: def f(x):
   269            return sin(x)+cos(x)**2
   275            return sin(x)+cos(x)*cos(x)
   270 \end{lstlisting}
   276 \end{lstlisting}
   271 \begin{itemize}
   277 \begin{itemize}
   272 \item \typ{def}
   278 \item \typ{def}
   273 \item name
   279 \item name
   274 \item arguments
   280 \item arguments
   330 \item Define a function as below
   336 \item Define a function as below
   331 \end{itemize}
   337 \end{itemize}
   332 \begin{lstlisting}
   338 \begin{lstlisting}
   333 In []: from scipy.integrate import odeint
   339 In []: from scipy.integrate import odeint
   334 In []: def epid(y, t):
   340 In []: def epid(y, t):
   335   ....     k, L = 0.00003, 25000
   341   ....     k = 0.00003
       
   342   ....     L = 25000
   336   ....     return k*y*(L-y)
   343   ....     return k*y*(L-y)
   337   ....
   344   ....
   338 \end{lstlisting}
   345 \end{lstlisting}
   339 \end{frame}
   346 \end{frame}
   340 
   347 
   380 \begin{itemize}
   387 \begin{itemize}
   381 \item Use \typ{odeint} to do the integration
   388 \item Use \typ{odeint} to do the integration
   382 \end{itemize}
   389 \end{itemize}
   383 \begin{lstlisting}
   390 \begin{lstlisting}
   384 In []: def pend_int(initial, t):
   391 In []: def pend_int(initial, t):
   385   ....     theta, omega = initial
   392   ....     theta = initial[0]
   386   ....     g, L = 9.81, 0.2
   393   ....     omega = initial[1]
       
   394   ....     g = 9.81
       
   395   ....     L = 0.2
   387   ....     f=[omega, -(g/L)*sin(theta)]
   396   ....     f=[omega, -(g/L)*sin(theta)]
   388   ....     return f
   397   ....     return f
   389   ....
   398   ....
   390 \end{lstlisting}
   399 \end{lstlisting}
   391 \end{frame}
   400 \end{frame}
   395 \begin{itemize}
   404 \begin{itemize}
   396 \item \typ{t} is the time variable \\ 
   405 \item \typ{t} is the time variable \\ 
   397 \item \typ{initial} has the initial values
   406 \item \typ{initial} has the initial values
   398 \end{itemize}
   407 \end{itemize}
   399 \begin{lstlisting}
   408 \begin{lstlisting}
   400 In []: t = linspace(0, 10, 101)
   409 In []: t = linspace(0, 20, 101)
   401 In []: initial = [10*2*pi/360, 0]
   410 In []: initial = [10*2*pi/360, 0]
   402 \end{lstlisting} 
   411 \end{lstlisting} 
   403 \end{frame}
   412 \end{frame}
   404 
   413 
   405 \begin{frame}[fragile]
   414 \begin{frame}[fragile]