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