author | bhanu |
Mon, 15 Nov 2010 15:07:22 +0530 | |
changeset 505 | 394c3642cf9c |
parent 500 | 3bc06bc12826 |
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 |
499 | 14 |
Internal Reviewer : Punch |
237
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
15 |
External Reviewer : |
499 | 16 |
Language Reviewer : Bhanukiran |
500
3bc06bc12826
checklist not OK; 5 more short answer questions and 2 long answer questions
anand
parents:
499
diff
changeset
|
17 |
Checklist OK? : <put date stamp here, not OK> [2010-10-05] |
237
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
18 |
|
338
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
19 |
|
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
20 |
.. #[Puneeth: Add pre-requisites.] |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
21 |
|
237
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
22 |
Script |
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 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
25 |
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
|
26 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
27 |
{{{ Show the slide containing title }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
28 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
29 |
{{{ Show the slide containing the outline slide }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
30 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
31 |
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
|
32 |
given set of points. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
33 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
34 |
First let us have a look at the problem. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
35 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
36 |
{{{ Show the slide containing problem statement. }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
37 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
38 |
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
|
39 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
|
338
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
46 |
.. #[Puneeth:] removed the explanation about loadtxt and unpack |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
47 |
.. 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
|
48 |
.. should work? |
237
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
49 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
50 |
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
|
51 |
loadtxt function. Type |
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 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
54 |
l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
55 |
l |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
56 |
t |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
57 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
58 |
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
|
59 |
correspondingly. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
60 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
61 |
Let us first plot l vs t^2. Type |
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 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
64 |
tsq = t * t |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
65 |
plot(l, tsq, 'bo') |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
66 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
67 |
{{{ switch to the plot window }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
68 |
|
338
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
69 |
.. #[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
|
70 |
.. apt. |
237
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
71 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
72 |
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
|
73 |
straight line connecting them. We shall, therefore, generate a least |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
74 |
square fit line. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
75 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
76 |
{{{ show the slide containing explanation on least square fit }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
77 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
values of m and c. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
81 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
82 |
let us now generate the A matrix with l values. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
:: |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
86 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
87 |
inter_mat = array((l, ones_like(l))) |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
88 |
inter_mat |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
89 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
90 |
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
|
91 |
:: |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
92 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
93 |
A = inter_mat.T |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
94 |
A |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
95 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
96 |
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
|
97 |
Type |
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 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
100 |
result = lstsq(A, tsq) |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
101 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
:: |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
105 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
106 |
m, c = result[0] |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
107 |
m |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
108 |
c |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
109 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
110 |
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
|
111 |
:: |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
112 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
113 |
tsq_fit = m * l + c |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
114 |
plot(l, tsq, 'bo') |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
115 |
plot(l, tsq_fit, 'r') |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
116 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
117 |
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
|
118 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
119 |
{{{ Pause here and try out the following exercises }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
120 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
121 |
%% 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
|
122 |
accordingly |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
123 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
124 |
{{{ continue from paused state }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
125 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
126 |
{{{ Show summary slide }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
127 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
128 |
This brings us to the end of the tutorial. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
129 |
we have learnt |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
130 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
131 |
* how to generate a least square fit |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
132 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
133 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
134 |
|
338
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
135 |
.. #[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
|
136 |
.. 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
|
137 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
138 |
Hope you have enjoyed and found it useful. |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
139 |
Thank you |
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
140 |
|
6c203780bfbe
Converted lstsq to new template form
Nishanth <nishanth@fossee.in>
parents:
diff
changeset
|
141 |
|
338
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
142 |
.. |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
143 |
Local Variables: |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
144 |
mode: rst |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
145 |
indent-tabs-mode: nil |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
146 |
sentence-end-double-space: nil |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
147 |
fill-column: 75 |
e4a7d0924143
Reviewed Least Square Fit LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
279
diff
changeset
|
148 |
End: |