Download presentation
Presentation is loading. Please wait.
Published byDavid Rich Modified over 6 years ago
1
Physics 114: Lecture 14-a Linear Fitting Using Matlab
John F. Federici NJIT Physics Department
2
Physics Cartoons
3
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.
4
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.
5
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’)
6
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
7
Curve Fitting Tool
8
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
9
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
10
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 = 1.04. How ‘good’ of a fit is this value of reduced Chi-Square?
11
Chi-Square Probability
Section I.C – Evaluating the Goodness of Fit: ν is the # degrees of freedom The meaning of the χ2 probability above is that it is the probability in percent that a repeated experiment will return a chi-squared statistic greater than χ2. In this case, cn2 = /24 = and ν=24, the probability is 40%: 40% of repeated measurements will give a higher value of χ2
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, 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
Uncertainties in the Parameters
In Matlab, as we already mentioned, the goodness of fit is determined by MAXIMIZING the R-Square parameter. The uncertainties in the fitting parameters is calculated for you. Remember, keep typically TWO significant digits in the uncertainty.
17
How do you know if it is good fit?
Is R-squared value close to one? Is the relative error in the fitting parameters small or large? Are the residuals randomly distributed about zero? DOES IT LOOK LIKE a good fit?
18
Class Exersize A student measures the position of an object as a function of time and acquires the data set given in the online syllabus for this week. Import the experimental data into Matlab Use the Curve Fitting App to fit the data to a LINEAR function x=xo+V*t Extract the best fit parameters xo and V Note that in Matlab, the fitting parameters are given with 95% confidence bounds. Convert those bounds into an uncertainty and evaluate the best fit parameter in the form of xo=12.34±0.12 and V=12.34±0.12. Note that the uncertainty above is for 95% confidence. When you use one standard deviation as the measure of uncertainty, how would you convert 95% confidence uncertainty into one standard deviation of uncertainty?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.