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$ |
|
243 \vspace{-0.1in} |
|
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$ |
249 \begin{lstlisting} |
252 \begin{lstlisting} |
250 In []: fsolve(sin(x)+cos(x)**2, 0) |
253 In []: fsolve(sin(x)+cos(x)**2, 0) |
251 NameError: name 'x' is not defined |
254 NameError: name 'x' is not defined |
252 In []: x = linspace(-pi, pi) |
255 In []: x = linspace(-pi, pi) |
253 In []: fsolve(sin(x)+cos(x)**2, 0) |
256 In []: fsolve(sin(x)+cos(x)**2, 0) |