How can I extract X and Y from gauss2 fit or any other fit (poly2, poly 3, ...)?

10 views (last 30 days)
Hello,
I have a txt data and I want to fit this data in other to calculate de beam radius of this gaussian plot but I dont know how can i have the X and Y of the fit. My code is:
% scan the column until end1 and saves the Y and E
for h = end1(loop):-1:lastpoints1
for f = 1:numfiles
E2 = E(:,f);
Y2 = Y(:,f);
gaussfit = fit(Y2,E2,'gauss2');
%I need to have the new Y and X (or E) to use the condition "if" below:
% if NEW_E(h,:) >= m(f)
% x(h,f) = NEW_Y(h,f);
% y(h,f) = NEW_E(h,f);
% end
end
end
You can see that everything is inside a loop. This is because I have more than one file to do the same thing: extract the X and Y of each file, fit, extract the new X and Y and scan until the condition "if" is true and save the value X and Y when the condition "if" is true.
I would be very grateful if someone knew how to extract the X and Y from the fit.
I tested the fit and it is enough for what I need but I don't know how to get the curve in an array or matrix.
Many thanks.

Accepted Answer

Adam Danz
Adam Danz on 17 Aug 2020
Edited: Adam Danz on 20 Aug 2020
The fit function merely fits your pre-existing (x,y) values and returns a fitobject that contains parameter values you can use to plot the function. There are no other x or y values other than the ones you provided to the fit function.
You can create any set of x values and compute the y values from the fitobject.
Or, you can plot the fitobject and let Matlab create and x and y values of the fitted line and then extract them.
Here's how to do both.
Option 1: Create any set of X values, then compute Y
Pros
  • Flexibility; you can choose any range of X values and at any interval.
  • No need to plot the the data if you don't need to.
  • You'll learn more using this method.
% Load built-in dataset
load census;
% Fit quadratic polynomial
f=fit(cdate,pop,'poly2');
% Plot the raw data (if you want to)
clf()
plot(cdate,pop, '.')
% Create x values using the min & max of the existing data
X = linspace(min(cdate),max(cdate),100);
% Compute the Y values. The function to use is defined by the fitobject output.
% f =
% Linear model Poly2:
% f(x) = p1*x^2 + p2*x + p3 % <----------------- HERE
% Coefficients (with 95% confidence bounds):
% p1 = 0.006541 (0.006124, 0.006958)
% p2 = -23.51 (-25.09, -21.93)
% p3 = 2.113e+04 (1.964e+04, 2.262e+04)
%
% Or you can look it up here
% https://www.mathworks.com/help/curvefit/list-of-library-models-for-curve-and-surface-fitting.html#btbcvnl
% Or you can get it from the formula(f) command.
coefs = coeffvalues(f);
Y = coefs(1)*X.^2 + coefs(2)*X + coefs(3);
% Plot Curve (if you want to)
hold on
plot(X,Y, 'r-')
Option 2: Plot the fit, then grab the (X,Y) values Matlab chose
Pros
  • Matlab does the heavy lifting.
  • Less room for user error.
% Load built-in dataset
load census;
% Fit quadratic polynomial
f=fit(cdate,pop,'poly2');
% Plot the raw data & fit
h = plot(f,cdate,pop);
% Get the handle to the fitted curve
curveHandle = findobj(h,'DisplayName', 'fitted curve');
% Get the X and Y values Matlab chose
X = curveHandle.XData;
Y = curveHandle.YData;
  7 Comments
Tay
Tay on 24 Aug 2020
I'm sorry for the long text. I tried to explain it correctly. I hope you understand..
Thank you very much in advance !!
Adam Danz
Adam Danz on 24 Aug 2020
Edited: Adam Danz on 24 Aug 2020
Those variable names make it really confusing to follow.
It sounds like you're trying to find the two x-coordinates where the curve intersects with the line at y=ymax*0.37. Of course you could solve for y, knowing the function and the x-inputs.
Alternatively, you could very closely approximate the intersections using the intersections function from the file exchange.
[x0,y0] = intersections(x,y, [min(xlim),max(xlim)], ymax*0.37*[1,1]);
where x and y are the coordinates used to plot the curve and xlim is Matlab's xlim() function.
Then, plot the results for confirmation
hold on
plot(x0,y0, 'm*')
yline(ymax*0.37)

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!