OPSE 301: Lab13 Data Analysis – Fitting Data to Arbitrary Functions John F. Federici NJIT Physics Department
Star Wars jokes….
Philosophy For this class, we are focusing on using Matlab… USE THE FORCE …… the Curve Fitting App…. so the details of HOW the fit is done is not important. For Physics Majors, you learned HOW the fit is done in Phys 114.
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 so that one MAXIMIZES the ‘goodness of the fit’ or equivalently minimizes the ERROR in the fit of the data to a theoretical function. In terms of statistical analysis, we use a ‘correlation coefficient’ R2 in Matlab to measure the ‘goodness’ of the fit. Values of R=1 is a perfect fit. Values of R significantly less then 1 indicate a ‘bad’ fit. You can also get a feeling for the ‘goodness’ of fit based on how closely the data points follow the theoretical curve. Let’s do a conceptual example.
Let’s look at a fit to a linear function Here are several plots with lines through the points. Which one do you think is the best fit? It is surprisingly easy to see by eye which one fits best, but what does your brain do to determine this? It is maximizing R the ‘goodness’ of the fit. So, now that you understand the basic concept, let’s implement it in MATLAB.
Example Lab 13 – 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.
Experimental Setup PZT: lead (Pb) zirconia (Zr) Titanate (Ti) THICKNESS of PZT is proportional to the voltage applied to the PZT.
Experimental Setup Measured Light Intensity by Photo Detector Voltage applied to PZT
Experimental Setup Measured Light Intensity by Photo Detector Voltage applied to PZT
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
Why this functional form? Amplitude Offset Initial Phase Delay between two interfering beams
Why this functional form? Assume that Voltage on PZT is proportional to distance PZT moves For a RAMP voltage, VELOCITY V of mirror on PZT is constant
Glitches in the data Why are there glitches in the data?
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
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
Removing Data from Fitting Fit data ONLY when Mirror is moving at CONSTANT velocity in ONE direction!
Roughly what value of N should you get? 1 2 3 4 5 N~5 After you do a fit and calculate N, CHECK to see if it makes sense. If it DOES NOT, there is something wrong with the fit.
OK, Let’s get on with the fitting Everyone turn on their Laptops, Power up MATLAB, and type in the commands as I do to follow along. IMPORT DATA: The first step is to import your data from the EXCEL file which you created in class for Lab 13. For practice, use the data which I put on the COURSE web page within the syllabus for Week 9. Importing the data is SIMILAR to what you did in Week 1 Matlab assignment Download the Lab 13 data set from the web site, in text format. Use the IMPORT DATA tool to read the data into Matlab. Y The first column of data is the ‘Time’ data. The second column of data is the ‘Voltage’ data for channel 1 (Interferometer Output) The second column of data is the Voltage data for the applied voltage to the PZT. Let’s use Matlab to see how its done. Define time data as variable ‘time’. Define photodetector voltage (channel 1) as value ‘output’. Define voltage applied to PZT as variable ‘PZT’.
Importing Data
Importing data
Importing Data
CURVE FITTING TOOL Let’s fit the photodetector data. Open the curve fitting tool. Use the CURVE Fitting Tool with ‘Custom Equation’ to fit the data. Choose FIT OPTION METHOD to be NONLINEAR LEAST SQUARES fit. Choose x data to be “time” Choose y data to be “output” Choose CUSTOM EQUATION and type in the equation you will be using. a*sin(b*x+c)+d Click on AUTO FIT.. The function should then try to fit. You MIGHT GET an ERROR. Open up FIT OPTIONS
Curve Fitting Tool
Example How do you know that this fit is bad?
Example 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. 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.
Adjusting Parameters One of the reasons that you get a poor fit is that the initial “guesses” of the fitting parameters are not close enough to the “real” values to get a good fit. Try changing putting in more realistic guesses. For example, the minimum and maximum values of the data are roughly 0.03 and 0.07 respectively. This means that the parameter a is approximately (0.07-0.03)/2=0.02. The parameter d represents a constant offset. From the data plot above, this is approximately 0.05. Edit the FIT OPTIONS, but using a starting point for a of 0.02 and d of 0.05. ReAPPLY the fit. Note that you STILL get a bad fit. This is because the initial guess of b is too far off. EDIT the fitting options again, but use a starting point of 6000 for parameter b. Note that the fit is MUCH better. Your new best fit should look like the following:
Adjusting Parameters
Editing Fit Parameters Does not fit Peaks well. Note that we can not quite fit the peaks of the experimental data. You will notice that while the data looks like a sine wave, there are “glitches” in that data near x=0.0033, x=0.0085, x=0.0135, and x=0.0185. We will get a better fit by EXCLUDING data outside of a specific x value range. In the Curve Fitting Tool window, choose the tab for “Tools…Exclude Outliers” or “Tools…Exclude by Rule” to limit the data to X<=0.004 and X>=0.008. The EXCLUDED data points should turn red and the data included in the fit are black. Finally, you can rescale the plot using “Tools…Axes Limits” tab.
Exclude by Rule
Excluding data Fits Peaks well. Best Fit Parameters Good FIT!