300 \begin{itemize} |
300 \begin{itemize} |
301 \item Finds the roots of a system of non-linear equations |
301 \item Finds the roots of a system of non-linear equations |
302 \item Input arguments - Function and initial estimate |
302 \item Input arguments - Function and initial estimate |
303 \item Returns the solution |
303 \item Returns the solution |
304 \end{itemize} |
304 \end{itemize} |
305 \begin{lstlisting} |
305 \end{frame} |
306 In []: fsolve(our_f, -pi/2) |
306 |
|
307 \begin{frame}[fragile] |
|
308 \frametitle{\typ{fsolve}} |
|
309 Find the root of $sin(x)+cos^2(x)$ nearest to $0$ |
|
310 \begin{lstlisting} |
|
311 In []: fsolve(sin(x)+cos(x)**2, 0) |
|
312 NameError: name 'x' is not defined |
|
313 In []: x = linspace(-pi, pi) |
|
314 In []: fsolve(sin(x)+cos(x)**2, 0) |
|
315 \end{lstlisting} |
|
316 \begin{small} |
|
317 \alert{\typ{TypeError:}} |
|
318 \typ{'numpy.ndarray' object is not callable} |
|
319 \end{small} |
|
320 \end{frame} |
|
321 |
|
322 \begin{frame}[fragile] |
|
323 \frametitle{Functions - Definition} |
|
324 We have been using them all along. Now let's see how to define them. |
|
325 \begin{lstlisting} |
|
326 In []: def f(x): |
|
327 return sin(x)+cos(x)**2 |
|
328 \end{lstlisting} |
|
329 \begin{itemize} |
|
330 \item \typ{def} |
|
331 \item name |
|
332 \item arguments |
|
333 \item \typ{return} |
|
334 \end{itemize} |
|
335 \end{frame} |
|
336 |
|
337 \begin{frame}[fragile] |
|
338 \frametitle{Functions - Calling them} |
|
339 \begin{lstlisting} |
|
340 In [15]: f() |
|
341 --------------------------------------- |
|
342 \end{lstlisting} |
|
343 \alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument} |
|
344 \typ{(0 given)} |
|
345 \begin{lstlisting} |
|
346 In []: f(0) |
|
347 Out[]: 1.0 |
|
348 In []: f(1) |
|
349 Out[]: 1.1333975665343254 |
|
350 \end{lstlisting} |
|
351 More on Functions later \ldots |
|
352 \end{frame} |
|
353 |
|
354 \begin{frame}[fragile] |
|
355 \frametitle{\typ{fsolve} \ldots} |
|
356 Find the root of $sin(x)+cos^2(x)$ nearest to $0$ |
|
357 \begin{lstlisting} |
|
358 In []: fsolve(f, 0) |
|
359 Out[]: -0.66623943249251527 |
307 \end{lstlisting} |
360 \end{lstlisting} |
308 \end{frame} |
361 \end{frame} |
309 |
362 |
310 %% \begin{frame}[fragile] |
363 %% \begin{frame}[fragile] |
311 %% \frametitle{Scipy Methods \dots} |
364 %% \frametitle{Scipy Methods \dots} |