25 |
25 |
26 As shown in the slide, we are first going to generate the two matrices tsq and |
26 As shown in the slide, we are first going to generate the two matrices tsq and |
27 A. Then we are going to use the =lstsq= function to find the values of m and c. |
27 A. Then we are going to use the =lstsq= function to find the values of m and c. |
28 |
28 |
29 To read the input file and parse the data, we are going to loadtxt function. |
29 To read the input file and parse the data, we are going to loadtxt function. |
30 Type:: |
30 Type |
|
31 :: |
31 |
32 |
32 data = loadtxt("/home/fossee/pendulum.txt") |
33 data = loadtxt("/home/fossee/pendulum.txt") |
33 data |
34 data |
34 |
35 |
35 As you can see, data is a sequence containing 90 records. Each record contains |
36 As you can see, data is a sequence containing 90 records. Each record contains |
38 all the time values. |
39 all the time values. |
39 |
40 |
40 Hence we have to use the unpack option with loadtxt. It unpacks the data into |
41 Hence we have to use the unpack option with loadtxt. It unpacks the data into |
41 sequences depending on the structure of data. |
42 sequences depending on the structure of data. |
42 |
43 |
43 Type:: |
44 Type |
|
45 :: |
44 |
46 |
45 l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) |
47 l, t = loadtxt("/home/fossee/pendulum.txt", unpack=True) |
46 l |
48 l |
47 t |
49 t |
48 |
50 |
49 We can see that l and t are two sequences containing length and time values |
51 We can see that l and t are two sequences containing length and time values |
50 correspondingly. |
52 correspondingly. |
51 |
53 |
52 Let us first plot l vs t^2. Type:: |
54 Let us first plot l vs t^2. Type |
|
55 :: |
53 |
56 |
54 tsq = t * t |
57 tsq = t * t |
55 plot(l, tsq, 'bo') |
58 plot(l, tsq, 'bo') |
56 |
59 |
57 |
60 |
59 |
62 |
60 We can see that there is a visible linear trend. |
63 We can see that there is a visible linear trend. |
61 |
64 |
62 let us now generate the A matrix with l values. |
65 let us now generate the A matrix with l values. |
63 We shall first generate a 2 x 90 matrix with the first row as l values and the |
66 We shall first generate a 2 x 90 matrix with the first row as l values and the |
64 second row as ones. Then take the transpose of it. Type:: |
67 second row as ones. Then take the transpose of it. Type |
|
68 :: |
65 |
69 |
66 inter_mat = array((l, ones_like(l))) |
70 inter_mat = array((l, ones_like(l))) |
67 inter_mat |
71 inter_mat |
68 |
72 |
69 We see that we have intermediate matrix. Now we need the transpose.Type:: |
73 We see that we have intermediate matrix. Now we need the transpose.Type |
|
74 :: |
70 |
75 |
71 A = inter_mat.T |
76 A = inter_mat.T |
72 A |
77 A |
73 |
78 |
74 Now we have both the matrices A and tsq. We only need to use the =lstsq= |
79 Now we have both the matrices A and tsq. We only need to use the =lstsq= |
75 Type:: |
80 Type |
|
81 :: |
76 |
82 |
77 result = lstsq(A, tsq) |
83 result = lstsq(A, tsq) |
78 |
84 |
79 The result is a sequence of values. The first item is the matrix p or in simple |
85 The result is a sequence of values. The first item is the matrix p or in simple |
80 words, the values of m and c. Hence, :: |
86 words, the values of m and c. Hence, |
|
87 :: |
81 |
88 |
82 m, c = result[0] |
89 m, c = result[0] |
83 m |
90 m |
84 c |
91 c |
85 |
92 |
86 Now that we have m and c, we need to generate the fitted values of t^2. Type:: |
93 Now that we have m and c, we need to generate the fitted values of t^2. Type |
|
94 :: |
87 |
95 |
88 tsq_fit = m * l + c |
96 tsq_fit = m * l + c |
89 plot(l, tsq, 'bo') |
97 plot(l, tsq, 'bo') |
90 plot(l, tsq_fit, 'r') |
98 plot(l, tsq_fit, 'r') |
91 |
99 |