lstsq.rst
changeset 195 e8a251048213
parent 139 9e67c055a413
child 221 7cd975ff5f0d
equal deleted inserted replaced
194:ca81c0a67c75 195:e8a251048213
       
     1 .. Author              : Nishanth
       
     2    Internal Reviewer 1 : Puneeth
       
     3    Internal Reviewer 2 : 
       
     4    External Reviewer   :
       
     5 
     1 Hello friends and welcome to the tutorial on Least Square Fit
     6 Hello friends and welcome to the tutorial on Least Square Fit
     2 
     7 
     3 {{{ Show the slide containing title }}}
     8 {{{ Show the slide containing title }}}
     4 
     9 
     5 {{{ Show the slide containing the outline slide }}}
    10 {{{ Show the slide containing the outline slide }}}
    15 
    20 
    16 It contains two columns of data. The first column is the length of the
    21 It contains two columns of data. The first column is the length of the
    17 pendulum and the second is the corresponding time period of the pendulum.
    22 pendulum and the second is the corresponding time period of the pendulum.
    18 
    23 
    19 As we know, the square of time period of a pendulum is directly proportional to
    24 As we know, the square of time period of a pendulum is directly proportional to
    20 its length, we shall plot l vs t^2 and verify if the proportionality is linear.
    25 its length, we shall plot l vs t^2 and verify this. 
    21 
    26 
    22 If it is not linear, we shall generate a least square fit line.
    27 #[Puneeth:] removed the explanation about loadtxt and unpack
       
    28  option. It's been done in another LO already. simple dependency 
       
    29  should work?
    23 
    30 
    24 {{{ show the slide containing explanation on least square fit }}}
    31 To read the input file and parse the data, we are going to use the
    25 
    32 loadtxt function.  Type 
    26 As shown in the slide, we are first going to generate the two matrices tsq and
       
    27 A. Then we are going to use the =lstsq= function to find the values of m and c.
       
    28 
       
    29 To read the input file and parse the data, we are going to loadtxt function.
       
    30 Type
       
    31 ::
       
    32 
       
    33     data = loadtxt("/home/fossee/pendulum.txt")
       
    34     data
       
    35 
       
    36 As you can see, data is a sequence containing 90 records. Each record contains
       
    37 two values. The first is length and second is time period. But what we need is 
       
    38 two sequences. One sequence containing all the length values and one containing
       
    39 all the time values.
       
    40 
       
    41 Hence we have to use the unpack option with loadtxt. It unpacks the data into
       
    42  sequences depending on the structure of data.
       
    43 
       
    44 Type
       
    45 ::
    33 ::
    46 
    34 
    47     l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
    35     l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
    48     l
    36     l
    49     t
    37     t
    55 ::
    43 ::
    56 
    44 
    57     tsq = t * t
    45     tsq = t * t
    58     plot(l, tsq, 'bo')
    46     plot(l, tsq, 'bo')
    59 
    47 
    60 
       
    61 {{{ switch to the plot window }}}
    48 {{{ switch to the plot window }}}
    62 
    49 
    63 We can see that there is a visible linear trend.
    50 #[Puneeth:] Moved explanation of least square fit here. seems more
       
    51 apt. 
       
    52 
       
    53 We can see that there is a visible linear trend, but we do not get a
       
    54 straight line connecting them. We shall, therefore, generate a least
       
    55 square fit line.
       
    56 
       
    57 {{{ show the slide containing explanation on least square fit }}}
       
    58 
       
    59 As shown in the slide, we are first going to generate the two matrices
       
    60 tsq and A. Then we are going to use the ``lstsq`` function to find the
       
    61 values of m and c.
    64 
    62 
    65 let us now generate the A matrix with l values.
    63 let us now generate the A matrix with l values.
    66 We shall first generate a 2 x 90 matrix with the first row as l values and the
    64 We shall first generate a 2 x 90 matrix with the first row as l values and the
    67 second row as ones. Then take the transpose of it. Type
    65 second row as ones. Then take the transpose of it. Type
    68 ::
    66 ::
    69 
    67 
    70     inter_mat = array((l, ones_like(l)))
    68     inter_mat = array((l, ones_like(l)))
    71     inter_mat
    69     inter_mat
    72 
    70 
    73 We see that we have intermediate matrix. Now we need the transpose.Type
    71 We see that we have intermediate matrix. Now we need the transpose. Type
    74 ::
    72 ::
    75 
    73 
    76     A = inter_mat.T
    74     A = inter_mat.T
    77     A
    75     A
    78 
    76 
    79 Now we have both the matrices A and tsq. We only need to use the =lstsq=
    77 Now we have both the matrices A and tsq. We only need to use the ``lstsq``
    80 Type
    78 Type
    81 ::
    79 ::
    82 
    80 
    83     result = lstsq(A, tsq)
    81     result = lstsq(A, tsq)
    84 
    82 
    85 The result is a sequence of values. The first item is the matrix p or in simple
    83 The result is a sequence of values. The first item in this sequence,
    86 words, the values of m and c. Hence, 
    84 is the matrix p i.e., the values of m and c. Hence, 
    87 ::
    85 ::
    88 
    86 
    89     m, c = result[0]
    87     m, c = result[0]
    90     m
    88     m
    91     c
    89     c
   118 
   116 
   119 #[Nishanth]: Will add this line after all of us fix on one.
   117 #[Nishanth]: Will add this line after all of us fix on one.
   120 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   118 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   121 
   119 
   122 Hope you have enjoyed and found it useful.
   120 Hope you have enjoyed and found it useful.
   123 Thankyou
   121 Thank you
   124  
   122  
   125 .. Author              : Nishanth
       
   126    Internal Reviewer 1 : 
       
   127    Internal Reviewer 2 : 
       
   128    External Reviewer   :