lstsq/script.rst
author Puneeth Chaganti <punchagan@fossee.in>
Tue, 19 Oct 2010 14:26:29 +0530
changeset 338 e4a7d0924143
parent 279 5c9dc3419df5
child 341 9f7eb1ed0e08
permissions -rw-r--r--
Reviewed Least Square Fit LO.
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
279
5c9dc3419df5 Slides for lstsq LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 237
diff changeset
     9
.. Plotting a least square fit line
5c9dc3419df5 Slides for lstsq LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 237
diff changeset
    10
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
.. Prerequisites
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    12
.. -------------
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
..   1. Basic Plotting
279
5c9dc3419df5 Slides for lstsq LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 237
diff changeset
    15
..   2. Arrays
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
     
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
.. Author              : Nishanth Amuluru
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
   Internal Reviewer   : 
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
   External Reviewer   :
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
   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
    21
338
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    22
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    23
.. #[Puneeth: Add pre-requisites.]
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    24
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
Script
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
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
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
    29
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
{{{ Show the slide containing title }}}
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
{{{ Show the slide containing the outline slide }}}
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
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
    35
given set of points.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
First let us have a look at the problem.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
{{{ Show the slide containing problem statement. }}}
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
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
    42
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
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
    44
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
    45
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
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
    47
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
    48
338
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    49
.. #[Puneeth:] removed the explanation about loadtxt and unpack
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    50
..  option. It's been done in another LO already. simple dependency 
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    51
..  should work?
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
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
    54
loadtxt function.  Type 
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
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
    l
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
    t
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
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
    62
correspondingly.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
Let us first plot l vs t^2. Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
::
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
    tsq = t * t
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
    plot(l, tsq, 'bo')
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
{{{ switch to the plot window }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
338
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    72
.. #[Puneeth:] Moved explanation of least square fit here. seems more
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
    73
.. apt. 
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
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
    76
straight line connecting them. We shall, therefore, generate a least
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
square fit line.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
{{{ show the slide containing explanation on least square fit }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
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
    82
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
    83
values of m and c.
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
let us now generate the A matrix with l values.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
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
    87
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
    88
::
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
    inter_mat = array((l, ones_like(l)))
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
    inter_mat
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
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
    94
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
    A = inter_mat.T
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
    A
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
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
   100
Type
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
::
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
    result = lstsq(A, tsq)
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
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
   106
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
   107
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
    m, c = result[0]
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
    m
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
    c
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
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
   114
::
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
    tsq_fit = m * l + c
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
    plot(l, tsq, 'bo')
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
    plot(l, tsq_fit, 'r')
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
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
   121
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
{{{ Pause here and try out the following exercises }}}
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
%% 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
   125
        accordingly
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
{{{ continue from paused state }}}
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
{{{ Show summary slide }}}
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
This brings us to the end of the tutorial.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
we have learnt
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
 * how to generate a least square fit
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
{{{ Show the "sponsored by FOSSEE" slide }}}
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
338
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   138
.. #[Nishanth]: Will add this line after all of us fix on one.
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   139
.. This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
237
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
Hope you have enjoyed and found it useful.
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
Thank you
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
6c203780bfbe Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
338
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   145
.. 
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   146
   Local Variables:
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   147
   mode: rst
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   148
   indent-tabs-mode: nil
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   149
   sentence-end-double-space: nil
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   150
   fill-column: 75
e4a7d0924143 Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 279
diff changeset
   151
   End: