1 Hello friends and welcome to the third tutorial in the series of tutorials on "Python for scientific computing." |
1 Hello friends and welcome to the third tutorial in the series of tutorials on "Python for scientific computing." |
2 |
2 |
3 In the previous tutorial we learnt how to read data from a file and plot the data |
3 In the previous tutorial we learnt how to read data from a file and plot it. |
4 We used 'for' loops and lists to get data in desired format. |
4 We used 'for' loops and lists to get data in the desired format. |
5 IPython -Pylab also provides with a function 'loadtxt' which can get us data without much hustle. |
5 IPython -Pylab also provides a function called 'loadtxt' that can get us the same data in the desired format without much hustle. |
6 |
6 |
7 We know that, pendulum.txt contains two columns, with length being first and time period is second column, so to get both columns in two separate variables we type |
7 We know that, pendulum.txt contains two columns, with length being first and time period is second column, so to get both columns in two separate variables we type |
8 |
8 |
9 l, t = loadtxt('pendulum.txt', unpack=True) |
9 l, t = loadtxt('pendulum.txt', unpack=True) |
10 |
10 |
11 (unpack = True)? will give us all of first column(length) in l and second column(time) in t |
11 (unpack = True) will give us all of first column(length) in l and second column(time) in t |
12 |
12 |
13 to get more help type |
13 to know more about loadtxt type |
14 |
14 |
15 loadtxt? |
15 loadtxt? |
16 This is really powerful tool to load data directly from files which are well structured and formatted. It supports many features like getting particular columns. |
16 This is a really powerful tool to load data directly from files which are well structured and formatted. It supports many features like getting selected columns only, or skipping rows. |
17 now to get squared values of t we can simply do |
17 |
|
18 Getting back to the problem, now to get squared values of t we can simply do |
18 |
19 |
19 tsq = t*t |
20 tsq = t*t |
20 |
21 |
21 and we dont have to use for loop anymore. This is benefit of arrays. If we try to something similar to lists we cant escape a 'for' loop. |
22 Note we dont have to use the 'for' loop anymore. This is the benefit of arrays. If we try to do the something similar using lists we won't be able to escape the 'for' loop. |
22 |
23 |
23 Now to plot l vs tsq is same as we did in previous session |
24 Let's now plot l vs tsq just as we did in the previous session |
24 |
25 |
25 plot(l, tsq, 'o') |
26 plot(l, tsq, 'o') |
26 |
27 |
27 Additionally the basic equation for finding Time period of simple pendulum we use equation: |
28 The basic equation for finding Time period of simple pendulum is: |
28 |
29 |
29 T = 2*pi*sqrt(L/g) |
30 T = 2*pi*sqrt(L/g) |
30 |
31 |
31 In our case we have t and l already, so to find g value for each element we can simply use: |
32 In this case we have the values of t and l already, so to find g value for each element we can simply use: |
32 |
33 |
33 g = 4*pi^2*L/T^2 |
34 g = 4*pi^2*L/T^2 |
34 |
35 |
35 g is array with 90 elements, so we take average of all these values to get acceleration due to gravity('g') by |
36 g is array with 90 elements, we can take the average of all these values to get the acceleration due to gravity('g') by |
36 |
37 |
37 print mean(g) |
38 print mean(g) |
38 |
39 |
39 Mean again is provided by pylab module which calculates average of given set of values. |
40 Mean again is provided by pylab module which calculates the average of the given set of values. |
40 There are other handy statistical functions available, such as median, mode, std(for standard deviation) etc. |
41 There are other handy statistical functions available, such as median, mode, std(for standard deviation) etc. |
41 |
42 |
42 So in this small session we have covered 'better' way of loading data from text files. |
43 In this small session we have covered 'better' way of loading data from text files. |
43 Why arrays are better choice then lists in some cases, and how they are helpful during some mathematical operations. |
44 Why arrays are a better choice than lists in some cases, and how they are more helpful with mathematical operations. |
44 |
45 |
45 Thank you! |
46 Hope it was useful to you. Thank you! |
46 ----------------------------------------------------------------------------------------------------------- |
47 ----------------------------------------------------------------------------------------------------------- |
47 In this tutorial we shall learn how to compute statistics using python. |
48 In this tutorial we shall learn how to compute statistics using python. |
48 We also shall learn how to represent data in the form of pie charts. |
49 We also shall learn how to represent data in the form of pie charts. |
49 |
50 |
50 Let us start with the most basic need in statistics, the mean. |
51 Let us start with the most basic need in statistics, the mean. |