day1/session5.tex
changeset 256 a06196a05043
parent 244 f4f3b36a9fba
child 259 bb77a470e00a
equal deleted inserted replaced
252:359f77927931 256:a06196a05043
   127 \section{Interpolation}
   127 \section{Interpolation}
   128 
   128 
   129 \begin{frame}[fragile]
   129 \begin{frame}[fragile]
   130 \frametitle{Interpolation}
   130 \frametitle{Interpolation}
   131 \begin{itemize}
   131 \begin{itemize}
   132 \item Let's use the L and T arrays and interpolate this data to obtain data at new points
   132   \item Given data file \typ{points.txt}.
   133 \end{itemize}
   133   \item It contains x,y position of particle.
   134 \begin{lstlisting}
   134   \item Plot the given points.
   135 In []: L = []
   135 %%  \item Interpolate the missing region.
   136 In []: T = []
   136 \end{itemize}
   137 In []: for line in open('pendulum.txt'):
   137 \emphbar{Loading data (revisited)}
   138            l, t = line.split()
   138 \begin{lstlisting}
   139            L.append(float(l))
   139 In []: data = loadtxt('points.txt')
   140            T.append(float(t))
   140 In []: data.shape
   141 In []: L = array(L)
   141 Out[]: (40, 2)
   142 In []: T = array(T)
   142 In []: x = data[:,0]
   143 \end{lstlisting}
   143 In []: y = data[:,1]
       
   144 In []: plot(x, y, '.')
       
   145 \end{lstlisting}
       
   146 \end{frame}
       
   147 
       
   148 \begin{frame}
       
   149   \frametitle{\typ{loadtxt}}
       
   150   \begin{itemize}
       
   151   \item Load data from a text file.
       
   152   \item Each row must have same number of values.
       
   153   \end{itemize}
   144 \end{frame}
   154 \end{frame}
   145 
   155 
   146 %% \begin{frame}[fragile]
   156 %% \begin{frame}[fragile]
   147 %% \frametitle{Interpolation \ldots}
   157 %% \frametitle{Interpolation \ldots}
   148 %% \begin{small}
   158 %% \begin{small}
   190 \end{itemize}
   200 \end{itemize}
   191 \end{frame}
   201 \end{frame}
   192 
   202 
   193 \begin{frame}[fragile]
   203 \begin{frame}[fragile]
   194 \frametitle{\typ{splrep}}
   204 \frametitle{\typ{splrep}}
   195 To find the B-spline representation
   205 To find the spline curve
   196 \begin{lstlisting}
   206 \begin{lstlisting}
   197 In []: tck = splrep(L, T)
   207 In []: tck = splrep(x, y)
   198 \end{lstlisting}
   208 \end{lstlisting}
   199 Returns 
   209 \typ{tck} contains parameters required for representing the spline curve!
   200 \begin{enumerate}
       
   201 \item the vector of knots, 
       
   202 \item the B-spline coefficients 
       
   203 \item the degree of the spline (default=3)
       
   204 \end{enumerate}
       
   205 \end{frame}
   210 \end{frame}
   206 
   211 
   207 \begin{frame}[fragile]
   212 \begin{frame}[fragile]
   208 \frametitle{\typ{splev}}
   213 \frametitle{\typ{splev}}
   209 To Evaluate a B-spline and it's derivatives
   214 To Evaluate a B-spline and it's derivatives
   210 \begin{lstlisting}
   215 \begin{lstlisting}
   211 In []: Lnew = arange(0.1,1,0.005)
   216 In []: Xnew = arange(0.01,3,0.02)
   212 In []: Tnew = splev(Lnew, tck)
   217 In []: Ynew = splev(Xnew, tck)
   213 
   218 
   214 #To obtain derivatives of the spline
   219 In []: y.shape
   215 #use der=1, 2,.. for 1st, 2nd,.. order
   220 Out[]: (40,)
   216 In []: Tnew = splev(Lnew, tck, der=1)
   221 
   217 \end{lstlisting}
   222 In []: Ynew.shape
       
   223 Out[]: (150,)
       
   224  
       
   225 In []: plot(Xnew, Ynew)
       
   226 \end{lstlisting}
       
   227 
   218 \end{frame}
   228 \end{frame}
   219 
   229 
   220 %% \begin{frame}[fragile]
   230 %% \begin{frame}[fragile]
   221 %% \frametitle{Interpolation \ldots}
   231 %% \frametitle{Interpolation \ldots}
   222 %% \begin{itemize}
   232 %% \begin{itemize}