least-squares.org
changeset 75 3a94917224e9
parent 2 008c0edc6eac
child 76 6dfdb6fc9d80
equal deleted inserted replaced
74:12729a0a1893 75:3a94917224e9
    18 *** Script
    18 *** Script
    19     Welcome. 
    19     Welcome. 
    20     
    20     
    21     In this tutorial we shall look at obtaining the least squares fit
    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
    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.
    23     pendulum data that we used in the tutorial on plotting from files.
    24 
    24 
    25     To be able to follow this tutorial comfortably, you should have a
    25     To be able to follow this tutorial comfortably, you should have a
    26     working knowledge of arrays, plotting and file reading. 
    26     working knowledge of arrays, plotting and file reading. 
    27 
    27 
    28     A least squares fit curve is the curve for which the sum of the
    28     A least squares fit curve is the curve for which the sum of the
    40 
    40 
    41     In matrix form...
    41     In matrix form...
    42     {Show a slide here?}
    42     {Show a slide here?}
    43     
    43     
    44     We have already seen (in a previous tutorial), how to read a file
    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
    45     and obtain the data set. Let's quickly get the required data
    46     from our file. 
    46     from our file. 
    47 
    47 
    48     In []: l = []
    48     In []: l = []
    49     In []: t = []
    49     In []: t = []
    50     In []: for line in open('pendulum.txt'):
    50     In []: for line in open('pendulum.txt'):
    77     and c. lstsq returns a lot of things along with these
    77     and c. lstsq returns a lot of things along with these
    78     coefficients. Look at the documentation of lstsq, for more
    78     coefficients. Look at the documentation of lstsq, for more
    79     information. 
    79     information. 
    80     In []: result = lstsq(A,tsq)
    80     In []: result = lstsq(A,tsq)
    81 
    81 
    82     We take put the required coefficients, which are the first thing
    82     We extract the required coefficients, which is the first element
    83     in the list of things that lstsq returns, into the variable coef. 
    83     in the list of things that lstsq returns, and store them into the variable coef. 
    84     In []: coef = result[0]
    84     In []: coef = result[0]
    85 
    85 
    86     To obtain the plot of the line, we simply use the equation of the
    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. 
    87     line, we have noted before. T^2 = mL + c. 
    88 
    88