Presentation is loading. Please wait.

Presentation is loading. Please wait.

Physics 114: Lecture 14 Linear Fitting

Similar presentations


Presentation on theme: "Physics 114: Lecture 14 Linear Fitting"— Presentation transcript:

1 Physics 114: Lecture 14 Linear Fitting
John F. Federici NJIT Physics Department

2 ANNOUNCEMENTS Course web page updated with grades.
HW #8 will be due a WEEK from today. MARCH 28th by 1pm. HW for WEEK 9 will be due March 30th by 1pm. The next Exam (full class Period) will be 2 weeks from today on APRIL 4th, in class. Exam will cover weeks 7-9 of the course, which includes new material we are covering this week. I will hand back Exam 1 today during class exercise.

3 Physics Cartoons

4 Reminder of Previous Results
Last time we showed that it is possible to fit a straight line of the form to any data by minimizing chi-square: This is called a ‘Least Squared Fit’ We found that we could solve for the parameters a and b that minimize the difference between the fitted line and the data (with errors si) as: Becomes N where Note, if errors si are all the same, they cancel, so can be ignored.

5 MatLAB Commands for Linear Fits
MatLAB has a “low level” routine called polyfit() that can be used to fit a linear function to a set of points (assumes all errors are equal): x = 0:0.1:2.5; y = randn(1,26)* *x; % Makes a slightly noisy linear set of points y = 3 + 2x p = polyfit(x,y,1) % Fits straight line and returns values of b and a in p % degree “1” polynomial p = % in this case, fit equation is y = x Plot the points, and overplot the fit using polyval() function. plot(x,y,'.'); hold on plot(x,polyval(p,x),'r') Here is the result. The points have a scatter of s = 0.3 around the fit, as we specified above.

6 Fits with Unequal Errors
MatLAB has another routine called glmfit() (generalized linear model regression) that can be used to specify weights (unequal errors). Say the errors are proportional to square-root of y (like Poisson): err = sqrt(y)/3; hold off errorbar(x,y,err, '.') % Plots y vs. x, with error bars Now calculate fit using glmfit() p = glmfit(x,y) % Does same as polyfit(x,y,1) (assumes equal errors) p = glmfit(x,y,’normal’,’weights’,err) % Allows specification of errors (weights), but must include ‘normal’ distribution type p = circshift(p,1) % Unfortunately, glmfit returns a,b instead of b,a as polyval() wants % circshift() has the effect of swapping the order of p elements hold on plot(x,polyval(p,x),’r’)

7 Matlab Curve Fitting APP
As with importing of data, there is a GUI App within MATLAB for curve fitting. It will handle polynomials AND arbitrary functions. For now, we will restrict ourselves to a 1 degree polynomial fit Lets use the data from the last example

8 Curve Fitting Tool

9 When you have the fit you want
You can transfer the figure to a “FIGURE” window to adjust labels, fonts, line types, etc. You can generate the Matlab code to do this fit [xData, yData, weights] = prepareCurveData( x, y, err ); ft = fittype( 'poly1' ); opts = fitoptions( 'Method', 'LinearLeastSquares' ); opts.Weights = weights; [fitresult, gof] = fit( xData, yData, ft, opts ); figure( 'Name', 'JFF-Fit' ); h = plot( fitresult, xData, yData ); NOTE: Uses FIT command in Matlab

10 Error Estimation We saw that when the errors of each point in the plot are the same, they cancel from the equations for the fit parameters a and b. If we do not, in fact, know the errors, but we believe that a linear fit is a correct fit to the points, then we can use the fit itself to estimate those errors. First, consider the residuals (deviations of the points from the fit, yi – y), calculated by resid = y - polyval(p,x) plot(x,resid) You can see that this is a random distribution with zero mean, as it should be. As usual, you can calculate the variance, where now there are two fewer degrees of freedom (m = 2) because we used the data to determine a and b. Indeed, typing std(resid) gives , which is close to the 0.3 we used. Can also be viewed in Curve fitting tool APP under View> Residual Plot

11 Chi-Square Probability
Recall that the value of c2 is This should be about equal to the number of degrees of freedom, n = N – 2 = 24 in this case. Since si is a constant 0.3, we can bring it out of the summation, and calculate sum(resid.^2)/0.3^2 ans = As we mentioned last time, it is often easier to consider the reduced chi-square, which is about unity for a good fit. In this case, cn2 = /24 = If we look this value up in table C.4 of the text, we find that P ~ 0.4 which means if we repeated the experiment multiple times, about 40% would be expected to have a larger chi-square.

12 Are you trying to pull a FAST one Prof. Federici?
You have been talking about c2 but the Curve Fitting Tool describes goodness of fit by R2!!!! R2 is ANOTHER measure of the “Goodness of a Fit”. Related to σ Is PERFECT fit I will let your Math Prof for Statistics explain to you why you may want to use one or another test of goodness…..

13 Does a ‘good’ fit in χ2 or R2 mean that we have the correct fit?
No….. Same data different fitting function IDEALLY, your fitting function should be based on your KNOWLEDGE of the PHYSICS (ie. Theoretically, what should be happening?) With four parameters I can fit an elephant, and with five I can make him wiggle his trunk. Attributed to John von Neumann by Enrico Fermi, as quoted by Freeman Dyson in "A meeting with Enrico Fermi" in Nature 427 (22 January 2004) p. 297

14 Uncertainties in the Parameters
We started out searching for a linear fit, , where a and b are the parameters of the fit. What are the uncertainties in these parameters? We saw in a previous lecture that errors due to a series of measurements propagate to the result for, e.g. a, according to Since we have the expressions for a and b as The partial derivatives are (note D is independent of yi)

15 Uncertainties in the Parameters
Inserting these into the expressions after some algebra (see text page 109), we have In the case of common si = s, we have For our example, this is calculated as del = 26*sum(x.^2)-sum(x)^2 siga = sum(x.^2)*0.3^2/del % gives sigb = 26*0.3^2/del % gives b=2.0661± a=2.880±0.013

16 Class Exersize


Download ppt "Physics 114: Lecture 14 Linear Fitting"

Similar presentations


Ads by Google