lstsq/script.rst
author Nishanth <nishanth@fossee.in>
Thu, 07 Oct 2010 14:33:45 +0530
changeset 237 6c203780bfbe
child 279 5c9dc3419df5
permissions -rw-r--r--
Converted lstsq to new template form
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     4
.. A - Students and teachers from Science and engineering backgrounds
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     5
   B - 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
   C - 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     7
   D - 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     8
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     9
.. Prerequisites
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. -------------
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
..   1. Basic Plotting
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
     2. Arrays
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
     
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
.. Author              : Nishanth Amuluru
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   Internal Reviewer   : 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   External Reviewer   :
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
Script
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
------
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
Hello friends and welcome to the tutorial on Least Square Fit
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
{{{ Show the slide containing title }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
{{{ Show the slide containing the outline slide }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
In this tutorial, we shall look at generating the least square fit line for a
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
given set of points.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
First let us have a look at the problem.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
{{{ Show the slide containing problem statement. }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
We have an input file generated from a simple pendulum experiment.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
It contains two columns of data. The first column is the length of the
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
pendulum and the second is the corresponding time period of the pendulum.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
As we know, the square of time period of a pendulum is directly proportional to
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
its length, we shall plot l vs t^2 and verify this. 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
#[Puneeth:] removed the explanation about loadtxt and unpack
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
 option. It's been done in another LO already. simple dependency 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
 should work?
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
To read the input file and parse the data, we are going to use the
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
loadtxt function.  Type 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
    l
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
    t
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
We can see that l and t are two sequences containing length and time values
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
correspondingly.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
Let us first plot l vs t^2. Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
    tsq = t * t
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
    plot(l, tsq, 'bo')
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
{{{ switch to the plot window }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
#[Puneeth:] Moved explanation of least square fit here. seems more
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
apt. 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
We can see that there is a visible linear trend, but we do not get a
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
straight line connecting them. We shall, therefore, generate a least
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
square fit line.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
{{{ show the slide containing explanation on least square fit }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
As shown in the slide, we are first going to generate the two matrices
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
tsq and A. Then we are going to use the ``lstsq`` function to find the
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
values of m and c.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
let us now generate the A matrix with l values.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
We shall first generate a 2 x 90 matrix with the first row as l values and the
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
second row as ones. Then take the transpose of it. Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
    inter_mat = array((l, ones_like(l)))
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
    inter_mat
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
We see that we have intermediate matrix. Now we need the transpose. Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
    A = inter_mat.T
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
    A
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
Now we have both the matrices A and tsq. We only need to use the ``lstsq``
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
    result = lstsq(A, tsq)
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
The result is a sequence of values. The first item in this sequence,
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
is the matrix p i.e., the values of m and c. Hence, 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
    m, c = result[0]
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
    m
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
    c
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
Now that we have m and c, we need to generate the fitted values of t^2. Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
    tsq_fit = m * l + c
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
    plot(l, tsq, 'bo')
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
    plot(l, tsq_fit, 'r')
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
We get the least square fit of l vs t^2
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
{{{ Pause here and try out the following exercises }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
%% 2 %% change the label on y-axis to "y" and save the lines of code
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
        accordingly
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
{{{ continue from paused state }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
{{{ Show summary slide }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
This brings us to the end of the tutorial.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
we have learnt
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
 * how to generate a least square fit
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
{{{ Show the "sponsored by FOSSEE" slide }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
#[Nishanth]: Will add this line after all of us fix on one.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
Hope you have enjoyed and found it useful.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
Thank you
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139