203 In []: allclose(Ax, b) |
203 In []: allclose(Ax, b) |
204 Out[]: True |
204 Out[]: True |
205 \end{lstlisting} |
205 \end{lstlisting} |
206 \end{frame} |
206 \end{frame} |
207 |
207 |
|
208 \section{Integration} |
|
209 |
|
210 \subsection{ODEs} |
208 |
211 |
209 \begin{frame}[fragile] |
212 \begin{frame}[fragile] |
210 \frametitle{ODE Integration} |
213 \frametitle{ODE Integration} |
211 We shall use the simple ODE of a simple pendulum. |
214 We shall use the simple ODE of a simple pendulum. |
212 \begin{equation*} |
215 \begin{equation*} |
213 \ddot{\theta} = -\frac{g}{L}sin(\theta) |
216 \ddot{\theta} = -\frac{g}{L}sin(\theta) |
214 \end{equation*} |
217 \end{equation*} |
215 \begin{itemize} |
218 \begin{itemize} |
216 \item This equation can be written as a system of two first order ODEs |
219 \item This equation can be written as a system of two first order ODEs |
217 \item $\dot{\theta} = \omega$ |
|
218 \item $\dot{\omega} = -\frac{g}{L}sin(\theta)$ |
|
219 \item At $t = 0$ \\ |
|
220 $\theta = \theta_0$ \& |
|
221 $\omega = 0$ |
|
222 \end{itemize} |
220 \end{itemize} |
223 \begin{lstlisting} |
221 \begin{align} |
224 \end{lstlisting} |
222 \dot{\theta} &= \omega \\ |
225 \end{frame} |
223 \dot{\omega} &= -\frac{g}{L}sin(\theta) \\ |
226 |
224 \text{At}\ t &= 0 : \nonumber \\ |
227 |
225 \theta = \theta_0\quad & \&\quad \omega = 0 \nonumber |
|
226 \end{align} |
|
227 \end{frame} |
|
228 |
|
229 \begin{frame}[fragile] |
|
230 \frametitle{Solving ODEs using SciPy} |
|
231 \begin{itemize} |
|
232 \item We use the \typ{odeint} function from scipy to do the integration |
|
233 \item Define a function as below |
|
234 \end{itemize} |
|
235 \begin{lstlisting} |
|
236 In []: def pend_int(unknown, t, p): |
|
237 .... theta, omega = unknown |
|
238 .... g, L = p |
|
239 .... f=[omega, -(g/L)*sin(theta)] |
|
240 .... return f |
|
241 .... |
|
242 \end{lstlisting} |
|
243 \end{frame} |
|
244 |
|
245 \begin{frame}[fragile] |
|
246 \frametitle{Solving ODEs using SciPy \ldots} |
|
247 \begin{itemize} |
|
248 \item \typ{t} is the time variable \\ |
|
249 \item \typ{p} has the constants \\ |
|
250 \item \typ{initial} has the initial values |
|
251 \end{itemize} |
|
252 \begin{lstlisting} |
|
253 In []: t = linspace(0, 10, 101) |
|
254 In []: p=(-9.81, 0.2) |
|
255 In []: initial = [10*2*pi/360, 0] |
|
256 \end{lstlisting} |
|
257 \end{frame} |
|
258 |
|
259 \begin{frame}[fragile] |
|
260 \frametitle{Solving ODEs using SciPy \ldots} |
|
261 |
|
262 \small{\typ{In []: from scipy.integrate import odeint}} |
|
263 \begin{lstlisting} |
|
264 In []: pend_sol = odeint(pend_int, |
|
265 initial,t, |
|
266 args=(p,)) |
|
267 \end{lstlisting} |
|
268 \end{frame} |
|
269 |
|
270 \subsection{Quadrature} |
|
271 |
|
272 \begin{frame}[fragile] |
|
273 \frametitle{Quadrature} |
|
274 Calculate the area under $(sin(x) + x^2)$ in the range $(0,1)$ |
|
275 \small{\typ{In []: from scipy.integrate import quad}} |
|
276 \begin{lstlisting} |
|
277 In []: f(x): |
|
278 return sin(x)+x**2 |
|
279 In []: integrate.quad(f, 0, 1) |
|
280 \end{lstlisting} |
|
281 \end{frame} |
228 |
282 |
229 \end{document} |
283 \end{document} |
230 |
284 |
231 \begin{frame}[fragile] |
|
232 \frametitle{} |
|
233 \begin{lstlisting} |
|
234 \end{lstlisting} |
|
235 \end{frame} |
|