how to approximate set of point to a given function
10 views (last 30 days)
Show older comments
Hello,
I am have a set of points (about 100 points) that are supposed to represent a rotated ellipse, given by this formula:
(a^2*sin(tr)^2+b^2*cos(tr)^2)*(x-x0)^2+2*(b^2-a^2)*sin(tr)*cos(tr)*(x-x0)*(y-y0)+(a^2+cos(tr)^2+b^2*sin(tr))*(y-y0)^2=a^2*b^2;
where x0,y0 are the coordinates of the center of the ellipse, a and b are the semi axes of the ellipse and tr is the rotation angle.
How would I go about finding the a,b,x0,y0 and tr so the points would be close as possible to the analytical formula.
I tried to use a multi variable minimization routine that minmize the fifference between the data points and the curve, but it seems to complicated and somewhat prone to errors.
I was wondering if there were a simple way to do that in MATLAB.
Thank you
3 Comments
Jan
on 14 Dec 2022
"The optimization algorithms I used (GA and Newton) did not converge" - Then I assume, they contain a programming error or your initial estimation was too far apart. If you post your code, the readers can check this.
Answers (2)
Bora Eryilmaz
on 14 Dec 2022
Edited: Bora Eryilmaz
on 14 Dec 2022
This is an optimization problem that can be solved using fminsearch and a least-squares cost function.
% "Unknown" model parameters
r = 3.5;
x0 = 2.8;
y0 = -1.6;
% Set of data points.
th = 0:0.05:2*pi;
x = r * cos(th) + x0;
y = r * sin(th) + y0;
plot(x, y, 'b', x0, y0, 'r+')
grid
axis equal
% Estimate model parameteres [r, x0, y0]
params0 = [0, 0, 0]; % Initial estimates.
params = fminsearch(@(p) costFcn(p,x,y), params0) % Passes data x and y to the function.
function cost = costFcn(params, x, y)
% Grid of points. Should generate xh and yh below of the same size as x and y data.
th = 0:0.05:2*pi;
r = params(1);
x0 = params(2);
y0 = params(3);
xh = r * cos(th) + x0;
yh = r * sin(th) + y0;
cost = sum((xh-x).^2 + (yh-y).^2); % Least-squares cost
end
Jan
on 14 Dec 2022
Search in the net for "Matlab fit ellipse":
- https://www.mathworks.com/matlabcentral/fileexchange/3215-fit_ellipse
- https://www.mathworks.com/matlabcentral/fileexchange/22684-ellipse-fit-direct-method
- https://www.mathworks.com/matlabcentral/answers/98522-how-do-i-fit-an-ellipse-to-my-data-in-matlab
- https://www.mathworks.com/matlabcentral/fileexchange/22683-ellipse-fit-taubin-method
- https://www.mathworks.com/matlabcentral/fileexchange/15125-fitellipse-m
2 Comments
Jan
on 14 Dec 2022
"Thanks but Matalb fit ellipse just givres a polynomial approximation." - I've posted links to 6 different approaches (the last one contains 2 methods, a linear and a non-linear fit).
"The goal here is not just getting the points but the ellipse properties" - The shown methods determine the parameters of a ellipse, which fits a given set of points.
See Also
Categories
Find more on Least Squares in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!