Physics 114: Lecture 18 Least Squares Fit to 2D Data John F. Federici NJIT Physics Department
Star Wars (retro) Humor Star Wars satire movie – Mel Brooks, Spaceballs (1987) “Luke Skywalker” finds out from “Darth Vader” how they are related. https://www.youtube.com/watch?v=CZd_YyFzPD0
Nonlinear Least Squares Fitting We saw that for a function y(x) the chi-square is which can be used to fit a curve to data points yi. In just the same way, we can easily extend this idea to fitting a function z(x,y), that is, a surface, to a 2D data set of measurements zi(x,y). You know that it takes three parameters to fit a Gaussian curve To extend this to a 2D Gaussian is straightforward. It is: Note that this has 5 parameters, but the most general way to describe a 2D Gaussian actually has 6 parameters, which you will do for homework.
The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section
The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section
The Big Picture… Why 2D fit? Before we get into details of HOW to do a 2D fit to data, let’s give a few examples of WHY one would want to do a 2D fit to data. EXAMPLE : Optical laser beams typically have a Gaussian profile in cross-section
The Big Picture… Why 2D fit? EXAMPLE : Gaussian beam profile can be distorted by atmospheric propagation… applications to free-space optical (wireless) communications. Gaussian beams can be efficiently coupled into fiber optical cables…. Fiber optic communications. Higher order Gaussian modes reduce efficiency of coupling of light into optical fibers.
Plotting a 2D Gaussian in MatLAB Let’s introduce some useful MatLAB commands for creating a 2D Gaussian. One can use a brute force method such as z = zeros(101,101); a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8; i = 0; for x = -5:0.1:5 i = i + 1; j = 0; for y = -5:0.1:5 j = j + 1; z(i,j) = a*exp(-((x-bx)/cx)^2 – ((y-by)/cy)^2); end imagesc(-5:0.1:5,-5:0.1:5,z) Or, we can plot it as a surface to provide x and y coordinates surf(-5:0.1:5,-5:0.1:5,z)
Better Way of Creating 2D Functions That way of using for loops is a very slow, so here is another way of doing it for “round” Gaussians: x = -5:0.1:5; y = -5:0.1:5; siz = size(x); siz = siz(2); a = 2.5; bx = 2.3; by = -1.2; c = 0.8; dist = zeros(siz,siz); for i=1:siz for j=1:siz dist(i,j) = x(i)^2 + y(j)^2; % Create “distance” array once end % use the next two statements multiple times for different Gaussians sdist = circshift(dist,round([bx/0.1,by/0.1])); % Shifted “distance” array z = a*exp(-sdist/c); imagesc(x,y,z);
Best Way of Creating 2D Functions The best way, however, is to use the meshgrid() MatLAB function: (This was introduced in Lecture 2) >> [x, y] = meshgrid(-5:.1:5, -5:.1:5); >> a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8; >> z = a*exp( - (((x-bx)/cx).^2 + ((y-by)/cy).^2)); >> surf(x,y,z); This is completely general, and allows vector calculations which are very fast. Here is another example, the sinc() function: z = a*sinc(sqrt(((x-bx)/cx).^2 + ((y-by)/cy).^2)); You can see that the meshgrid() function is very powerful as a shortcut for 2D function calculation. To add noise, just add sig*randn(size(z)), where sig is the standard deviation of the noise.
Examples of Fitting 2D Function Clearly, fitting a Gaussian 2D function to the data will be a NONLINEAR fit. Remember, the concept (Lecture 16) is that one is searching parameter space to find values for the parameters which make Chi-Squared (or R-Squared) a MINIMUM.
Searching Parameter Space in 2D Brute Force Example Just as we did in the 1D case, searching parameter space is just a trial and error exercise. % Initial values around which to search a0 = max(max(z)); [ix iy] = find(z == a0); bx0 = x(ix,iy); by0 = y(ix,iy); cx0 = 1; cy0 = 1; astep = 0.05; bstep = 0.05; cstep = 0.05; chisqmin = 1e20; for a = a0-5*astep:astep:a0+5*astep for bx = bx0-5*bstep:bstep:bx0+5*bstep for by = by0-5*bstep:bstep:by0+5*bstep for cx = cx0-5*cstep:cstep:cx0+5*cstep for cy = cy0-5*cstep:cstep:cy0+5*cstep ztest = a*exp(-(((x-bx)/cx).^2 + ((y-by)/cy).^2)); chisq = sum(sum(((ztest-z)/sig).^2)); if (chisq < chisqmin); chisqmin = chisq; params = [a,bx,by,cx,cy]; end end params = 2.1465 2.3000 -1.2000 1.2500 0.8000
USE THE FORCE LUKE Rather than writing code to search parameter space ourselves, we will follow our example in 1-D fitting and use the FORCE…. ie. built-in functions of Matlab to help us search parameter space. Best fit 2D data
USE THE FORCE LUKE Not so good ‘Best fit’ >> a = 2.5; bx = 2.3; by = -1.2; cx = 1.5; cy = 0.8;
Why are the fit parameters off? We need to adjust FIT options and limit the UPPER and LOWER Limits as well as INITIAL Guess…. We want a GLOBAL minimum not a LOCAL minimum The extracted parameters must make PHYSICAL sense. Example: If we were fitting data for the speed of a car on the Garden State Parkway, what MINIMUM, MAXIMUM, and INITIAL GUESSES would we use for the speed?
Change in FIT OPTIONS
Class Exercise Import Lecture_18_Laser_mode.jpg into matlab. Save the JPG data as the variable ‘laser’. Use the following code to convert to grayscale for fitting. >> grayL=rgb2gray(laser); % convert from RGB to grayscale >> colormap(gray) % use grayscale color map >> imagesc(grayL) % Plot image of laser beam mode Create the “x” and “y” limits for this 400 by 548 pixel image using the following code >> y=1:1:548; >> x=1:1:400; Using the curve fitting tool, fit the image of the laser beam mode to a GAUSSIAN beam profile z = a*exp( - (((x-bx)/cx).^2 + ((y-by)/cy).^2)) You will need to change the fit options for the fitting parameters a, bx, by, cx, and cy to get a ‘good’ best fit. Once you have a reasonable fit, comment on whether or not the laser mode appears to be well represented by a Gaussian profile. HINT: In Curve fitting tool, plot the residuals.