statistics-script
changeset 46 34df59770550
parent 45 9d61db7bf2f4
equal deleted inserted replaced
45:9d61db7bf2f4 46:34df59770550
     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 
       
     3 This session is a continuation of the tutorial on Plotting Experimental data.
       
     4 
       
     5 We shall look at plotting experimental data using slightly advanced methods here. And then look into some statistical operations.
     2 
     6 
     3 In the previous tutorial we learnt how to read data from a file and plot it.
     7 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 the desired format.
     8 We used 'for' loops and lists to get data in the desired format.
     5 IPython -Pylab also provides a function called 'loadtxt' that can get us the same data in the desired format without much hustle.
     9 IPython -Pylab also provides a function called 'loadtxt' that can get us the same data in the desired format without much hustle.
     6 
    10 
     7 We shall use the same pendulum.txt file that we used in the previous session.
    11 We shall use the same pendulum.txt file that we used in the previous session.
     8 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
    12 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
     9 
    13 
    10 l, t = loadtxt('pendulum.txt', unpack=True)
    14 l, t = loadtxt('pendulum.txt', unpack=True)
    11 
    15 
    12 (unpack = True) will give us all of first column(length) in l and second column(time) in t
    16 (unpack = True) will give us all the data in the first column which is the length in l and all the data in the second column which is the time period in t. Here both l and t are arrays. We shall look into what arrays are in subsequent tutorials.
    13 
    17 
    14 to know more about loadtxt type 
    18 to know more about loadtxt type 
    15 
    19 
    16 loadtxt?
    20 loadtxt?
    17 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. 
    21 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. 
    18 
    22 
    19 Getting back to the problem, now to get squared values of t we can simply do
    23 Let's back to the problem, hit q to exit. Now to get squared values of t we can simply do
    20 
    24 
    21 tsq = t*t
    25 tsq = t*t
    22 
    26 
    23 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.
    27 Note that we don't 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 use of the 'for' loop.
    24 
    28 
    25 Let's now plot l vs tsq just as we did in the previous session
    29 Let's now plot l vs tsq just as we did in the previous session
    26 
    30 
    27 plot(l, tsq, 'o')
    31 plot(l, tsq, 'o')
    28 
    32 
    29 The basic equation for finding Time period of simple pendulum is:
    33 Let's continue with the pendulum expt to obtain the value of the acceleration due to gravity. The basic equation for finding Time period of simple pendulum is:
    30 
    34 
    31 T = 2*pi*sqrt(L/g)
    35 T = 2*pi*sqrt(L/g)
       
    36 
       
    37 rearranging this equation we obtain the value of as
       
    38 g = 4 pi squared into l by t squared.
    32 
    39 
    33 In this case we have the values of t and l already, so to find g value for each element we can simply use:
    40 In this case we have the values of t and l already, so to find g value for each element we can simply use:
    34 
    41 
    35 g = 4*pi^2*L/T^2
    42 g = 4*pi^2*L/T^2
    36 
    43 
    37 g is array with 90 elements, we can take the average of all these values to get the acceleration due to gravity('g') by
    44 g here is array, we can take the average of all these values to get the acceleration due to gravity('g') by
    38 
    45 
    39 print mean(g)
    46 print mean(g)
    40 
    47 
    41 Mean again is provided by pylab module which calculates the average of the given set of values.
    48 Mean again is provided by pylab module which calculates the average of the given set of values.
    42 There are other handy statistical functions available, such as median, mode, std(for standard deviation) etc.
    49 There are other handy statistical functions available, such as median, mode, std(for standard deviation) etc.