Please turn in this assignment (and all future assignments for this course) by emailing them to me . Please use the subject of "Physics 222 HW5" for these assignments (if you use the link above, the subject will be added automatically). Please attach the source code to any programs that you write for this assignment. If you write more than one program, attach each separately. When answering the questions themselves feel free to answer directly in the body of your message (or attach your answers). I may take off points for assignments that are not sent to me in the correct form.
In this problem , you will write a program to read in data from a file, calculate a least square fit of the data, and then plot the resulting data and fit. The method of doing least squares fits is outlined in detail in Chapter 8 of Taylor's Introduction to Error Analysis. Most of you should have a copy. If you don't have a copy then borrow one.
For this problem, I will provide you with two data files (Data set 1 and Data set 2), though if you have data from previous lab work, feel free to use one data file that you provide and one that I provide. In the provided data file, the first line will be the title of the graph, the second line will be the x-axis label for the graph, the third line will be the y-axis label for the graph, and the fourth line will be the number of data points in the plot. The lines after that will be the x data, the y data, and the uncertainty in y. For this program you can assume that the x uncertainty is negligible.
As explained by Taylor, to fit an exponential, you take the natural log of your y data and fit that a line to log y versus x. The fit that you are doing is to the equation:
ln y = ln A + Bxwhich is equivalent to
y = AeBxwhere A and B are the fit parameters. To take into account the fact that the uncertainties will vary for y, we will use equations 8.37-8.39 on page 201 in Taylor's book to find A and B. Look there for the equations and how to use them. Note that in this case, as is explained on page 196 of Taylor, the uncertainty of y (σ) should be replaced by the uncertainty of the ln y which is σ/y (equation 8.34). Also note that in this case equation 8.37 will give you ln A, not A itself.
The calculations for this program require taking many sums, so I would recommend that you write a function to calculate sums. Your functions would take an array and and integer standing for its size, and return the sum of the componets of the array.
The calculations also require multiplying the elements of two arrays together to make a third array. I would recommend writing a function to do this as well. You would pass three arrays — the two that you will multiply and a third that will hold their product — and an integer holding the siae of the arrays to your function. Your function would then loop through and put the product of your first two arrays in the third array. As well as calculating the fit parameters A and B, you should calculate the uncertainties in A and B using equations 8.12 and 8.15-8.17. --> Your program should print out the values of the constant , as well as the plot described below.
Send me postscript copies of plots of both of your sets of data including line for your least squares fit, as well as the output specified above. For your plots, use the PLPlot library again (or GNUPlot if you prefer, but all the GNUPlot commands must be sent by your program). You can use the pls.line method of PLPlot to make the lines. You should plot the points with error bars - look at the plerrx and plerry functions "(man plerrx" and "man plerry"). For the plots, you can either make semi-log plots using the axis option of plenv (see "man plenv"). Or, you can use linear axes, but then explicitly plot the fits as exponentials.