lstsq.rst
changeset 324 4054b1a6392d
parent 323 e675f9208b91
parent 319 e8c02b3c51ac
child 325 51e61d26c802
equal deleted inserted replaced
323:e675f9208b91 324:4054b1a6392d
     1 .. Author              : Nishanth
       
     2    Internal Reviewer 1 : Puneeth
       
     3    Internal Reviewer 2 : 
       
     4    External Reviewer   :
       
     5 
       
     6 Hello friends and welcome to the tutorial on Least Square Fit
       
     7 
       
     8 {{{ Show the slide containing title }}}
       
     9 
       
    10 {{{ Show the slide containing the outline slide }}}
       
    11 
       
    12 In this tutorial, we shall look at generating the least square fit line for a
       
    13 given set of points.
       
    14 
       
    15 First let us have a look at the problem.
       
    16 
       
    17 {{{ Show the slide containing problem statement. }}}
       
    18 
       
    19 We have an input file generated from a simple pendulum experiment.
       
    20 
       
    21 It contains two columns of data. The first column is the length of the
       
    22 pendulum and the second is the corresponding time period of the pendulum.
       
    23 
       
    24 As we know, the square of time period of a pendulum is directly proportional to
       
    25 its length, we shall plot l vs t^2 and verify this. 
       
    26 
       
    27 #[Puneeth:] removed the explanation about loadtxt and unpack
       
    28  option. It's been done in another LO already. simple dependency 
       
    29  should work?
       
    30 
       
    31 To read the input file and parse the data, we are going to use the
       
    32 loadtxt function.  Type 
       
    33 ::
       
    34 
       
    35     l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
       
    36     l
       
    37     t
       
    38 
       
    39 We can see that l and t are two sequences containing length and time values
       
    40 correspondingly.
       
    41 
       
    42 Let us first plot l vs t^2. Type
       
    43 ::
       
    44 
       
    45     tsq = t * t
       
    46     plot(l, tsq, 'bo')
       
    47 
       
    48 {{{ switch to the plot window }}}
       
    49 
       
    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.
       
    62 
       
    63 let us now generate the A matrix with l values.
       
    64 We shall first generate a 2 x 90 matrix with the first row as l values and the
       
    65 second row as ones. Then take the transpose of it. Type
       
    66 ::
       
    67 
       
    68     inter_mat = array((l, ones_like(l)))
       
    69     inter_mat
       
    70 
       
    71 We see that we have intermediate matrix. Now we need the transpose. Type
       
    72 ::
       
    73 
       
    74     A = inter_mat.T
       
    75     A
       
    76 
       
    77 Now we have both the matrices A and tsq. We only need to use the ``lstsq``
       
    78 Type
       
    79 ::
       
    80 
       
    81     result = lstsq(A, tsq)
       
    82 
       
    83 The result is a sequence of values. The first item in this sequence,
       
    84 is the matrix p i.e., the values of m and c. Hence, 
       
    85 ::
       
    86 
       
    87     m, c = result[0]
       
    88     m
       
    89     c
       
    90 
       
    91 Now that we have m and c, we need to generate the fitted values of t^2. Type
       
    92 ::
       
    93 
       
    94     tsq_fit = m * l + c
       
    95     plot(l, tsq, 'bo')
       
    96     plot(l, tsq_fit, 'r')
       
    97 
       
    98 We get the least square fit of l vs t^2
       
    99 
       
   100 {{{ Pause here and try out the following exercises }}}
       
   101 
       
   102 %% 2 %% change the label on y-axis to "y" and save the lines of code
       
   103         accordingly
       
   104 
       
   105 {{{ continue from paused state }}}
       
   106 
       
   107 {{{ Show summary slide }}}
       
   108 
       
   109 This brings us to the end of the tutorial.
       
   110 we have learnt
       
   111 
       
   112  * how to generate a least square fit
       
   113 
       
   114 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   115 
       
   116 #[Nishanth]: Will add this line after all of us fix on one.
       
   117 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   118 
       
   119 Hope you have enjoyed and found it useful.
       
   120 Thank you
       
   121 
       
   122 Questions
       
   123 =========
       
   124 
       
   125  1. What does ones_like([1, 2, 3]) produce
       
   126 
       
   127    a. array([1, 1, 1])
       
   128    #. [1, 1, 1]
       
   129    #. [1.0, 1.0, 1.0]
       
   130    #. Error
       
   131    
       
   132  2. What does ones_like([1.2, 3, 4, 5]) produce
       
   133 
       
   134    a. [1.2, 3, 4, 5]
       
   135    #. array([1.0, 1.0, 1.0, 1.0])
       
   136    #. array([1, 1, 1, 1])
       
   137    #. array([1.2, 3, 4, 5])
       
   138 
       
   139  3. What is the shape of the