least-squares.org
changeset 2 008c0edc6eac
child 75 3a94917224e9
equal deleted inserted replaced
1:f48921e39df1 2:008c0edc6eac
       
     1 * Least Squares Fit
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* What do we want to do? Why?
       
     5 ********* What's a least square fit?
       
     6 ********* Why is it useful?
       
     7 ******* How are we doing it?
       
     8 ******* Arsenal Required
       
     9 ********* working knowledge of arrays
       
    10 ********* plotting
       
    11 ********* file reading
       
    12 ***** Procedure
       
    13 ******* The equation (for a single point)
       
    14 ******* It's matrix form
       
    15 ******* Getting the required matrices
       
    16 ******* getting the solution
       
    17 ******* plotting
       
    18 *** Script
       
    19     Welcome. 
       
    20     
       
    21     In this tutorial we shall look at obtaining the least squares fit
       
    22     of a given data-set. For this purpose, we shall use the same
       
    23     pendulum data used in the tutorial on plotting from files.
       
    24 
       
    25     To be able to follow this tutorial comfortably, you should have a
       
    26     working knowledge of arrays, plotting and file reading. 
       
    27 
       
    28     A least squares fit curve is the curve for which the sum of the
       
    29     squares of it's distance from the given set of points is
       
    30     minimum. We shall use the lstsq function to obtain the least
       
    31     squares fit curve. 
       
    32 
       
    33     In our example, we know that the length of the pendulum is
       
    34     proportional to the square of the time-period. Therefore, we
       
    35     expect the least squares fit curve to be a straight line. 
       
    36 
       
    37     The equation of the line is of the form T^2 = mL+c. We have a set
       
    38     of values for L and the corresponding T^2 values. Using this, we
       
    39     wish to obtain the equation of the straight line. 
       
    40 
       
    41     In matrix form...
       
    42     {Show a slide here?}
       
    43     
       
    44     We have already seen (in a previous tutorial), how to read a file
       
    45     and obtain the data set. We shall quickly get the required data
       
    46     from our file. 
       
    47 
       
    48     In []: l = []
       
    49     In []: t = []
       
    50     In []: for line in open('pendulum.txt'):
       
    51     ....     point = line.split()
       
    52     ....     l.append(float(point[0]))
       
    53     ....     t.append(float(point[1]))
       
    54     ....
       
    55     ....
       
    56 
       
    57     Since, we have learnt to use arrays and know that they are more
       
    58     efficient, we shall use them. We convert the lists l and t to
       
    59     arrays and calculate the values of time-period squared. 
       
    60 
       
    61     In []: l = array(l)
       
    62     In []: t = array(t)
       
    63     In []: tsq = t*t
       
    64 
       
    65     Now we shall obtain A, in the desired form using some simple array
       
    66     manipulation 
       
    67 
       
    68     In []: A = array([l, ones_like(l)])
       
    69     In []: A = A.T
       
    70     
       
    71     Type A, to confirm that we have obtained the desired array. 
       
    72     In []: A
       
    73     Also note the shape of A. 
       
    74     In []: A.shape
       
    75 
       
    76     We shall now use the lstsq function, to obtain the coefficients m
       
    77     and c. lstsq returns a lot of things along with these
       
    78     coefficients. Look at the documentation of lstsq, for more
       
    79     information. 
       
    80     In []: result = lstsq(A,tsq)
       
    81 
       
    82     We take put the required coefficients, which are the first thing
       
    83     in the list of things that lstsq returns, into the variable coef. 
       
    84     In []: coef = result[0]
       
    85 
       
    86     To obtain the plot of the line, we simply use the equation of the
       
    87     line, we have noted before. T^2 = mL + c. 
       
    88 
       
    89     In []: Tline = coef[0]*l + coef[1]
       
    90     In []: plot(l, Tline)
       
    91 
       
    92     Also, it would be nice to have a plot of the points. So, 
       
    93     In []: plot(l, tsq, 'o')
       
    94 
       
    95     This brings us to the end of this tutorial. In this tutorial,
       
    96     you've learnt how to obtain a least squares fit curve for a given
       
    97     set of points. 
       
    98 
       
    99     Hope you enjoyed it. Thanks. 
       
   100 
       
   101 *** Notes
       
   102