How to apply a fit on this shape?

15 views (last 30 days)
Since my data doens't really count as a function cftool is not an option. I wonder whether this is still faisable to fit:
(I tried several elliptical fits but they don't seem to work. May be because I dealt with it a little clumsily)

Accepted Answer

John D'Errico
John D'Errico on 15 Jul 2020
Edited: John D'Errico on 15 Jul 2020
What does it mean to "fit" it to you? What will you do with that fit? The data is not a function, just a set of points, that happen to represent a closed curve in the plane.
A simple solution might be to convert to polar coordinates.
help cart2pol
Now, fit the function r(theta). Be careful, since r is a periodic function. It should have the property that r(0) == r(2*pi). So a polynomial fit would be a bad idea. Instead, perhaps fit this as a sum.
r(theta) = a0 + a1*sin(theta) + b1*cos(theta) + a2*sin(2*theta) + b2*cos(2*theta) + ...
Essentially, I am describing what is essentially a Fourier series there (that is, not an fft, but a Fourier seies.)
As it is constructed, the function ALWAYS has the property of being periodic. And, while you could, in theory, trivially use the curve fitting toolbox for this task, there is no reason to do so. All of the parameters can be estimated using one call to backslash.
x = x(:);
y = y(:);
mux = mean(x);
muy = mean(y);
[Theta,R] = cart2pol(x- mux,y-muy);
[Theta,ind] = sort(Theta);
R = R(ind);
plot(Theta,R,'o')
n = numel(x);
M = 6;
A = [ones(n,1),sin(Theta*(1:M)),cos(Theta*(1:M))];
coeffs = A\R;
Rhat = A*coeffs;
hold on
plot(Theta,Rhat)
Larger values of M will yield a better approximation. A perfect circle would have m == 0. Because your curve has an oval shape, you need to have a few extra terms, but not many. M == 6 will require the estimation of 13 coefficients for the series.
I lack your data, so I generated some random crap data as an example, using ginput.
plot(x,y,'o')
Nothing exciting. Vaguely ovoid. I'm actually surprised it was that good.
R as a function of Theta seems reasonable.
And now, we can reconstruct the original curve.
[xhat,yhat] = pol2cart(Theta,Rhat);
plot(x,y,'bo',xhat + mux,yhat + muy,'-xr')
Surprisingly good for a simple, moderately low order Fourier series approximation, given the data came from my shaky eye/mouse coordinated hand.
  9 Comments
Image Analyst
Image Analyst on 19 Feb 2023
Moved: John D'Errico on 20 Feb 2023
@tarek hussein see the attached paper.
Sorry, I don't have MATLAB implementations for any of the shapes.
tarek hussein
tarek hussein on 20 Feb 2023
Moved: John D'Errico on 20 Feb 2023
thanks for your response

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!