194 \item Presently, let's look at real roots |
194 \item Presently, let's look at real roots |
195 \end{itemize} |
195 \end{itemize} |
196 \end{frame} |
196 \end{frame} |
197 |
197 |
198 \begin{frame}[fragile] |
198 \begin{frame}[fragile] |
199 \frametitle{Initial Estimates} |
199 \frametitle{Roots of $f(x)=0$} |
200 \begin{itemize} |
200 \begin{itemize} |
201 \item Find roots of $cosx-x^2$ in $(-\pi/2, \pi/2)$ |
201 \item Given function $cosx-x^2$ |
202 \item How to get a rough initial estimate? |
202 \item First we find \alert{a} root in $(-\pi/2, \pi/2)$ |
203 \end{itemize} |
203 \item Then we find \alert{all} roots in $(-\pi/2, \pi/2)$ |
204 \begin{enumerate} |
204 \end{itemize} |
205 \item Check for change of signs of $f(x)$ in the given interval |
205 \end{frame} |
206 \item A root lies in the interval where the sign change occurs |
206 |
207 \end{enumerate} |
207 %% \begin{frame}[fragile] |
208 \end{frame} |
208 %% \frametitle{Fixed Point Method} |
209 |
209 %% \begin{enumerate} |
210 \begin{frame}[fragile] |
210 %% \item Convert $f(x)=0$ to the form $x=g(x)$ |
211 \frametitle{Initial Estimates \ldots} |
211 %% \item Start with an initial value of $x$ and iterate successively |
212 \begin{lstlisting} |
212 %% \item $x_{n+1}=g(x_n)$ and $x_0$ is our initial guess |
213 In []: def our_f(x): |
213 %% \item Iterate until $x_{n+1}-x_n \le tolerance$ |
214 ....: return cos(x)-x**2 |
214 %% \end{enumerate} |
215 ....: |
215 %% \end{frame} |
216 In []: x = linspace(-pi/2, pi/2, 11) |
|
217 \end{lstlisting} |
|
218 \begin{itemize} |
|
219 \item Get the intervals of x, where sign changes occur |
|
220 \end{itemize} |
|
221 \end{frame} |
|
222 |
|
223 %% \begin{frame}[fragile] |
|
224 %% \frametitle{Initial Estimates \ldots} |
|
225 %% \begin{lstlisting} |
|
226 %% In []: pos = y[:-1]*y[1:] < 0 |
|
227 %% In []: rpos = zeros(x.shape, dtype=bool) |
|
228 %% In []: rpos[:-1] = pos |
|
229 %% In []: rpos[1:] += pos |
|
230 %% In []: rts = x[rpos] |
|
231 %% \end{lstlisting} |
|
232 %% \end{frame} |
|
233 |
|
234 \begin{frame}[fragile] |
|
235 \frametitle{Fixed Point Method} |
|
236 \begin{enumerate} |
|
237 \item Convert $f(x)=0$ to the form $x=g(x)$ |
|
238 \item Start with an initial value of $x$ and iterate successively |
|
239 \item $x_{n+1}=g(x_n)$ and $x_0$ is our initial guess |
|
240 \item Iterate until $x_{n+1}-x_n \le tolerance$ |
|
241 \end{enumerate} |
|
242 \end{frame} |
|
243 |
216 |
244 %% \begin{frame}[fragile] |
217 %% \begin{frame}[fragile] |
245 %% \frametitle{Fixed Point \dots} |
218 %% \frametitle{Fixed Point \dots} |
246 %% \begin{lstlisting} |
219 %% \begin{lstlisting} |
247 %% In []: def our_g(x): |
220 %% In []: def our_g(x): |
257 %% \end{frame} |
230 %% \end{frame} |
258 |
231 |
259 \begin{frame}[fragile] |
232 \begin{frame}[fragile] |
260 \frametitle{Bisection Method} |
233 \frametitle{Bisection Method} |
261 \begin{enumerate} |
234 \begin{enumerate} |
262 \item Start with an interval $(a, b)$ within which a root exists |
235 \item Start with the given interval $(-\pi/2, \pi/2)$ ($(a, b)$) |
263 \item $f(a)\cdot f(b) < 0$ |
236 \item $f(a)\cdot f(b) < 0$ |
264 \item Bisect the interval. $c = \frac{a+b}{2}$ |
237 \item Bisect the interval. $c = \frac{a+b}{2}$ |
265 \item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$ |
238 \item Change the interval to $(a, c)$ if $f(a)\cdot f(c) < 0$ |
266 \item Else, change the interval to $(c, b)$ |
239 \item Else, change the interval to $(c, b)$ |
267 \item Go back to 3 until $(b-a) \le$ tolerance |
240 \item Go back to 3 until $(b-a) \le$ tolerance |
268 \end{enumerate} |
241 \end{enumerate} |
|
242 \alert{\typ{tolerance = 1e-5}} |
269 \end{frame} |
243 \end{frame} |
270 |
244 |
271 %% \begin{frame}[fragile] |
245 %% \begin{frame}[fragile] |
272 %% \frametitle{Bisection \dots} |
246 %% \frametitle{Bisection \dots} |
273 %% \begin{lstlisting} |
247 %% \begin{lstlisting} |
307 %% ....: |
281 %% ....: |
308 %% ....: |
282 %% ....: |
309 %% \end{lstlisting} |
283 %% \end{lstlisting} |
310 %% \end{frame} |
284 %% \end{frame} |
311 |
285 |
312 \begin{frame}[fragile] |
286 %% \begin{frame}[fragile] |
313 \frametitle{Newton-Raphson \ldots} |
287 %% \frametitle{Newton-Raphson \ldots} |
314 \begin{itemize} |
288 %% \begin{itemize} |
315 \item What if $f^{'}(x) = 0$? |
289 %% \item What if $f^{'}(x) = 0$? |
316 \item Can you write a better version of the Newton-Raphson? |
290 %% \item Can you write a better version of the Newton-Raphson? |
317 \item What if the differential is not easy to calculate? |
291 %% \item What if the differential is not easy to calculate? |
318 \item Look at Secant Method |
292 %% \item Look at Secant Method |
319 \end{itemize} |
293 %% \end{itemize} |
320 \end{frame} |
294 %% \end{frame} |
|
295 |
|
296 \begin{frame}[fragile] |
|
297 \frametitle{Initial Estimates} |
|
298 \begin{itemize} |
|
299 \item Given an interval |
|
300 \item How to find \alert{all} the roots? |
|
301 \end{itemize} |
|
302 \begin{enumerate} |
|
303 \item Check for change of signs of $f(x)$ in the given interval |
|
304 \item A root lies in the interval where the sign change occurs |
|
305 \end{enumerate} |
|
306 \end{frame} |
|
307 |
|
308 \begin{frame}[fragile] |
|
309 \frametitle{Initial Estimates \ldots} |
|
310 \begin{lstlisting} |
|
311 In []: def our_f(x): |
|
312 ....: return cos(x)-x**2 |
|
313 ....: |
|
314 In []: x = linspace(-pi/2, pi/2, 11) |
|
315 \end{lstlisting} |
|
316 \begin{itemize} |
|
317 \item Get the intervals of x, where sign changes occur |
|
318 \end{itemize} |
|
319 \end{frame} |
|
320 |
|
321 \begin{frame}[fragile] |
|
322 \frametitle{Initial Estimates \ldots} |
|
323 \begin{lstlisting} |
|
324 In []: pos = y[:-1]*y[1:] < 0 |
|
325 In []: rpos = zeros(x.shape, dtype=bool) |
|
326 In []: rpos[:-1] = pos |
|
327 In []: rpos[1:] += pos |
|
328 In []: rts = x[rpos] |
|
329 \end{lstlisting} |
|
330 Now use Newton-Raphson? |
|
331 \end{frame} |
|
332 |
321 |
333 |
322 \begin{frame}[fragile] |
334 \begin{frame}[fragile] |
323 \frametitle{Scipy Methods - \typ{roots}} |
335 \frametitle{Scipy Methods - \typ{roots}} |
324 \begin{itemize} |
336 \begin{itemize} |
325 \item Calculates the roots of polynomials |
337 \item Calculates the roots of polynomials |