lstsq.rst
changeset 149 b9ae88095ade
parent 139 9e67c055a413
child 195 e8a251048213
equal deleted inserted replaced
148:60a4616dbf55 149:b9ae88095ade
       
     1 Hello friends and welcome to the tutorial on Least Square Fit
       
     2 
       
     3 {{{ Show the slide containing title }}}
       
     4 
       
     5 {{{ Show the slide containing the outline slide }}}
       
     6 
       
     7 In this tutorial, we shall look at generating the least square fit line for a
       
     8 given set of points.
       
     9 
       
    10 First let us have a look at the problem.
       
    11 
       
    12 {{{ Show the slide containing problem statement. }}}
       
    13 
       
    14 We have an input file generated from a simple pendulum experiment.
       
    15 
       
    16 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.
       
    18 
       
    19 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.
       
    21 
       
    22 If it is not linear, we shall generate a least square fit line.
       
    23 
       
    24 {{{ show the slide containing explanation on least square fit }}}
       
    25 
       
    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 ::
       
    46 
       
    47     l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
       
    48     l
       
    49     t
       
    50 
       
    51 We can see that l and t are two sequences containing length and time values
       
    52 correspondingly.
       
    53 
       
    54 Let us first plot l vs t^2. Type
       
    55 ::
       
    56 
       
    57     tsq = t * t
       
    58     plot(l, tsq, 'bo')
       
    59 
       
    60 
       
    61 {{{ switch to the plot window }}}
       
    62 
       
    63 We can see that there is a visible linear trend.
       
    64 
       
    65 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
       
    67 second row as ones. Then take the transpose of it. Type
       
    68 ::
       
    69 
       
    70     inter_mat = array((l, ones_like(l)))
       
    71     inter_mat
       
    72 
       
    73 We see that we have intermediate matrix. Now we need the transpose.Type
       
    74 ::
       
    75 
       
    76     A = inter_mat.T
       
    77     A
       
    78 
       
    79 Now we have both the matrices A and tsq. We only need to use the =lstsq=
       
    80 Type
       
    81 ::
       
    82 
       
    83     result = lstsq(A, tsq)
       
    84 
       
    85 The result is a sequence of values. The first item is the matrix p or in simple
       
    86 words, the values of m and c. Hence, 
       
    87 ::
       
    88 
       
    89     m, c = result[0]
       
    90     m
       
    91     c
       
    92 
       
    93 Now that we have m and c, we need to generate the fitted values of t^2. Type
       
    94 ::
       
    95 
       
    96     tsq_fit = m * l + c
       
    97     plot(l, tsq, 'bo')
       
    98     plot(l, tsq_fit, 'r')
       
    99 
       
   100 We get the least square fit of l vs t^2
       
   101 
       
   102 {{{ Pause here and try out the following exercises }}}
       
   103 
       
   104 %% 2 %% change the label on y-axis to "y" and save the lines of code
       
   105         accordingly
       
   106 
       
   107 {{{ continue from paused state }}}
       
   108 
       
   109 {{{ Show summary slide }}}
       
   110 
       
   111 This brings us to the end of the tutorial.
       
   112 we have learnt
       
   113 
       
   114  * how to use loadtxt to read files
       
   115  * how to generate a least square fit
       
   116 
       
   117 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   118 
       
   119 #[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
       
   121 
       
   122 Hope you have enjoyed and found it useful.
       
   123 Thankyou
       
   124  
       
   125 .. Author              : Nishanth
       
   126    Internal Reviewer 1 : 
       
   127    Internal Reviewer 2 : 
       
   128    External Reviewer   :