|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. A - Students and teachers from Science and engineering backgrounds |
|
5 B - |
|
6 C - |
|
7 D - |
|
8 |
|
9 .. Prerequisites |
|
10 .. ------------- |
|
11 |
|
12 .. 1. Basic Plotting |
|
13 2. Arrays |
|
14 |
|
15 .. Author : Nishanth Amuluru |
|
16 Internal Reviewer : |
|
17 External Reviewer : |
|
18 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
19 |
|
20 Script |
|
21 ------ |
|
22 |
|
23 Hello friends and welcome to the tutorial on Least Square Fit |
|
24 |
|
25 {{{ Show the slide containing title }}} |
|
26 |
|
27 {{{ Show the slide containing the outline slide }}} |
|
28 |
|
29 In this tutorial, we shall look at generating the least square fit line for a |
|
30 given set of points. |
|
31 |
|
32 First let us have a look at the problem. |
|
33 |
|
34 {{{ Show the slide containing problem statement. }}} |
|
35 |
|
36 We have an input file generated from a simple pendulum experiment. |
|
37 |
|
38 It contains two columns of data. The first column is the length of the |
|
39 pendulum and the second is the corresponding time period of the pendulum. |
|
40 |
|
41 As we know, the square of time period of a pendulum is directly proportional to |
|
42 its length, we shall plot l vs t^2 and verify this. |
|
43 |
|
44 #[Puneeth:] removed the explanation about loadtxt and unpack |
|
45 option. It's been done in another LO already. simple dependency |
|
46 should work? |
|
47 |
|
48 To read the input file and parse the data, we are going to use the |
|
49 loadtxt function. Type |
|
50 :: |
|
51 |
|
52 l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) |
|
53 l |
|
54 t |
|
55 |
|
56 We can see that l and t are two sequences containing length and time values |
|
57 correspondingly. |
|
58 |
|
59 Let us first plot l vs t^2. Type |
|
60 :: |
|
61 |
|
62 tsq = t * t |
|
63 plot(l, tsq, 'bo') |
|
64 |
|
65 {{{ switch to the plot window }}} |
|
66 |
|
67 #[Puneeth:] Moved explanation of least square fit here. seems more |
|
68 apt. |
|
69 |
|
70 We can see that there is a visible linear trend, but we do not get a |
|
71 straight line connecting them. We shall, therefore, generate a least |
|
72 square fit line. |
|
73 |
|
74 {{{ show the slide containing explanation on least square fit }}} |
|
75 |
|
76 As shown in the slide, we are first going to generate the two matrices |
|
77 tsq and A. Then we are going to use the ``lstsq`` function to find the |
|
78 values of m and c. |
|
79 |
|
80 let us now generate the A matrix with l values. |
|
81 We shall first generate a 2 x 90 matrix with the first row as l values and the |
|
82 second row as ones. Then take the transpose of it. Type |
|
83 :: |
|
84 |
|
85 inter_mat = array((l, ones_like(l))) |
|
86 inter_mat |
|
87 |
|
88 We see that we have intermediate matrix. Now we need the transpose. Type |
|
89 :: |
|
90 |
|
91 A = inter_mat.T |
|
92 A |
|
93 |
|
94 Now we have both the matrices A and tsq. We only need to use the ``lstsq`` |
|
95 Type |
|
96 :: |
|
97 |
|
98 result = lstsq(A, tsq) |
|
99 |
|
100 The result is a sequence of values. The first item in this sequence, |
|
101 is the matrix p i.e., the values of m and c. Hence, |
|
102 :: |
|
103 |
|
104 m, c = result[0] |
|
105 m |
|
106 c |
|
107 |
|
108 Now that we have m and c, we need to generate the fitted values of t^2. Type |
|
109 :: |
|
110 |
|
111 tsq_fit = m * l + c |
|
112 plot(l, tsq, 'bo') |
|
113 plot(l, tsq_fit, 'r') |
|
114 |
|
115 We get the least square fit of l vs t^2 |
|
116 |
|
117 {{{ Pause here and try out the following exercises }}} |
|
118 |
|
119 %% 2 %% change the label on y-axis to "y" and save the lines of code |
|
120 accordingly |
|
121 |
|
122 {{{ continue from paused state }}} |
|
123 |
|
124 {{{ Show summary slide }}} |
|
125 |
|
126 This brings us to the end of the tutorial. |
|
127 we have learnt |
|
128 |
|
129 * how to generate a least square fit |
|
130 |
|
131 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
132 |
|
133 #[Nishanth]: Will add this line after all of us fix on one. |
|
134 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
135 |
|
136 Hope you have enjoyed and found it useful. |
|
137 Thank you |
|
138 |
|
139 |