Assignment 1(1)

2
Assignment 1 : Data Analysis This assignment will help you master the plotting and fitting techniques you will require to analyze your data from the lab sections. We are focusing on using python as a common data plotting and fitting tool. (There are others that work as well, such as Mathematica, Maple, MATLAB ... Excel does not handle errors properly, however). You will probably want to browse the folder on Connect and look at a basic online tutorial to learn the few basic commands you will need. Some basic scripts have been provided as a starting point. The easiest way to learn is to use one of these and figure out how a few lines work at a time. For each question with a plot, submit both the plot itself, and the code you used to do the fit and/or create the plot. 1. Practising python Include the scripts and appropriate input or output from each question. a) Write a python script that prints out the (integer) numbers from 1 to 100 Hint: look up how to use a “for loop” b) Modify the script to print out the sum of the numbers between 1 and 100 c) Write a python script that prompts the user to input a number, reads in the number, and prints out the (natural) log of the number. Hint: check the python function “raw_input” d) Write a python script that reads in data from a text file in which column 1 is a measurement and column 2 is the uncertainty in the measurement. The script should then write out another file in which column 1 is the log of the measurements, and column 2 is the appropriate uncertainty on the log of the measurement. Make up an input file of a few lines to test this. Include the input and output files you generated. Hint: you will need to look up the ‘open’ command for files, and write out a formatted string of the two numbers 2. Plot data Download the file “chisq.dat” from Connect into your own directory. Use any text editor (not excel or other spreadsheet programs) to examine the file. It should contain a list of 12 data quadruplets (x, y, dy1, dy2) where dy1 and dy2 are two different error values for y. The data represents the different measurements of the same quantity along with their 1 st reported experimental uncertainty (dy1). Using python, plot y(x). Be sure that you plot the error bars – for now we will use dy1. Note: You should start reading the data at line 5 or ignore commented lines. The file is not in the csv format, because the values are not separated by commas. Be sure that each axis is labelled properly. 3. Unweighted fit Use a calculator to find the mean value and standard deviation of the 12 data points by hand. Next, fit a constant (y=c) to the data to determine the mean value for all 12 reported y values, without using any uncertainty values. You can modify the fitting script on Connect (curve_fit.py) to do this. Add a text label inside the plot with the fit result (fitted mean, uncertainty on the fitted mean, and chi-square per degree of freedom (= goodness of fit)). Assignment 1 page 1 of 2

description

Neat

Transcript of Assignment 1(1)

Page 1: Assignment 1(1)

Assignment 1 : Data Analysis

This assignment will help you master the plotting and fitting techniques you will require to analyze your data from the lab sections. We are focusing on using python as a common data plotting and fitting tool. (There are others that work as well, such as Mathematica, Maple, MATLAB ... Excel does not handle errors properly, however). You will probably want to browse the folder on Connect and look at a basic online tutorial to learn the few basic commands you will need. Some basic scripts have been provided as a starting point. The easiest way to learn is to use one of these and figure out how a few lines work at a time. For each question with a plot, submit both the plot itself, and the code you used to do the fit and/or create the plot.

1. Practising python

Include the scripts and appropriate input or output from each question.

a) Write a python script that prints out the (integer) numbers from 1 to 100Hint: look up how to use a “for loop”

b) Modify the script to print out the sum of the numbers between 1 and 100 c) Write a python script that prompts the user to input a number, reads in the number, and prints

out the (natural) log of the number.Hint: check the python function “raw_input”

d) Write a python script that reads in data from a text file in which column 1 is a measurement and column 2 is the uncertainty in the measurement. The script should then write out another file in which column 1 is the log of the measurements, and column 2 is the appropriate uncertainty on the log of the measurement. Make up an input file of a few lines to test this. Include the input and output files you generated.Hint: you will need to look up the ‘open’ command for files, and write out a formatted string of the two numbers

2. Plot data

Download the file “chisq.dat” from Connect into your own directory. Use any text editor (not excel or other spreadsheet programs) to examine the file. It should contain a list of 12 data quadruplets (x, y, dy1, dy2) where dy1 and dy2 are two different error values for y. The data represents the different measurements of the same quantity along with their 1st reported experimental uncertainty (dy1). Using python, plot y(x). Be sure that you plot the error bars – for now we will use dy1.Note: You should start reading the data at line 5 or ignore commented lines. The file is not in the csv format, because the values are not separated by commas. Be sure that each axis is labelled properly.

3. Unweighted fit

Use a calculator to find the mean value and standard deviation of the 12 data points by hand. Next, fit a constant (y=c) to the data to determine the mean value for all 12 reported y values, without using any uncertainty values. You can modify the fitting script on Connect (curve_fit.py) to do this. Add a text label inside the plot with the fit result (fitted mean, uncertainty on the fitted mean, and chi-square per degree of freedom (= goodness of fit)).

Assignment 1 page 1 of 2

Page 2: Assignment 1(1)

Note: curve_fit does an unweighted fit if the given error is None (i.e., if sigma=None). To avoid plotting the error bars, you can just set the variable that stores the y-error to None (see comments). Make sure that you change the fit function to the constant function. An example of how to add a text box is given in the “GettingStarted.py” - see text() and figtext().

4. Weighted fit

Next do a weighted fit, by fitting the same model of a constant y to the data, but this time using the dy1 uncertainty values. Compare your result to the value found in step #3 above. Add the same information from this fit to the plot as before. Add a label indicating that this is a weighted fit and make sure that the error-bars are plotted.

5. Weighted fit with constant weights

Now repeat these steps using dy2 as your error vector (all weights =1). Label the plot as “all weights = 1.0”. Create a new error vector, dy3 = 2 * dy2 and then perform a fit using dy3 as the error vector. Plot the data with error-bars and fit with the label “all weights = 2.0” and include the text box with fit information as before.

6. Compare fit results

Compare all of your results by making a Table giving the average value, its uncertainty, and the chi2/dof (chi square / number of degrees of freedom) for each of the fits in steps 3, 4 and 5 above. What can you conclude about fitting when all the errors are equal to each other?

Assignment 1 page 2 of 2