Presentation is loading. Please wait.

Presentation is loading. Please wait.

Physics 114: Lecture 16 Least Squares Fit to Arbitrary Functions

Similar presentations


Presentation on theme: "Physics 114: Lecture 16 Least Squares Fit to Arbitrary Functions"— Presentation transcript:

1 Physics 114: Lecture 16 Least Squares Fit to Arbitrary Functions
John F. Federici NJIT Physics Department

2 More Star Wars jokes….

3 More Philosophy For this class, we are more interested in ANALYZING the data then the details of exactly HOW to write all our own code from scratch. We will USE THE FORCE …… the Curve Fitting App…. so the details of HOW the fit is done is not important. However, you should understand the basic concept. So hang on for 5-6 slides while we get past the basic idea, and then we will fit arbitrary functions.

4 Nonlinear Least Squares Fitting
The general means to fit curves, surfaces, or higher dimensions to data relies on minimizing chi-square, but there is no closed-form method to calculate the coefficients. As usual, for a function y(x), say, the chi-square is The techniques we developed in Chapters 6 and 7 only work when the parameters in the function y(x) are linear, i.e. obey When this is not the case, i.e. when y(x) depends on products or powers in parameters), then the minimization of chi-square results in coupled equations that in general cannot be solved. We may sometimes be able to linearize it, as we saw last time, but generally we need to proceed by trial and error.

5 Searching Parameter Space
The basic approach could not be simpler, philosophically. The idea is to simply calculate a trial value of the function for a given set of values of parameters, calculate the chi-square, and repeat for a large number of parameters until you find the minimum chi-square. As a concrete example, consider a Gaussian function with parameters a, b and c: fit to a set of data yi = , , , , , , , with equal errors si = s = 0.2. Try a set of parameters a = 1, b = 1, c = 1. With these parameters, the function y(x) gives y(x) = , , , , , , The reduced chi-square is then A plot of the curves yi and y(x) are shown at right.

6 Searching Parameter Space (cont’d)
Now we simply try other sets of parameters and continue to calculate chi-square, attempting to find a minimum chi-square such that the reduced chi-square is about 1. The plot shows the result of trying other values of a from 1 to 3, stepping by 0.5, while keeping b = 1 and c = 1. The reduced chi-square for this set of five curves is Looks like a = 2.5, b = 1, c = 1 is the best so far. Now we change one of the other parameters, by say, setting b = 1.2, and varying a again, with c = 1. The plot is shown at right, and the chi-square for this new set of curves is Looks like a = 2.5, b = 1.2, c = 1 is the best so far.

7 Searching Parameter Space (cont’d)
If we repeat this 3 more times, increasing b by 0.2 each time, we end up with the following 25 values of reduced chi-square: c=1 Now let’s try c=1.2, and repeat the whole thing again: c=1.2 b=1.0 b=1.2 b=1.4 b=1.6 b=1.8 a= a= a= a= a= b=1.0 b=1.2 b=1.4 b=1.6 b=1.8 a= a= a= a= a=

8 Searching Parameter Space (cont’d)
After additional trial c values, we find a best fit of a = 2.0, b = 1.2, c = Here is the final fit with these values. But note that once we have the parameters, we can plot a much smoother Gaussian through the points by evaluating it at more x values. Note that we could be even more precise by stepping with smaller steps.

9 Searching Parameter Space (cont’d)
Here is the reduced chi-square shown for smaller steps, covering the same range of a and b, but now as a 10 x 10 plot, and shown as an image. The minimum chi-square is in a dip on a surface in “parameter space”. cn2= 1.29

10 What is the Point Prof. Federici?
The point LUKE is that Matlab invokes an algorithm to search the parameter space for the set of parameters that minimizes R2 or χ2. Whether the algorithm is a very simple (but time consuming) method of plotting out a grid or using calculus to locate a local minimum (set derivatives equal to zero), the essential concept is that one is searching in parameter space for a local minimum in 2, 3, 4 or N dimensional space. So, now that you understand the basic concept, let’s implement it in MATLAB.

