Given a set of N data points, with associated errorsthe aim is to find the values of a and b such that is the “best” line through the data points. The “best” line is achieved by weighting points...


Given a set ofNdata points, with associated errorsthe aim is to find the values ofaandbsuch that





is the “best” line through the data points. The “best” line is achieved by weighting points with small errors more highly, and vice-versa.



This is accomplished by minimising the sum of the squares of the differences between the data points and this line, weighting the data such that those with a smaller error contribute more to the least squares sum we are trying to minimise. We assume that theare independent, and we have measured values for theand estimated values for the.The predicted value offor any givencan be found fromaandband so we aim to minimise the sum:





We can find expressions foraandbby differentiating the above with respect toaandband solving the resulting simultaneous equations. This yields:



and



where:





and.



The uncertainty on the fit can be evaluated by calculating the standard deviation of the estimates of the best-fit parametersaandb.These are given by:



and








Your task



You will write a program to calculate a weighted least-squares fit to a set of data. You will use your student ID to generate a unique set of errors on the data so your fit should be unique. The x and y values for the 5 data points to be used in your fit are noted in the table below:
























x



-2



0



2



4



6



y



2.1



2.4



2.5



3.5



4.2





1.Download from Minerva the coderandomnumbers.py. This code will generate a set of random errors. Use your student ID as a seed in the random number generator in this code, and run it to create personalised errorson each of the data points in the table above. The errors should range between the limits±0.5.


2.Create a file with 3 columns of data. From left to right the columns should store values for x, y and e. There should be 5 rows corresponding to each of the data points and their errors on y.


3.Write a program that will:


a.Read in your data file and place values for x, y and e into 3 1-dimensional numpy arrays.(2 marks).


b.Using these values of x, y and e, calculate values forp, q, r, s, tandΔ.
(2 marks).


c.Calculate the least squares parametersaandb.
(2 marks).


d.Calculate the uncertainty estimates foraandb().(2 marks).


e.Output to screen the values of the best fit parameters and their uncertainties.(2 marks).


f.Incorporate error checking code in your program to guard against possible problems such as only one data point being entered from the file, a zero weight being entered, the data file not being found or syntax errors in the data file.(3 marks).


g.Draw a suitable graph of the data and the fit. Use the matplotlib package to do this. Your graph should contain the data, axis labels, and appropriately-sized error bars on the data. It should also contain a line of best fit through the data (NOTE: this is not the same as using a line to join up individual points!). The easiest way to do this is to use your values of a and b to generate y-values for a set of corresponding x values and then to plot these (in addition to plotting the data). Add the fit results and the error estimates to a small panel appropriately positioned within the graph.(7 marks).


h.Create a flowchart describing your program.(2 marks).

Apr 29, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here