day1/session6.tex
changeset 365 2cda9b04f142
parent 359 cb17c87b090e
child 366 ec4cb3ba7f09
equal deleted inserted replaced
364:856470d9a952 365:2cda9b04f142
   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:}
   158 \begin{lstlisting}
   158 \begin{lstlisting}
   159 In []: x
   159 In []: x
   160 Out[]: 
   160 Out[]: array([ 1., -2., -2.])
   161 array([[ 1.],
       
   162        [-2.],
       
   163        [-2.]])
       
   164 \end{lstlisting}
   161 \end{lstlisting}
   165 \end{frame}
   162 \end{frame}
   166 
   163 
   167 \begin{frame}[fragile]
   164 \begin{frame}[fragile]
   168 \frametitle{Let's check!}
   165 \frametitle{Let's check!}
   169 \begin{lstlisting}
   166 \begin{lstlisting}
   170 In []: Ax = dot(A,x)
   167 In []: Ax = dot(A, x)
   171 In []: Ax
   168 In []: Ax
   172 Out[]: 
   169 Out[]: array([  1.00000000e+00,  -2.00000000e+00,  -1.11022302e-16])
   173 array([[  1.00000000e+00],
       
   174        [ -2.00000000e+00],
       
   175        [  2.22044605e-16]])
       
   176 \end{lstlisting}
   170 \end{lstlisting}
   177 \begin{block}{}
   171 \begin{block}{}
   178 The last term in the matrix is actually \alert{0}!\\
   172 The last term in the matrix is actually \alert{0}!\\
   179 We can use \kwrd{allclose()} to check.
   173 We can use \kwrd{allclose()} to check.
   180 \end{block}
   174 \end{block}
   244 \end{frame}
   238 \end{frame}
   245 
   239 
   246 \begin{frame}[fragile]
   240 \begin{frame}[fragile]
   247 \frametitle{\typ{fsolve}}
   241 \frametitle{\typ{fsolve}}
   248 Find the root of $sin(x)+cos^2(x)$ nearest to $0$
   242 Find the root of $sin(x)+cos^2(x)$ nearest to $0$
   249 \begin{lstlisting}
   243 \vspace{-0.1in}
   250 In []: fsolve(sin(x)+cos(x)**2, 0)
   244 \begin{center}
       
   245 \includegraphics[height=2.8in, interpolate=true]{data/fsolve}    
       
   246 \end{center}
       
   247 \end{frame}
       
   248 
       
   249 \begin{frame}[fragile]
       
   250 \frametitle{\typ{fsolve}}
       
   251 Root of $sin(x)+cos^2(x)$ nearest to $0$
       
   252 \begin{lstlisting}
       
   253 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   251 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}
   252 In []: x = linspace(-pi, pi)
   261 In []: x = linspace(-pi, pi)
   253 In []: fsolve(sin(x)+cos(x)**2, 0)
   262 In []: fsolve(sin(x)+cos(x)*cos(x), 0)
   254 \end{lstlisting}
   263 \end{lstlisting}
   255 \begin{small}
   264 \begin{small}
   256 \alert{\typ{TypeError:}}
   265 \alert{\typ{TypeError:}}
   257 \typ{'numpy.ndarray' object is not callable}
   266 \typ{'numpy.ndarray' object is not callable}
   258 \end{small}
   267 \end{small}
   261 \begin{frame}[fragile]
   270 \begin{frame}[fragile]
   262 \frametitle{Functions - Definition}
   271 \frametitle{Functions - Definition}
   263 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.
   264 \begin{lstlisting}
   273 \begin{lstlisting}
   265 In []: def f(x):
   274 In []: def f(x):
   266            return sin(x)+cos(x)**2
   275            return sin(x)+cos(x)*cos(x)
   267 \end{lstlisting}
   276 \end{lstlisting}
   268 \begin{itemize}
   277 \begin{itemize}
   269 \item \typ{def}
   278 \item \typ{def}
   270 \item name
   279 \item name
   271 \item arguments
   280 \item arguments
   327 \item Define a function as below
   336 \item Define a function as below
   328 \end{itemize}
   337 \end{itemize}
   329 \begin{lstlisting}
   338 \begin{lstlisting}
   330 In []: from scipy.integrate import odeint
   339 In []: from scipy.integrate import odeint
   331 In []: def epid(y, t):
   340 In []: def epid(y, t):
   332   ....     k, L = 0.00003, 25000
   341   ....     k = 0.00003
       
   342   ....     L = 25000
   333   ....     return k*y*(L-y)
   343   ....     return k*y*(L-y)
   334   ....
   344   ....
   335 \end{lstlisting}
   345 \end{lstlisting}
   336 \end{frame}
   346 \end{frame}
   337 
   347 
   377 \begin{itemize}
   387 \begin{itemize}
   378 \item Use \typ{odeint} to do the integration
   388 \item Use \typ{odeint} to do the integration
   379 \end{itemize}
   389 \end{itemize}
   380 \begin{lstlisting}
   390 \begin{lstlisting}
   381 In []: def pend_int(initial, t):
   391 In []: def pend_int(initial, t):
   382   ....     theta, omega = initial
   392   ....     theta = initial[0]
   383   ....     g, L = 9.81, 0.2
   393   ....     omega = initial[1]
       
   394   ....     g = 9.81
       
   395   ....     L = 0.2
   384   ....     f=[omega, -(g/L)*sin(theta)]
   396   ....     f=[omega, -(g/L)*sin(theta)]
   385   ....     return f
   397   ....     return f
   386   ....
   398   ....
   387 \end{lstlisting}
   399 \end{lstlisting}
   388 \end{frame}
   400 \end{frame}
   392 \begin{itemize}
   404 \begin{itemize}
   393 \item \typ{t} is the time variable \\ 
   405 \item \typ{t} is the time variable \\ 
   394 \item \typ{initial} has the initial values
   406 \item \typ{initial} has the initial values
   395 \end{itemize}
   407 \end{itemize}
   396 \begin{lstlisting}
   408 \begin{lstlisting}
   397 In []: t = linspace(0, 10, 101)
   409 In []: t = linspace(0, 20, 101)
   398 In []: initial = [10*2*pi/360, 0]
   410 In []: initial = [10*2*pi/360, 0]
   399 \end{lstlisting} 
   411 \end{lstlisting} 
   400 \end{frame}
   412 \end{frame}
   401 
   413 
   402 \begin{frame}[fragile]
   414 \begin{frame}[fragile]