|
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 data = loadtxt("/home/fossee/pendulum.txt") |
|
33 data |
|
34 |
|
35 As you can see, data is a sequence containing 90 records. Each record contains |
|
36 two values. The first is length and second is time period. But what we need is |
|
37 two sequences. One sequence containing all the length values and one containing |
|
38 all the time values. |
|
39 |
|
40 Hence we have to use the unpack option with loadtxt. It unpacks the data into |
|
41 sequences depending on the structure of data. |
|
42 |
|
43 Type:: |
|
44 |
|
45 l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) |
|
46 l |
|
47 t |
|
48 |
|
49 We can see that l and t are two sequences containing length and time values |
|
50 correspondingly. |
|
51 |
|
52 Let us first plot l vs t^2. Type:: |
|
53 |
|
54 tsq = t * t |
|
55 plot(l, tsq, 'bo') |
|
56 |
|
57 |
|
58 {{{ switch to the plot window }}} |
|
59 |
|
60 We can see that there is a visible linear trend. |
|
61 |
|
62 let us now generate the A matrix with l values. |
|
63 We shall first generate a 2 x 90 matrix with the first row as l values and the |
|
64 second row as ones. Then take the transpose of it. Type:: |
|
65 |
|
66 inter_mat = array((l, ones_like(l))) |
|
67 inter_mat |
|
68 |
|
69 We see that we have intermediate matrix. Now we need the transpose.Type:: |
|
70 |
|
71 A = inter_mat.T |
|
72 A |
|
73 |
|
74 Now we have both the matrices A and tsq. We only need to use the =lstsq= |
|
75 Type:: |
|
76 |
|
77 result = lstsq(A, tsq) |
|
78 |
|
79 The result is a sequence of values. The first item is the matrix p or in simple |
|
80 words, the values of m and c. Hence, :: |
|
81 |
|
82 m, c = result[0] |
|
83 m |
|
84 c |
|
85 |
|
86 Now that we have m and c, we need to generate the fitted values of t^2. Type:: |
|
87 |
|
88 tsq_fit = m * l + c |
|
89 plot(l, tsq, 'bo') |
|
90 plot(l, tsq_fit, 'r') |
|
91 |
|
92 We get the least square fit of l vs t^2 |
|
93 |
|
94 {{{ Pause here and try out the following exercises }}} |
|
95 |
|
96 %% 2 %% change the label on y-axis to "y" and save the lines of code |
|
97 accordingly |
|
98 |
|
99 {{{ continue from paused state }}} |
|
100 |
|
101 {{{ Show summary slide }}} |
|
102 |
|
103 This brings us to the end of the tutorial. |
|
104 we have learnt |
|
105 * how to use loadtxt to read files |
|
106 * how to generate a least square fit |
|
107 |
|
108 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
109 |
|
110 #[Nishanth]: Will add this line after all of us fix on one. |
|
111 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
112 |
|
113 Hope you have enjoyed and found it useful. |
|
114 Thankyou |
|
115 |
|
116 .. Author : Nishanth |
|
117 Internal Reviewer 1 : |
|
118 Internal Reviewer 2 : |
|
119 External Reviewer : |