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