71 % postbreak = \space\dots |
71 % postbreak = \space\dots |
72 % } |
72 % } |
73 |
73 |
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
75 % Title page |
75 % Title page |
76 \title[ODEs \& Root Finding]{Python for Science and Engg:\\ODEs \& Finding Roots} |
76 \title[Solving Equations \& ODEs]{Python for Science and Engg:\\Solving Equations \& ODEs} |
77 |
77 |
78 \author[FOSSEE] {FOSSEE} |
78 \author[FOSSEE] {FOSSEE} |
79 |
79 |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
81 \date[] {31, October 2009\\Day 1, Session 6} |
81 \date[] {31, October 2009\\Day 1, Session 6} |
121 %% \frametitle{Outline} |
121 %% \frametitle{Outline} |
122 %% \tableofcontents |
122 %% \tableofcontents |
123 %% % You might wish to add the option [pausesections] |
123 %% % You might wish to add the option [pausesections] |
124 %% \end{frame} |
124 %% \end{frame} |
125 |
125 |
|
126 \section{Solving linear equations} |
|
127 |
|
128 \begin{frame}[fragile] |
|
129 \frametitle{Solution of equations} |
|
130 Consider, |
|
131 \begin{align*} |
|
132 3x + 2y - z & = 1 \\ |
|
133 2x - 2y + 4z & = -2 \\ |
|
134 -x + \frac{1}{2}y -z & = 0 |
|
135 \end{align*} |
|
136 Solution: |
|
137 \begin{align*} |
|
138 x & = 1 \\ |
|
139 y & = -2 \\ |
|
140 z & = -2 |
|
141 \end{align*} |
|
142 \end{frame} |
|
143 |
|
144 \begin{frame}[fragile] |
|
145 \frametitle{Solving using Matrices} |
|
146 Let us now look at how to solve this using \kwrd{matrices} |
|
147 \begin{lstlisting} |
|
148 In []: A = array([[3,2,-1], |
|
149 [2,-2,4], |
|
150 [-1, 0.5, -1]]) |
|
151 In []: b = array([[1], [-2], [0]]) |
|
152 In []: x = solve(A, b) |
|
153 In []: Ax = dot(A,x) |
|
154 \end{lstlisting} |
|
155 \end{frame} |
|
156 |
|
157 \begin{frame}[fragile] |
|
158 \frametitle{Solution:} |
|
159 \begin{lstlisting} |
|
160 In []: x |
|
161 Out[]: |
|
162 array([[ 1.], |
|
163 [-2.], |
|
164 [-2.]]) |
|
165 \end{lstlisting} |
|
166 \end{frame} |
|
167 |
|
168 \begin{frame}[fragile] |
|
169 \frametitle{Let's check!} |
|
170 \begin{lstlisting} |
|
171 In []: Ax |
|
172 Out[]: |
|
173 array([[ 1.00000000e+00], |
|
174 [ -2.00000000e+00], |
|
175 [ 2.22044605e-16]]) |
|
176 \end{lstlisting} |
|
177 \begin{block}{} |
|
178 The last term in the matrix is actually \alert{0}!\\ |
|
179 We can use \kwrd{allclose()} to check. |
|
180 \end{block} |
|
181 \begin{lstlisting} |
|
182 In []: allclose(Ax, b) |
|
183 Out[]: True |
|
184 \end{lstlisting} |
|
185 \inctime{15} |
|
186 \end{frame} |
|
187 |
|
188 \subsection{Exercises} |
|
189 |
|
190 \begin{frame}[fragile] |
|
191 \frametitle{Problem 1} |
|
192 Given the matrix:\\ |
|
193 \begin{center} |
|
194 $\begin{bmatrix} |
|
195 -2 & 2 & 3\\ |
|
196 2 & 1 & 6\\ |
|
197 -1 &-2 & 0\\ |
|
198 \end{bmatrix}$ |
|
199 \end{center} |
|
200 Find: |
|
201 \begin{itemize} |
|
202 \item[i] Transpose |
|
203 \item[ii]Inverse |
|
204 \item[iii]Determinant |
|
205 \item[iv] Eigenvalues and Eigen vectors |
|
206 \item[v] Singular Value decomposition |
|
207 \end{itemize} |
|
208 \end{frame} |
|
209 |
|
210 \begin{frame}[fragile] |
|
211 \frametitle{Problem 2} |
|
212 Given |
|
213 \begin{center} |
|
214 A = |
|
215 $\begin{bmatrix} |
|
216 -3 & 1 & 5 \\ |
|
217 1 & 0 & -2 \\ |
|
218 5 & -2 & 4 \\ |
|
219 \end{bmatrix}$ |
|
220 , B = |
|
221 $\begin{bmatrix} |
|
222 0 & 9 & -12 \\ |
|
223 -9 & 0 & 20 \\ |
|
224 12 & -20 & 0 \\ |
|
225 \end{bmatrix}$ |
|
226 \end{center} |
|
227 Find: |
|
228 \begin{itemize} |
|
229 \item[i] Sum of A and B |
|
230 \item[ii]Elementwise Product of A and B |
|
231 \item[iii] Matrix product of A and B |
|
232 \end{itemize} |
|
233 \end{frame} |
|
234 |
|
235 \begin{frame}[fragile] |
|
236 \frametitle{Solution} |
|
237 Sum: |
|
238 $\begin{bmatrix} |
|
239 -3 & 10 & 7 \\ |
|
240 -8 & 0 & 18 \\ |
|
241 17 & -22 & 4 \\ |
|
242 \end{bmatrix}$ |
|
243 ,\\ Elementwise Product: |
|
244 $\begin{bmatrix} |
|
245 0 & 9 & -60 \\ |
|
246 -9 & 0 & -40 \\ |
|
247 60 & 40 & 0 \\ |
|
248 \end{bmatrix}$ |
|
249 ,\\ Matrix product: |
|
250 $\begin{bmatrix} |
|
251 51 & -127 & 56 \\ |
|
252 -24 & 49 & -12 \\ |
|
253 66 & -35 & -100 \\ |
|
254 \end{bmatrix}$ |
|
255 \end{frame} |
|
256 |
|
257 \begin{frame}[fragile] |
|
258 \frametitle{Problem 3} |
|
259 Solve the set of equations: |
|
260 \begin{align*} |
|
261 x + y + 2z -w & = 3\\ |
|
262 2x + 5y - z - 9w & = -3\\ |
|
263 2x + y -z + 3w & = -11 \\ |
|
264 x - 3y + 2z + 7w & = -5\\ |
|
265 \end{align*} |
|
266 \inctime{10} |
|
267 \end{frame} |
|
268 |
|
269 \begin{frame}[fragile] |
|
270 \frametitle{Solution} |
|
271 Use \kwrd{solve()} |
|
272 \begin{align*} |
|
273 x & = -5\\ |
|
274 y & = 2\\ |
|
275 z & = 3\\ |
|
276 w & = 0\\ |
|
277 \end{align*} |
|
278 \end{frame} |
|
279 |
|
280 \section{Finding Roots} |
|
281 |
|
282 \begin{frame}[fragile] |
|
283 \frametitle{Scipy Methods - \typ{roots}} |
|
284 \begin{itemize} |
|
285 \item Calculates the roots of polynomials |
|
286 \end{itemize} |
|
287 \begin{lstlisting} |
|
288 In []: coeffs = [1, 6, 13] |
|
289 In []: roots(coeffs) |
|
290 \end{lstlisting} |
|
291 \end{frame} |
|
292 |
|
293 \begin{frame}[fragile] |
|
294 \frametitle{Scipy Methods - \typ{fsolve}} |
|
295 \begin{small} |
|
296 \begin{lstlisting} |
|
297 In []: from scipy.optimize import fsolve |
|
298 \end{lstlisting} |
|
299 \end{small} |
|
300 \begin{itemize} |
|
301 \item Finds the roots of a system of non-linear equations |
|
302 \item Input arguments - Function and initial estimate |
|
303 \item Returns the solution |
|
304 \end{itemize} |
|
305 \begin{lstlisting} |
|
306 In []: fsolve(our_f, -pi/2) |
|
307 \end{lstlisting} |
|
308 \end{frame} |
|
309 |
|
310 %% \begin{frame}[fragile] |
|
311 %% \frametitle{Scipy Methods \dots} |
|
312 %% \begin{small} |
|
313 %% \begin{lstlisting} |
|
314 %% In []: from scipy.optimize import fixed_point |
|
315 |
|
316 %% In []: from scipy.optimize import bisect |
|
317 |
|
318 %% In []: from scipy.optimize import newton |
|
319 %% \end{lstlisting} |
|
320 %% \end{small} |
|
321 %% \end{frame} |
|
322 |
126 \section{ODEs} |
323 \section{ODEs} |
127 |
|
128 \begin{frame}[fragile] |
324 \begin{frame}[fragile] |
129 \frametitle{ODE Integration} |
325 \frametitle{ODE Integration} |
130 We shall use the simple ODE of a simple pendulum. |
326 We shall use the simple ODE of a simple pendulum. |
131 \begin{equation*} |
327 \begin{equation*} |
132 \ddot{\theta} = -\frac{g}{L}sin(\theta) |
328 \ddot{\theta} = -\frac{g}{L}sin(\theta) |
179 In []: pend_sol = odeint(pend_int, |
375 In []: pend_sol = odeint(pend_int, |
180 initial,t) |
376 initial,t) |
181 \end{lstlisting} |
377 \end{lstlisting} |
182 \end{frame} |
378 \end{frame} |
183 |
379 |
184 \section{Finding Roots} |
|
185 |
|
186 \begin{frame}[fragile] |
|
187 \frametitle{Scipy Methods - \typ{roots}} |
|
188 \begin{itemize} |
|
189 \item Calculates the roots of polynomials |
|
190 \end{itemize} |
|
191 \begin{lstlisting} |
|
192 In []: coeffs = [1, 6, 13] |
|
193 In []: roots(coeffs) |
|
194 \end{lstlisting} |
|
195 \end{frame} |
|
196 |
|
197 \begin{frame}[fragile] |
|
198 \frametitle{Scipy Methods - \typ{fsolve}} |
|
199 \begin{small} |
|
200 \begin{lstlisting} |
|
201 In []: from scipy.optimize import fsolve |
|
202 \end{lstlisting} |
|
203 \end{small} |
|
204 \begin{itemize} |
|
205 \item Finds the roots of a system of non-linear equations |
|
206 \item Input arguments - Function and initial estimate |
|
207 \item Returns the solution |
|
208 \end{itemize} |
|
209 \begin{lstlisting} |
|
210 In []: fsolve(our_f, -pi/2) |
|
211 \end{lstlisting} |
|
212 \end{frame} |
|
213 |
|
214 %% \begin{frame}[fragile] |
|
215 %% \frametitle{Scipy Methods \dots} |
|
216 %% \begin{small} |
|
217 %% \begin{lstlisting} |
|
218 %% In []: from scipy.optimize import fixed_point |
|
219 |
|
220 %% In []: from scipy.optimize import bisect |
|
221 |
|
222 %% In []: from scipy.optimize import newton |
|
223 %% \end{lstlisting} |
|
224 %% \end{small} |
|
225 %% \end{frame} |
|
226 |
380 |
227 \begin{frame} |
381 \begin{frame} |
228 \frametitle{Things we have learned} |
382 \frametitle{Things we have learned} |
229 \begin{itemize} |
383 \begin{itemize} |
230 \item Solving ODEs |
384 \item Solving ODEs |