author | Puneeth Chaganti <punchagan@fossee.in> |
Wed, 28 Oct 2009 12:36:25 +0530 | |
changeset 222 | ffa8f417d761 |
parent 221 | 9ed9539446bc |
child 223 | 081600805dde |
permissions | -rw-r--r-- |
202 | 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2 |
%Tutorial slides on Python. |
|
3 |
% |
|
4 |
% Author: FOSSEE |
|
5 |
% Copyright (c) 2009, FOSSEE, IIT Bombay |
|
6 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
||
8 |
\documentclass[14pt,compress]{beamer} |
|
9 |
%\documentclass[draft]{beamer} |
|
10 |
%\documentclass[compress,handout]{beamer} |
|
11 |
%\usepackage{pgfpages} |
|
12 |
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
||
14 |
% Modified from: generic-ornate-15min-45min.de.tex |
|
15 |
\mode<presentation> |
|
16 |
{ |
|
17 |
\usetheme{Warsaw} |
|
18 |
\useoutertheme{split} |
|
19 |
\setbeamercovered{transparent} |
|
20 |
} |
|
21 |
||
22 |
\usepackage[english]{babel} |
|
23 |
\usepackage[latin1]{inputenc} |
|
24 |
%\usepackage{times} |
|
25 |
\usepackage[T1]{fontenc} |
|
26 |
\usepackage{amsmath} |
|
27 |
||
28 |
% Taken from Fernando's slides. |
|
29 |
\usepackage{ae,aecompl} |
|
30 |
\usepackage{mathpazo,courier,euler} |
|
31 |
\usepackage[scaled=.95]{helvet} |
|
32 |
||
33 |
\definecolor{darkgreen}{rgb}{0,0.5,0} |
|
34 |
||
35 |
\usepackage{listings} |
|
36 |
\lstset{language=Python, |
|
37 |
basicstyle=\ttfamily\bfseries, |
|
38 |
commentstyle=\color{red}\itshape, |
|
39 |
stringstyle=\color{darkgreen}, |
|
40 |
showstringspaces=false, |
|
41 |
keywordstyle=\color{blue}\bfseries} |
|
42 |
||
43 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
44 |
% Macros |
|
45 |
\setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
46 |
\newcommand{\emphbar}[1] |
|
47 |
{\begin{beamercolorbox}[rounded=true]{emphbar} |
|
48 |
{#1} |
|
49 |
\end{beamercolorbox} |
|
50 |
} |
|
51 |
\newcounter{time} |
|
52 |
\setcounter{time}{0} |
|
53 |
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
54 |
||
55 |
\newcommand{\typ}[1]{\lstinline{#1}} |
|
56 |
||
57 |
\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
58 |
||
59 |
%%% This is from Fernando's setup. |
|
60 |
% \usepackage{color} |
|
61 |
% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
62 |
% % Use and configure listings package for nicely formatted code |
|
63 |
% \usepackage{listings} |
|
64 |
% \lstset{ |
|
65 |
% language=Python, |
|
66 |
% basicstyle=\small\ttfamily, |
|
67 |
% commentstyle=\ttfamily\color{blue}, |
|
68 |
% stringstyle=\ttfamily\color{orange}, |
|
69 |
% showstringspaces=false, |
|
70 |
% breaklines=true, |
|
71 |
% postbreak = \space\dots |
|
72 |
% } |
|
73 |
||
74 |
||
75 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
76 |
% Title page |
|
77 |
\title[Basic Python]{Interpolation, Differentiation and Integration} |
|
78 |
||
79 |
\author[FOSSEE] {FOSSEE} |
|
80 |
||
81 |
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
222
ffa8f417d761
Corrected session number in session5.
Puneeth Chaganti <punchagan@fossee.in>
parents:
221
diff
changeset
|
82 |
\date[] {31, October 2009\\Day 1, Session 5} |
202 | 83 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
84 |
||
85 |
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
86 |
%\logo{\pgfuseimage{iitmlogo}} |
|
87 |
||
88 |
||
89 |
%% Delete this, if you do not want the table of contents to pop up at |
|
90 |
%% the beginning of each subsection: |
|
91 |
\AtBeginSubsection[] |
|
92 |
{ |
|
93 |
\begin{frame}<beamer> |
|
94 |
\frametitle{Outline} |
|
95 |
\tableofcontents[currentsection,currentsubsection] |
|
96 |
\end{frame} |
|
97 |
} |
|
98 |
||
99 |
%%\AtBeginSection[] |
|
100 |
%%{ |
|
101 |
%%\begin{frame}<beamer> |
|
102 |
%% \frametitle{Outline} |
|
103 |
%% \tableofcontents[currentsection,currentsubsection] |
|
104 |
%%\end{frame} |
|
105 |
%%} |
|
106 |
||
107 |
% If you wish to uncover everything in a step-wise fashion, uncomment |
|
108 |
% the following command: |
|
109 |
%\beamerdefaultoverlayspecification{<+->} |
|
110 |
||
111 |
%\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
112 |
||
113 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
114 |
% DOCUMENT STARTS |
|
115 |
\begin{document} |
|
116 |
||
117 |
\begin{frame} |
|
118 |
\titlepage |
|
119 |
\end{frame} |
|
120 |
||
121 |
\begin{frame} |
|
122 |
\frametitle{Outline} |
|
123 |
\tableofcontents |
|
124 |
% \pausesections |
|
125 |
\end{frame} |
|
126 |
||
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
127 |
\section{Interpolation} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
128 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
129 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
130 |
\frametitle{Interpolation} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
131 |
\begin{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
132 |
\item Let us begin with interpolation |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
133 |
\item Let's use the L and T arrays and interpolate this data to obtain data at new points |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
134 |
\end{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
135 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
136 |
In []: L = [] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
137 |
In []: T = [] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
138 |
In []: for line in open('pendulum.txt'): |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
139 |
l, t = line.split() |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
140 |
L.append(float(l)) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
141 |
T.append(float(t)) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
142 |
In []: L = array(L) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
143 |
In []: T = array(T) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
144 |
In []: Tsq = T*T |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
145 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
146 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
147 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
148 |
%% \begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
149 |
%% \frametitle{Interpolation \ldots} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
150 |
%% \begin{small} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
151 |
%% \typ{In []: from scipy.interpolate import interp1d} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
152 |
%% \end{small} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
153 |
%% \begin{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
154 |
%% \item The \typ{interp1d} function returns a function |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
155 |
%% \begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
156 |
%% In []: f = interp1d(L, T) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
157 |
%% \end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
158 |
%% \item Functions can be assigned to variables |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
159 |
%% \item This function interpolates between known data values to obtain unknown |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
160 |
%% \end{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
161 |
%% \end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
162 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
163 |
%% \begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
164 |
%% \frametitle{Interpolation \ldots} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
165 |
%% \begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
166 |
%% In []: Ln = arange(0.1,0.99,0.005) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
167 |
%% # Interpolating! |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
168 |
%% # The new values in range of old data |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
169 |
%% In []: plot(L, T, 'o', Ln, f(Ln), '-') |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
170 |
%% In []: f = interp1d(L, T, kind='cubic') |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
171 |
%% # When kind not specified, it's linear |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
172 |
%% # Others are ... |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
173 |
%% # 'nearest', 'zero', |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
174 |
%% # 'slinear', 'quadratic' |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
175 |
%% \end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
176 |
%% \end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
177 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
178 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
179 |
\frametitle{Spline Interpolation} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
180 |
\begin{small} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
181 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
182 |
In []: from scipy.interpolate import splrep |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
183 |
In []: from scipy.interpolate import splev |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
184 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
185 |
\end{small} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
186 |
\begin{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
187 |
\item Involves two steps |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
188 |
\begin{enumerate} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
189 |
\item Find out the spline curve, coefficients |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
190 |
\item Evaluate the spline at new points |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
191 |
\end{enumerate} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
192 |
\end{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
193 |
\end{frame} |
202 | 194 |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
195 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
196 |
\frametitle{\typ{splrep}} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
197 |
To find the B-spline representation |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
198 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
199 |
In []: tck = splrep(L, T) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
200 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
201 |
Returns a tuple containing |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
202 |
\begin{enumerate} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
203 |
\item the vector of knots, |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
204 |
\item the B-spline coefficients |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
205 |
\item the degree of the spline (default=3) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
206 |
\end{enumerate} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
207 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
208 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
209 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
210 |
\frametitle{\typ{splev}} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
211 |
To Evaluate a B-spline and it's derivatives |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
212 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
213 |
In []: Lnew = arange(0.1,1,0.005) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
214 |
In []: Tnew = splev(Lnew, tck) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
215 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
216 |
#To obtain derivatives of the spline |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
217 |
#use der=1, 2,.. for 1st, 2nd,.. order |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
218 |
In []: Tnew = splev(Lnew, tck, der=1) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
219 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
220 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
221 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
222 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
223 |
\frametitle{} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
224 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
225 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
226 |
\section{Differentiation} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
227 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
228 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
229 |
\frametitle{Numerical Differentiation} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
230 |
\begin{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
231 |
\item Given function $f(x)$ or data points $y=f(x)$ |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
232 |
\item We wish to calculate $f^{'}(x)$ at points $x$ |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
233 |
\item Taylor series - finite difference approximations |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
234 |
\end{itemize} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
235 |
\begin{center} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
236 |
\begin{tabular}{l l} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
237 |
$f(x+h)=f(x)+h.f^{'}(x)$ &Forward \\ |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
238 |
$f(x-h)=f(x)-h.f^{'}(x)$ &Backward |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
239 |
\end{tabular} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
240 |
\end{center} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
241 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
242 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
243 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
244 |
\frametitle{Forward Difference} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
245 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
246 |
In []: x = linspace(0, 2*pi, 100) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
247 |
In []: y = sin(x) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
248 |
In []: deltax = x[1] - x[0] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
249 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
250 |
Obtain the finite forward difference of y |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
251 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
252 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
253 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
254 |
\frametitle{Forward Difference \ldots} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
255 |
\begin{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
256 |
In []: fD = (y[1:] - y[:-1]) / deltax |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
257 |
In []: plot(x, y, x[:-1], fD) |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
258 |
\end{lstlisting} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
259 |
\begin{center} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
260 |
\includegraphics[height=2in, interpolate=true]{data/fwdDiff} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
261 |
\end{center} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
262 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
263 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
264 |
\begin{frame}[fragile] |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
265 |
\frametitle{} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
266 |
\end{frame} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
267 |
|
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
268 |
\section{Quadrature} |
202 | 269 |
|
270 |
\begin{frame}[fragile] |
|
271 |
\frametitle{Quadrature} |
|
272 |
\begin{itemize} |
|
273 |
\item We wish to find area under a curve |
|
274 |
\item Area under $(sin(x) + x^2)$ in $(0,1)$ |
|
275 |
\item scipy has functions to do that |
|
276 |
\end{itemize} |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
277 |
\begin{small} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
278 |
\typ{In []: from scipy.integrate import quad} |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
279 |
\end{small} |
202 | 280 |
\begin{itemize} |
281 |
\item Inputs - function to integrate, limits |
|
282 |
\end{itemize} |
|
283 |
\begin{lstlisting} |
|
284 |
In []: x = 0 |
|
285 |
In []: quad(sin(x)+x**2, 0, 1) |
|
286 |
\end{lstlisting} |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
287 |
\begin{small} |
202 | 288 |
\alert{\typ{error:}} |
289 |
\typ{First argument must be a callable function.} |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
290 |
\end{small} |
202 | 291 |
\end{frame} |
292 |
||
293 |
\begin{frame}[fragile] |
|
294 |
\frametitle{Functions - Definition} |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
295 |
We have been using them all along. Now let's see how to define them. |
202 | 296 |
\begin{lstlisting} |
297 |
In []: def f(x): |
|
298 |
return sin(x)+x**2 |
|
299 |
In []: quad(f, 0, 1) |
|
300 |
\end{lstlisting} |
|
301 |
\begin{itemize} |
|
302 |
\item \typ{def} |
|
303 |
\item arguments |
|
304 |
\item \typ{return} |
|
305 |
\end{itemize} |
|
306 |
\end{frame} |
|
307 |
||
308 |
\begin{frame}[fragile] |
|
309 |
\frametitle{Functions - Calling them} |
|
310 |
\begin{lstlisting} |
|
311 |
In [15]: f() |
|
312 |
--------------------------------------- |
|
313 |
\end{lstlisting} |
|
314 |
\alert{\typ{TypeError:}}\typ{f() takes exactly 1 argument} |
|
315 |
\typ{(0 given)} |
|
316 |
\begin{lstlisting} |
|
317 |
In []: f(0) |
|
318 |
Out[]: 0.0 |
|
319 |
In []: f(1) |
|
320 |
Out[]: 1.8414709848078965 |
|
321 |
\end{lstlisting} |
|
322 |
\end{frame} |
|
323 |
||
324 |
||
325 |
\begin{frame}[fragile] |
|
326 |
\frametitle{Functions - Default Arguments} |
|
327 |
\begin{lstlisting} |
|
328 |
In []: def f(x=1): |
|
329 |
return sin(x)+x**2 |
|
330 |
In []: f(10) |
|
331 |
Out[]: 99.455978889110625 |
|
332 |
In []: f(1) |
|
333 |
Out[]: 1.8414709848078965 |
|
334 |
In []: f() |
|
335 |
Out[]: 1.8414709848078965 |
|
336 |
\end{lstlisting} |
|
337 |
\end{frame} |
|
338 |
||
339 |
\begin{frame}[fragile] |
|
340 |
\frametitle{Functions - Keyword Arguments} |
|
341 |
\begin{lstlisting} |
|
342 |
In []: def f(x=1, y=pi): |
|
343 |
return sin(y)+x**2 |
|
344 |
In []: f() |
|
345 |
Out[]: 1.0000000000000002 |
|
346 |
In []: f(2) |
|
347 |
Out[]: 4.0 |
|
348 |
In []: f(y=2) |
|
349 |
Out[]: 1.9092974268256817 |
|
350 |
In []: f(y=pi/2,x=0) |
|
351 |
Out[]: 1.0 |
|
352 |
\end{lstlisting} |
|
353 |
\end{frame} |
|
354 |
||
355 |
\begin{frame}[fragile] |
|
356 |
\frametitle{More on functions} |
|
357 |
\begin{itemize} |
|
358 |
\item Scope of variables in the function is local |
|
359 |
\item Mutable items are \alert{passed by reference} |
|
360 |
\item First line after definition may be a documentation string |
|
361 |
(\alert{recommended!}) |
|
362 |
\item Function definition and execution defines a name bound to the |
|
363 |
function |
|
364 |
\item You \emph{can} assign a variable to a function! |
|
365 |
\end{itemize} |
|
366 |
\end{frame} |
|
367 |
||
368 |
\begin{frame}[fragile] |
|
369 |
\frametitle{Quadrature \ldots} |
|
370 |
\begin{lstlisting} |
|
371 |
In []: quad(f, 0, 1) |
|
372 |
\end{lstlisting} |
|
373 |
Returns the integral and an estimate of the absolute error in the result. |
|
374 |
\begin{itemize} |
|
375 |
\item Use \typ{dblquad} for Double integrals |
|
376 |
\item Use \typ{tplquad} for Triple integrals |
|
377 |
\end{itemize} |
|
378 |
\end{frame} |
|
379 |
||
380 |
\begin{frame} |
|
381 |
\frametitle{Things we have learned} |
|
382 |
\begin{itemize} |
|
221
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
383 |
\item Interpolation |
9ed9539446bc
Updated day1 session5 and session6.
Puneeth Chaganti <punchagan@fossee.in>
parents:
202
diff
changeset
|
384 |
\item Differentiation |
202 | 385 |
\item Functions |
386 |
\begin{itemize} |
|
387 |
\item Definition |
|
388 |
\item Calling |
|
389 |
\item Default Arguments |
|
390 |
\item Keyword Arguments |
|
391 |
\end{itemize} |
|
392 |
\item Quadrature |
|
393 |
\end{itemize} |
|
394 |
\end{frame} |
|
395 |
\end{document} |
|
396 |