least-squares.org
changeset 81 2eff0ebac2dc
parent 76 6dfdb6fc9d80
--- a/least-squares.org	Sat Apr 17 12:50:42 2010 +0530
+++ b/least-squares.org	Sat Apr 17 15:51:43 2010 +0530
@@ -21,26 +21,34 @@
     In this tutorial we shall look at obtaining the least squares fit
     of a given data-set. For this purpose, we shall use the same
     pendulum data that we used in the tutorial on plotting from files.
-    Hope you have the file with you. 
 
     To be able to follow this tutorial comfortably, you should have a
     working knowledge of arrays, plotting and file reading. 
 
     A least squares fit curve is the curve for which the sum of the
     squares of it's distance from the given set of points is
-    minimum. We shall use the lstsq function to obtain the least
-    squares fit curve. 
+    minimum. 
+
+    Previously, when we plotted the data from pendulum.txt we got a 
+    scatter plot of points as shown. 
 
     In our example, we know that the length of the pendulum is
-    proportional to the square of the time-period. Therefore, we
-    expect the least squares fit curve to be a straight line. 
+    proportional to the square of the time-period. But when we plot
+    the data using lines we get a distorted line as shown. What
+    we expect ideally, is something like the redline in this graph. 
+    From the problem we know that L is directly proportional to T^2.
+    But experimental data invariably contains errors and hence does
+    not produce an ideal plot. The best fit curve for this data has 
+    to be a linear curve and this can be obtained by performing least
+    square fit on the data set. We shall use the lstsq function to
+    obtain the least squares fit curve. 
 
     The equation of the line is of the form T^2 = mL+c. We have a set
     of values for L and the corresponding T^2 values. Using this, we
     wish to obtain the equation of the straight line. 
 
     In matrix form the equation is represented as shown, 
-    Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 as shown.
+    Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 matrix as shown.
     And p is a 2X1 matrix of the slope and Y-intercept. In order to 
     obtain the least square fit curve we need to find the matrix p
 
@@ -65,9 +73,14 @@
     A = array([l, ones_like(l)])
 
     As we have seen in a previous tutorial, ones_like() gives an array similar
-    in shape to the given array, in this case l, with all the elements as 1.
+    in shape to the given array, in this case l, with all the elements as 1. 
+    Please note, this is how we create an array from an existing array.
 
-    A = A.T to get the transpose of the given array.
+    Let's now look at the shape of A. 
+    A.shape
+    This is an 2X90 matrix. But we need a 90X2 matrix, so we shall transpose it.
+
+    A = A.T
     
     Type A, to confirm that we have obtained the desired array. 
     A
@@ -88,14 +101,15 @@
     line, we have noted before. T^2 = mL + c. 
 
     Tline = coef[0]*l + coef[1]
-    plot(l, Tline)
+    plot(l, Tline, 'r')
 
     Also, it would be nice to have a plot of the points. So, 
     plot(l, tsq, 'o')
 
     This brings us to the end of this tutorial. In this tutorial,
     you've learnt how to obtain a least squares fit curve for a given
-    set of points. 
+    set of points using lstsq. There are other curve fitting functions
+    available in Pylab such as polyfit.
 
     Hope you enjoyed it. Thanks.