lstsq.rst
author nishanth
Wed, 15 Sep 2010 18:51:28 +0530
changeset 135 7bc03b5096f9
parent 132 b8f7ee434b91
child 139 9e67c055a413
permissions -rw-r--r--
corrected the rst syntax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Least Square Fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     2
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     4
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     6
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     7
In this tutorial, we shall look at generating the least square fit line for a
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     8
given set of points.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
     9
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    10
First let us have a look at the problem.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    11
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    12
{{{ Show the slide containing problem statement. }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    13
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    14
We have an input file generated from a simple pendulum experiment.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    15
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    16
It contains two columns of data. The first column is the length of the
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    17
pendulum and the second is the corresponding time period of the pendulum.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    18
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    19
As we know, the square of time period of a pendulum is directly proportional to
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    20
its length, we shall plot l vs t^2 and verify if the proportionality is linear.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    21
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    22
If it is not linear, we shall generate a least square fit line.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    23
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    24
{{{ show the slide containing explanation on least square fit }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    25
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    26
As shown in the slide, we are first going to generate the two matrices tsq and
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    27
A. Then we are going to use the =lstsq= function to find the values of m and c.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    28
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    29
To read the input file and parse the data, we are going to loadtxt function.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    30
Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    31
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    32
    data = loadtxt("/home/fossee/pendulum.txt")
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    33
    data
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    34
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    35
As you can see, data is a sequence containing 90 records. Each record contains
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    36
two values. The first is length and second is time period. But what we need is 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    37
two sequences. One sequence containing all the length values and one containing
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    38
all the time values.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    39
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    40
Hence we have to use the unpack option with loadtxt. It unpacks the data into
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    41
 sequences depending on the structure of data.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    42
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    43
Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    44
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    45
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    46
    l
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    47
    t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    48
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    49
We can see that l and t are two sequences containing length and time values
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    50
correspondingly.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    51
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    52
Let us first plot l vs t^2. Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    53
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    54
    tsq = t * t
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    55
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    56
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    57
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    58
{{{ switch to the plot window }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    59
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    60
We can see that there is a visible linear trend.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    61
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    62
let us now generate the A matrix with l values.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    63
We shall first generate a 2 x 90 matrix with the first row as l values and the
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    64
second row as ones. Then take the transpose of it. Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    65
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    66
    inter_mat = array((l, ones_like(l)))
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    67
    inter_mat
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    68
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    69
We see that we have intermediate matrix. Now we need the transpose.Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    70
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    71
    A = inter_mat.T
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    72
    A
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    73
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    74
Now we have both the matrices A and tsq. We only need to use the =lstsq=
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    75
Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    76
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    77
    result = lstsq(A, tsq)
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    78
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    79
The result is a sequence of values. The first item is the matrix p or in simple
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    80
words, the values of m and c. Hence, ::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    81
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    82
    m, c = result[0]
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    83
    m
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    84
    c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    85
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    86
Now that we have m and c, we need to generate the fitted values of t^2. Type::
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    87
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    88
    tsq_fit = m * l + c
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    89
    plot(l, tsq, 'bo')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    90
    plot(l, tsq_fit, 'r')
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    91
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    92
We get the least square fit of l vs t^2
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    93
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    94
{{{ Pause here and try out the following exercises }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    95
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    96
%% 2 %% change the label on y-axis to "y" and save the lines of code
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    97
        accordingly
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    98
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
    99
{{{ continue from paused state }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   100
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   101
{{{ Show summary slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   102
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   103
This brings us to the end of the tutorial.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   104
we have learnt
135
7bc03b5096f9 corrected the rst syntax
nishanth
parents: 132
diff changeset
   105
132
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   106
 * how to use loadtxt to read files
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   107
 * how to generate a least square fit
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   108
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   109
{{{ Show the "sponsored by FOSSEE" slide }}}
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   110
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   111
#[Nishanth]: Will add this line after all of us fix on one.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   112
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   113
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   114
Hope you have enjoyed and found it useful.
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   115
Thankyou
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   116
 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   117
.. Author              : Nishanth
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   118
   Internal Reviewer 1 : 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   119
   Internal Reviewer 2 : 
b8f7ee434b91 initial commit of lstsq
nishanth
parents:
diff changeset
   120
   External Reviewer   :