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