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 that we used in the tutorial on plotting from files. |
23 pendulum data that we used in the tutorial on plotting from files. |
24 Hope you have the file with you. |
|
25 |
24 |
26 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 |
27 working knowledge of arrays, plotting and file reading. |
26 working knowledge of arrays, plotting and file reading. |
28 |
27 |
29 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 |
30 squares of it's distance from the given set of points is |
29 squares of it's distance from the given set of points is |
31 minimum. We shall use the lstsq function to obtain the least |
30 minimum. |
32 squares fit curve. |
31 |
|
32 Previously, when we plotted the data from pendulum.txt we got a |
|
33 scatter plot of points as shown. |
33 |
34 |
34 In our example, we know that the length of the pendulum is |
35 In our example, we know that the length of the pendulum is |
35 proportional to the square of the time-period. Therefore, we |
36 proportional to the square of the time-period. But when we plot |
36 expect the least squares fit curve to be a straight line. |
37 the data using lines we get a distorted line as shown. What |
|
38 we expect ideally, is something like the redline in this graph. |
|
39 From the problem we know that L is directly proportional to T^2. |
|
40 But experimental data invariably contains errors and hence does |
|
41 not produce an ideal plot. The best fit curve for this data has |
|
42 to be a linear curve and this can be obtained by performing least |
|
43 square fit on the data set. We shall use the lstsq function to |
|
44 obtain the least squares fit curve. |
37 |
45 |
38 The equation of the line is of the form T^2 = mL+c. We have a set |
46 The equation of the line is of the form T^2 = mL+c. We have a set |
39 of values for L and the corresponding T^2 values. Using this, we |
47 of values for L and the corresponding T^2 values. Using this, we |
40 wish to obtain the equation of the straight line. |
48 wish to obtain the equation of the straight line. |
41 |
49 |
42 In matrix form the equation is represented as shown, |
50 In matrix form the equation is represented as shown, |
43 Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 as shown. |
51 Tsq = A.p where Tsq is an NX1 matrix, and A is an NX2 matrix as shown. |
44 And p is a 2X1 matrix of the slope and Y-intercept. In order to |
52 And p is a 2X1 matrix of the slope and Y-intercept. In order to |
45 obtain the least square fit curve we need to find the matrix p |
53 obtain the least square fit curve we need to find the matrix p |
46 |
54 |
47 Let's get started. As you can see, the file pendulum.txt |
55 Let's get started. As you can see, the file pendulum.txt |
48 is on our Desktop and hence we navigate to the Desktop by typing |
56 is on our Desktop and hence we navigate to the Desktop by typing |
63 manipulation |
71 manipulation |
64 |
72 |
65 A = array([l, ones_like(l)]) |
73 A = array([l, ones_like(l)]) |
66 |
74 |
67 As we have seen in a previous tutorial, ones_like() gives an array similar |
75 As we have seen in a previous tutorial, ones_like() gives an array similar |
68 in shape to the given array, in this case l, with all the elements as 1. |
76 in shape to the given array, in this case l, with all the elements as 1. |
|
77 Please note, this is how we create an array from an existing array. |
69 |
78 |
70 A = A.T to get the transpose of the given array. |
79 Let's now look at the shape of A. |
|
80 A.shape |
|
81 This is an 2X90 matrix. But we need a 90X2 matrix, so we shall transpose it. |
|
82 |
|
83 A = A.T |
71 |
84 |
72 Type A, to confirm that we have obtained the desired array. |
85 Type A, to confirm that we have obtained the desired array. |
73 A |
86 A |
74 Also note the shape of A. |
87 Also note the shape of A. |
75 A.shape |
88 A.shape |
86 |
99 |
87 To obtain the plot of the line, we simply use the equation of the |
100 To obtain the plot of the line, we simply use the equation of the |
88 line, we have noted before. T^2 = mL + c. |
101 line, we have noted before. T^2 = mL + c. |
89 |
102 |
90 Tline = coef[0]*l + coef[1] |
103 Tline = coef[0]*l + coef[1] |
91 plot(l, Tline) |
104 plot(l, Tline, 'r') |
92 |
105 |
93 Also, it would be nice to have a plot of the points. So, |
106 Also, it would be nice to have a plot of the points. So, |
94 plot(l, tsq, 'o') |
107 plot(l, tsq, 'o') |
95 |
108 |
96 This brings us to the end of this tutorial. In this tutorial, |
109 This brings us to the end of this tutorial. In this tutorial, |
97 you've learnt how to obtain a least squares fit curve for a given |
110 you've learnt how to obtain a least squares fit curve for a given |
98 set of points. |
111 set of points using lstsq. There are other curve fitting functions |
|
112 available in Pylab such as polyfit. |
99 |
113 |
100 Hope you enjoyed it. Thanks. |
114 Hope you enjoyed it. Thanks. |
101 |
115 |
102 *** Notes |
116 *** Notes |
103 |
117 |