11 Example 1 Everyone turn on their Laptops, and type in the commands as I do to follow along. >> d = [ ]; >> theta = [ ]; >> plot(theta,d,'.') We will now fit this data with the following equation Where n is the fitting parameter

12 CURVE FITTING TOOL Choose x data to be “theta” Choose y data to be “d”
Choose CUSTOM EQUATION and type in the equation you will be using. 5*sin(x)*(1-cos(x)/(n^2-(sin(x))^2)^(1/2)) Click on AUTO FIT.. The function should then try to fit. You MIGHT GET an ERROR. Open up FIT OPTIONS

13 Example 1 – cont. Specific algorithm which is used to determine best fit parameters

14 Example 1 – Cont. For your fitting parameters, you can change the upper and lower limit for the fitting parameter and also change your initial ‘guess’ as to the final answer. Change the LOWER limit to be 1. The quantity which you are solving for is a refractive index which is typically 1 or larger. Sometimes when you do the fitting you need to provide a REASONABLE guess for the fitting parameter. Remember, MATLAB is searching parameter space for a minimum. You want to make sure that it finds a GLOBAL minimum and not just a localized minimum.

15 GOOD FITS! General model:
f(x) = 5*sin(x)*(1-cos(x)/(n^2-(sin(x))^2)^(1/2)) Coefficients (with 95% confidence bounds): n = (1.425, 1.741) Goodness of fit: SSE: R-square: Adjusted R-square: RMSE: Note that the nonlinear fit gives you the best fit parameter with 95% (ie. 2σ) uncertainty.

16 Example 2 – Interferometry
Light waves INTERFERE through PRINCIPLE OF SUPERPOSITION. The NET wave is the algebraic sum of the two individual waves. Depending on the phase relation between the waves (ie. whether the peaks and valleys line up or anti-align) one can achieve HIGH power output or LOW power input.

17 Experimental Setup PZT: lead (Pb) zirconia (Zr) Titanate (Ti) THICKNESS of PZT is proportional to the voltage applied to the PZT.

18 Experimental Setup Measured Light Intensity by Photo Detector
Voltage applied to PZT

19 Experimental Setup Measured Light Intensity by Photo Detector
Voltage applied to PZT

20 Analysis of data The NUMBER Of interference fringes which pass by the detector is related to HOW FAR the PZT stack moves. From the Data, the goal is to EXTRACT from the data HOW FAR does the Mirror on the PZT material move. Wavelength of the Light (use 650nm) We calculate N from the best fit parameter A1 Length of time that mirror moving

21 Glitches in the data Why are there glitches in the data?

22 Voltage proportional to Thickness of PZT – Position of Mirror
Mirror moving Forward Mirror moving Backward Mirror moving Forward Mirror moving Forward Mirror moving Backward Time

23 Voltage proportional to Thickness of PZT – Position of Mirror
Mirror moving Forward Mirror moving Backward Mirror moving Forward Mirror moving Forward Mirror moving Backward Time

24 Removing Data from Fitting
Fit data ONLY when Mirror is moving at CONSTANT velocity in ONE direction!

25 Class EXERCISE Download Lecture 16 Class Exercise data file.
Create a plot that looks like the figure below to make sure that you have the data correct Use the CURVE Fitting Tool with ‘Custom Equation’ to fit the data. y = a*sin(b*x+c)+d EXCLUDE the data outside of the specific time range so that you ONLY fit data for the mirror moving at constant velocity in one direction. choose the tab for “Tools…Exclude Outliers” or you could instead use the “Tools…Exclude by Rule” to limit the data As needed, edit the FIT OPTIONS for starting points and limits for parameters a, b, c and d. so that you get a reasonably good fit. You can estimate these values using the plotted data which looks like the graph above. With best fit parameters, use equations on Slide 20 to calculate how far mirror has moved.


Download ppt "Physics 114: Lecture 16 Least Squares Fit to Arbitrary Functions"

Similar presentations


Ads by Google