# HG changeset patch # User Nishanth # Date 1286442225 -19800 # Node ID 6c203780bfbeb3edc4fc225f353a6cf3ccff42cd # Parent 33828497b5dae0501875cfdd45825cc7d0eaf2d0 Converted lstsq to new template form diff -r 33828497b5da -r 6c203780bfbe lstsq.rst --- a/lstsq.rst Thu Oct 07 14:31:05 2010 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -.. Author : Nishanth - Internal Reviewer 1 : Puneeth - Internal Reviewer 2 : - External Reviewer : - -Hello friends and welcome to the tutorial on Least Square Fit - -{{{ Show the slide containing title }}} - -{{{ Show the slide containing the outline slide }}} - -In this tutorial, we shall look at generating the least square fit line for a -given set of points. - -First let us have a look at the problem. - -{{{ Show the slide containing problem statement. }}} - -We have an input file generated from a simple pendulum experiment. - -It contains two columns of data. The first column is the length of the -pendulum and the second is the corresponding time period of the pendulum. - -As we know, the square of time period of a pendulum is directly proportional to -its length, we shall plot l vs t^2 and verify this. - -#[Puneeth:] removed the explanation about loadtxt and unpack - option. It's been done in another LO already. simple dependency - should work? - -To read the input file and parse the data, we are going to use the -loadtxt function. Type -:: - - l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) - l - t - -We can see that l and t are two sequences containing length and time values -correspondingly. - -Let us first plot l vs t^2. Type -:: - - tsq = t * t - plot(l, tsq, 'bo') - -{{{ switch to the plot window }}} - -#[Puneeth:] Moved explanation of least square fit here. seems more -apt. - -We can see that there is a visible linear trend, but we do not get a -straight line connecting them. We shall, therefore, generate a least -square fit line. - -{{{ show the slide containing explanation on least square fit }}} - -As shown in the slide, we are first going to generate the two matrices -tsq and A. Then we are going to use the ``lstsq`` function to find the -values of m and c. - -let us now generate the A matrix with l values. -We shall first generate a 2 x 90 matrix with the first row as l values and the -second row as ones. Then take the transpose of it. Type -:: - - inter_mat = array((l, ones_like(l))) - inter_mat - -We see that we have intermediate matrix. Now we need the transpose. Type -:: - - A = inter_mat.T - A - -Now we have both the matrices A and tsq. We only need to use the ``lstsq`` -Type -:: - - result = lstsq(A, tsq) - -The result is a sequence of values. The first item in this sequence, -is the matrix p i.e., the values of m and c. Hence, -:: - - m, c = result[0] - m - c - -Now that we have m and c, we need to generate the fitted values of t^2. Type -:: - - tsq_fit = m * l + c - plot(l, tsq, 'bo') - plot(l, tsq_fit, 'r') - -We get the least square fit of l vs t^2 - -{{{ Pause here and try out the following exercises }}} - -%% 2 %% change the label on y-axis to "y" and save the lines of code - accordingly - -{{{ continue from paused state }}} - -{{{ Show summary slide }}} - -This brings us to the end of the tutorial. -we have learnt - - * how to generate a least square fit - -{{{ Show the "sponsored by FOSSEE" slide }}} - -#[Nishanth]: Will add this line after all of us fix on one. -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India - -Hope you have enjoyed and found it useful. -Thank you - -Questions -========= - - 1. What does ones_like([1, 2, 3]) produce - - a. array([1, 1, 1]) - #. [1, 1, 1] - #. [1.0, 1.0, 1.0] - #. Error - - 2. What does ones_like([1.2, 3, 4, 5]) produce - - a. [1.2, 3, 4, 5] - #. array([1.0, 1.0, 1.0, 1.0]) - #. array([1, 1, 1, 1]) - #. array([1.2, 3, 4, 5]) - - 3. What is the shape of the diff -r 33828497b5da -r 6c203780bfbe lstsq/questions.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lstsq/questions.rst Thu Oct 07 14:33:45 2010 +0530 @@ -0,0 +1,18 @@ +Objective Questions +------------------- + + 1. What does ones_like([1, 2, 3]) produce + + a. array([1, 1, 1]) + #. [1, 1, 1] + #. [1.0, 1.0, 1.0] + #. Error + + 2. What does ones_like([1.2, 3, 4, 5]) produce + + a. [1.2, 3, 4, 5] + #. array([1.0, 1.0, 1.0, 1.0]) + #. array([1, 1, 1, 1]) + #. array([1.2, 3, 4, 5]) + + diff -r 33828497b5da -r 6c203780bfbe lstsq/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lstsq/quickref.tex Thu Oct 07 14:33:45 2010 +0530 @@ -0,0 +1,11 @@ +Creating a tuple:\\ +{\ex \lstinline| t = (1, "hello", 2.5)|} + +Accessing elements of tuples:\\ +{\ex \lstinline| t[index] Ex: t[2]|} + +Accessing slices of tuples:\\ +{\ex \lstinline| t[start:stop:step]|} + +Swapping values:\\ +{\ex \lstinline| a, b = b, a|} diff -r 33828497b5da -r 6c203780bfbe lstsq/script.rst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lstsq/script.rst Thu Oct 07 14:33:45 2010 +0530 @@ -0,0 +1,139 @@ +.. Objectives +.. ---------- + +.. A - Students and teachers from Science and engineering backgrounds + B - + C - + D - + +.. Prerequisites +.. ------------- + +.. 1. Basic Plotting + 2. Arrays + +.. Author : Nishanth Amuluru + Internal Reviewer : + External Reviewer : + Checklist OK? : [2010-10-05] + +Script +------ + +Hello friends and welcome to the tutorial on Least Square Fit + +{{{ Show the slide containing title }}} + +{{{ Show the slide containing the outline slide }}} + +In this tutorial, we shall look at generating the least square fit line for a +given set of points. + +First let us have a look at the problem. + +{{{ Show the slide containing problem statement. }}} + +We have an input file generated from a simple pendulum experiment. + +It contains two columns of data. The first column is the length of the +pendulum and the second is the corresponding time period of the pendulum. + +As we know, the square of time period of a pendulum is directly proportional to +its length, we shall plot l vs t^2 and verify this. + +#[Puneeth:] removed the explanation about loadtxt and unpack + option. It's been done in another LO already. simple dependency + should work? + +To read the input file and parse the data, we are going to use the +loadtxt function. Type +:: + + l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) + l + t + +We can see that l and t are two sequences containing length and time values +correspondingly. + +Let us first plot l vs t^2. Type +:: + + tsq = t * t + plot(l, tsq, 'bo') + +{{{ switch to the plot window }}} + +#[Puneeth:] Moved explanation of least square fit here. seems more +apt. + +We can see that there is a visible linear trend, but we do not get a +straight line connecting them. We shall, therefore, generate a least +square fit line. + +{{{ show the slide containing explanation on least square fit }}} + +As shown in the slide, we are first going to generate the two matrices +tsq and A. Then we are going to use the ``lstsq`` function to find the +values of m and c. + +let us now generate the A matrix with l values. +We shall first generate a 2 x 90 matrix with the first row as l values and the +second row as ones. Then take the transpose of it. Type +:: + + inter_mat = array((l, ones_like(l))) + inter_mat + +We see that we have intermediate matrix. Now we need the transpose. Type +:: + + A = inter_mat.T + A + +Now we have both the matrices A and tsq. We only need to use the ``lstsq`` +Type +:: + + result = lstsq(A, tsq) + +The result is a sequence of values. The first item in this sequence, +is the matrix p i.e., the values of m and c. Hence, +:: + + m, c = result[0] + m + c + +Now that we have m and c, we need to generate the fitted values of t^2. Type +:: + + tsq_fit = m * l + c + plot(l, tsq, 'bo') + plot(l, tsq_fit, 'r') + +We get the least square fit of l vs t^2 + +{{{ Pause here and try out the following exercises }}} + +%% 2 %% change the label on y-axis to "y" and save the lines of code + accordingly + +{{{ continue from paused state }}} + +{{{ Show summary slide }}} + +This brings us to the end of the tutorial. +we have learnt + + * how to generate a least square fit + +{{{ Show the "sponsored by FOSSEE" slide }}} + +#[Nishanth]: Will add this line after all of us fix on one. +This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India + +Hope you have enjoyed and found it useful. +Thank you + + diff -r 33828497b5da -r 6c203780bfbe lstsq/slides.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lstsq/slides.tex Thu Oct 07 14:33:45 2010 +0530 @@ -0,0 +1,106 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2009, FOSSEE, IIT Bombay +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\documentclass[14pt,compress]{beamer} +%\documentclass[draft]{beamer} +%\documentclass[compress,handout]{beamer} +%\usepackage{pgfpages} +%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] + +% Modified from: generic-ornate-15min-45min.de.tex +\mode +{ + \usetheme{Warsaw} + \useoutertheme{infolines} + \setbeamercovered{transparent} +} + +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +%\usepackage{times} +\usepackage[T1]{fontenc} + +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros +\setbeamercolor{emphbar}{bg=blue!20, fg=black} +\newcommand{\emphbar}[1] +{\begin{beamercolorbox}[rounded=true]{emphbar} + {#1} + \end{beamercolorbox} +} +\newcounter{time} +\setcounter{time}{0} +\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} + +\newcommand{\typ}[1]{\lstinline{#1}} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +% Title page +\title{Your Title Here} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[fragile] + \frametitle{Outline} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% All other slides here. %% +%% The same slides will be used in a classroom setting. %% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[fragile] + \frametitle{Summary} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document}