day1/session6.tex
changeset 270 0ad8566e98ff
parent 266 28d4714a9702
child 271 3f32f679bb45
equal deleted inserted replaced
269:9c9be698d7ad 270:0ad8566e98ff
   224 %%  ....:   
   224 %%  ....:   
   225 %% In []: x0
   225 %% In []: x0
   226 %% \end{lstlisting}
   226 %% \end{lstlisting}
   227 %% \end{frame}
   227 %% \end{frame}
   228 
   228 
   229 \begin{frame}[fragile]
   229 %% \begin{frame}[fragile]
   230 \frametitle{Bisection Method}
   230 %% \frametitle{Bisection Method}
   231 \begin{enumerate}
   231 %% \begin{enumerate}
   232 \item Start with the given interval $(-\pi/2, \pi/2)$ ($(a, b)$)
   232 %% \item Start with the given interval $(-\pi/2, \pi/2)$ ($(a, b)$)
   233 \item $f(a)\cdot f(b) < 0$
   233 %% \item $f(a)\cdot f(b) < 0$
   234 \item Bisect the interval. $c = \frac{a+b}{2}$
   234 %% \item Bisect the interval. $c = \frac{a+b}{2}$
   235 \item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$
   235 %% \item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$
   236 \item Else, change the interval to $(c, b)$
   236 %% \item Else, change the interval to $(c, b)$
   237 \item Go back to 3 until $(b-a) \le$ tolerance
   237 %% \item Go back to 3 until $(b-a) \le$ tolerance
   238 \end{enumerate}
   238 %% \end{enumerate}
   239 \alert{\typ{tolerance = 1e-5}}
   239 %% \alert{\typ{tolerance = 1e-5}}
   240 \end{frame}
   240 %% \end{frame}
   241 
   241 
   242 %% \begin{frame}[fragile]
   242 %% \begin{frame}[fragile]
   243 %% \frametitle{Bisection \dots}
   243 %% \frametitle{Bisection \dots}
   244 %% \begin{lstlisting}
   244 %% \begin{lstlisting}
   245 %% In []: tolerance = 1e-5
   245 %% In []: tolerance = 1e-5
   253 %%  ....:         a = c
   253 %%  ....:         a = c
   254 %%  ....:         
   254 %%  ....:         
   255 %% \end{lstlisting}
   255 %% \end{lstlisting}
   256 %% \end{frame}
   256 %% \end{frame}
   257 
   257 
   258 \begin{frame}[fragile]
   258 %% \begin{frame}[fragile]
   259 \frametitle{Newton-Raphson Method}
   259 %% \frametitle{Newton-Raphson Method}
   260 \begin{enumerate}
   260 %% \begin{enumerate}
   261 \item Start with an initial guess of x for the root
   261 %% \item Start with an initial guess of x for the root
   262 \item $\Delta x = -f(x)/f^{'}(x)$
   262 %% \item $\Delta x = -f(x)/f^{'}(x)$
   263 \item $ x \leftarrow x + \Delta x$
   263 %% \item $ x \leftarrow x + \Delta x$
   264 \item Go back to 2 until $|\Delta x| \le$ tolerance
   264 %% \item Go back to 2 until $|\Delta x| \le$ tolerance
   265 \end{enumerate}
   265 %% \end{enumerate}
   266 \end{frame}
   266 %% \end{frame}
   267 
   267 
   268 %% \begin{frame}[fragile]
   268 %% \begin{frame}[fragile]
   269 %% \frametitle{Newton-Raphson \dots}
   269 %% \frametitle{Newton-Raphson \dots}
   270 %% \begin{lstlisting}
   270 %% \begin{lstlisting}
   271 %% In []: def our_df(x):
   271 %% In []: def our_df(x):
   288 %% \item What if the differential is not easy to calculate?
   288 %% \item What if the differential is not easy to calculate?
   289 %% \item Look at Secant Method
   289 %% \item Look at Secant Method
   290 %% \end{itemize}
   290 %% \end{itemize}
   291 %% \end{frame}
   291 %% \end{frame}
   292 
   292 
   293 \begin{frame}[fragile]
   293 %% \begin{frame}[fragile]
   294 \frametitle{Initial Estimates}
   294 %% \frametitle{Initial Estimates}
   295 \begin{itemize}
   295 %% \begin{itemize}
   296 \item Given an interval
   296 %% \item Given an interval
   297 \item How to find \alert{all} the roots?
   297 %% \item How to find \alert{all} the roots?
   298 \end{itemize}
   298 %% \end{itemize}
   299 \begin{enumerate}
   299 %% \begin{enumerate}
   300 \item Check for change of signs of $f(x)$ in the given interval
   300 %% \item Check for change of signs of $f(x)$ in the given interval
   301 \item A root lies in the interval where the sign change occurs
   301 %% \item A root lies in the interval where the sign change occurs
   302 \end{enumerate}
   302 %% \end{enumerate}
   303 \end{frame}
   303 %% \end{frame}
   304 
   304 
   305 \begin{frame}[fragile]
   305 %% \begin{frame}[fragile]
   306 \frametitle{Initial Estimates \ldots}
   306 %% \frametitle{Initial Estimates \ldots}
   307 \begin{lstlisting}
   307 %% \begin{lstlisting}
   308 In []: def our_f(x):
   308 %% In []: def our_f(x):
   309  ....:     return cos(x) - x*x
   309 %%  ....:     return cos(x) - x*x
   310  ....: 
   310 %%  ....: 
   311 In []: x = linspace(-pi/2, pi/2, 11)
   311 %% In []: x = linspace(-pi/2, pi/2, 11)
   312 In []: y = our_f(x)
   312 %% In []: y = our_f(x)
   313 \end{lstlisting}
   313 %% \end{lstlisting}
   314 Get the intervals of x, where sign changes occur
   314 %% Get the intervals of x, where sign changes occur
   315 \end{frame}
   315 %% \end{frame}
   316 
   316 
   317 \begin{frame}[fragile]
   317 %% \begin{frame}[fragile]
   318 \frametitle{Initial Estimates \ldots}
   318 %% \frametitle{Initial Estimates \ldots}
   319 \begin{lstlisting}
   319 %% \begin{lstlisting}
   320 In []: pos = y[:-1]*y[1:] < 0
   320 %% In []: pos = y[:-1]*y[1:] < 0
   321 In []: rpos = zeros(x.shape, dtype=bool)
   321 %% In []: rpos = zeros(x.shape, dtype=bool)
   322 In []: rpos[:-1] = pos
   322 %% In []: rpos[:-1] = pos
   323 In []: rpos[1:] += pos
   323 %% In []: rpos[1:] += pos
   324 In []: rts = x[rpos]
   324 %% In []: rts = x[rpos]
   325 \end{lstlisting}
   325 %% \end{lstlisting}
   326 Now use Newton-Raphson?
   326 %% Now use Newton-Raphson?
   327 \end{frame}
   327 %% \end{frame}
   328 
   328 
   329 
   329 
   330 \begin{frame}[fragile]
   330 \begin{frame}[fragile]
   331 \frametitle{Scipy Methods - \typ{roots}}
   331 \frametitle{Scipy Methods - \typ{roots}}
   332 \begin{itemize}
   332 \begin{itemize}
   353 \begin{lstlisting}
   353 \begin{lstlisting}
   354   In []: fsolve(our_f, -pi/2)
   354   In []: fsolve(our_f, -pi/2)
   355 \end{lstlisting}
   355 \end{lstlisting}
   356 \end{frame}
   356 \end{frame}
   357 
   357 
   358 \begin{frame}[fragile]
   358 %% \begin{frame}[fragile]
   359 \frametitle{Scipy Methods \dots}
   359 %% \frametitle{Scipy Methods \dots}
   360 \begin{small}
   360 %% \begin{small}
   361 \begin{lstlisting}
   361 %% \begin{lstlisting}
   362 In []: from scipy.optimize import fixed_point
   362 %% In []: from scipy.optimize import fixed_point
   363 
   363 
   364 In []: from scipy.optimize import bisect
   364 %% In []: from scipy.optimize import bisect
   365 
   365 
   366 In []: from scipy.optimize import newton
   366 %% In []: from scipy.optimize import newton
   367 \end{lstlisting}
   367 %% \end{lstlisting}
   368 \end{small}
   368 %% \end{small}
   369 \end{frame}
   369 %% \end{frame}
   370 
   370 
   371 \begin{frame}
   371 \begin{frame}
   372   \frametitle{Things we have learned}
   372   \frametitle{Things we have learned}
   373   \begin{itemize}
   373   \begin{itemize}
   374   \item Solving ODEs
   374   \item Solving ODEs
   375   \item Finding Roots
   375   \item Finding Roots
   376     \begin{itemize}
       
   377     \item Estimating Interval
       
   378     \item Newton-Raphson
       
   379     \item Scipy methods
       
   380     \end{itemize}
       
   381   \end{itemize}
   376   \end{itemize}
   382 \end{frame}
   377 \end{frame}
   383 
   378 
   384 \end{document}
   379 \end{document}