summaryrefslogtreecommitdiff
path: root/least_square_fit/script.rst
blob: acf6432cd05a48d13fb062ad9e5c80271e6aa482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
.. Objectives
.. ----------

.. Plotting a least square fit line

.. Prerequisites
.. -------------

..   1. Basic Plotting
..   2. Arrays
..   3. Loading data from files 
     
.. Author              : Nishanth Amuluru
   Internal Reviewer   : Punch
   External Reviewer   :
   Language Reviewer   : Bhanukiran
   Checklist OK?       : <put date stamp here, not OK> [2010-10-05]


Script
------

.. L1

{{{ Show the  first slide containing title, name of the production
team along with the logo of MHRD }}}

.. R1

Hello friends and welcome to the tutorial on 'Least Square Fit'.

.. L2

{{{ Show the slide containing objectives }}}

.. R2

At the end of this tutorial, you will be able to,

 1. Generate the least square fit line for a
    given set of points.

.. L3

{{{ Switch to the pre-requisite slide }}}

.. R3

Before beginning this tutorial,we would suggest you to complete the 
tutorial on "Using plot interactively", "Loading data from files"
and "Getting started with arrays".

Let us start this tutorial with the help of an example.

.. L4

{{{ Show the slide containing exercise 1 }}}

.. R4

Generate a least square fit line for l v/s t^2 using the data in the 
file 'pendulum.txt'.

.. L5

{{{ Open the file 'pendulum.txt' and show }}}

.. R5

We have an input file generated from a simple pendulum experiment.

It contains two columns of data. The first column is the length 
of the pendulum and the second is the corresponding time period 
of the pendulum.

As we know, the square of time period of a pendulum is directly 
proportional to its length, we shall plot l vs t^2 and verify this. 

To read the input file and parse the data, we are going to use the
loadtxt function.Switch to the terminal.

.. L6
::
 
    ipython -pylab
    l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True)
    l
    t

.. R6

We can see that l and t are two sequences containing length and time 
values correspondingly.

Let us first plot l vs t^2.

.. L7
::

    tsq = t * t
    plot(l, tsq, 'bo')

{{{ switch to the plot window }}}

.. R7

We can see that there is a visible linear trend, but we do not get a
straight line connecting them. We shall, therefore, generate a least
square fit line.

We will first generate the two matrices tsq and A. 
Then we will use the ``lstsq`` function to find the
values of m and c.

.. L8

{{{ Switch to the terminal }}}
::

    inter_mat = array((l, ones_like(l)))
    inter_mat

.. R8

let us now generate the A matrix with l values.
We shall first generate a 2 x 90 matrix with the first row as l values 
and the second row as ones. Then take the transpose of it. Type

.. R9

We see that we have intermediate matrix. Now we need the transpose. 
Type

.. L9
::

    A = inter_mat.T
    A

.. R10

Now we have both the matrices A and tsq. We only need to use 
the ``lstsq``
Type

.. L10
::

    result = lstsq(A, tsq)

.. R11

The result is a sequence of values. The first item in this sequence,
is the matrix p i.e., the values of m and c. 

.. L11
::

    m, c = result[0]
    m
    c

.. R12

Now that we have m and c, we need to generate the fitted values of t^2. 
Type

.. L12
::

    tsq_fit = m * l + c
    plot(l, tsq, 'bo')
    plot(l, tsq_fit, 'r')

.. R13

We get the least square fit of l vs t^2.

.. L13

.. L14

{{{ Show summary slide }}}

.. R14

This brings us to the end of the tutorial.In this tutorial,
we have learnt to,

 1. Generate a least square fit using matrices.
 #. Use the function ``lstsq()`` to generate a least square fit line.

.. L15

{{{Show self assessment questions slide}}}

.. R15

Here are some self assessment questions for you to solve

1. What does ones_like([1, 2, 3]) produce

   - array([1, 1, 1])
   - [1, 1, 1]
   - [1.0, 1.0, 1.0]
   - Error
   
2. The plot of ``u`` vs ``v`` is a bunch of scattered points that show a
   linear trend. How do you find the least square fit line 
   of ``u`` vs ``v``.

.. L16

{{{solution of self assessment questions on slide}}}

.. R16

And the answers,

1. The function ``ones_like([1, 2, 3])`` will generate 'array([1, 1, 1])'.

2. The following set of commands will produce the least square fit 
   line of ``u`` vs ``v``
::

    A = array(u, ones_like(u)).T
    result = lstsq(A, v)
    m, c = result[0]
    lst_line = m * u + c

.. L17

{{{ Show the thank you slide }}}

.. R17

Hope you have enjoyed this tutorial and found it useful.
Thank you!