Curve fitting: general Parabola --> translated and rotated coordinate system

47 views (last 30 days)
Hi guys, I have a set of points through which I need to fit a general parabola (to find out its vertex, angle of inclination of directrix from x-axis and parameter a). But problem is these points are not well conditioned and they are in a rotated and translated coordinate system, though not inclined set of axes(angle between axes ==90 degree). Does anyone how to do that in matlab?
Points are something like this:
x= [-5.2:.4:0,-5.2:.4:0]';
y=[4,3.6,5.2,6,6.4,6.8,6.8,7.2,7.6,10.4,10.4,11.2,11.6,11.2,3.6,3.2,2.8,2.4,2,1.6,1.2,0.4,0,-.4,-1.2,-1.2,-1.6,-1.6]';
Just do plot(x,y,'.') and you will see what I am talking about.
Any help would be much appreciated.
Thanks, Shubham

Accepted Answer

Matt J
Matt J on 28 Jun 2013
Edited: Matt J on 28 Jun 2013
Something like this, maybe:
function [vertex,theta, a] = myfit(x,y)
xy=[x(:),y(:)].';
theta= fminsearch(@(theta) cost(theta,xy), 45);
[~,coeffs]=cost(theta,xy);
[a,b,c]=deal(coeffs(1),coeffs(2), coeffs(3));
xv=-b/2/a;
vertex=R(-theta)*[xv;polyval(coeffs,xv)];
function [Cost,coeffs,xx,yy] = cost(theta,xy)
Rxy=R(theta)*xy;
[xx,idx]=sort(Rxy(1,:));
yy=Rxy(2,idx);
[coeffs,S]=polyfit(xx,yy,2);
Cost=S.normr;
function Rmat=R(theta)
Rmat=[cosd(theta), -sind(theta); sind(theta), cosd(theta)];
  5 Comments
Carl Witthoft
Carl Witthoft on 17 Oct 2016
I think there's a bug in the proposed solution: if the original parabola is rotated in the opposite direction, the default starting value (+45) will find a local minimum at a value roughly 90 degrees off, and not very accurately there. Probably the initial value should be selected after determining in which quadrant(s) the majority of the input dataset reside(s). The reason for this problem is that the parabola x=y^2 will yield a minimum more or less for a degenerate parabola, i.e. straight line, along the axis of the parabola.
Christoph
Christoph on 3 Apr 2017
Edited: Christoph on 3 Apr 2017
First let me thank you very much for your provided code, it helped me a lot. Second this would be an idea how to draw the ellipse afterwards,
a = coeffs(1);
b = coeffs(2);
c = coeffs(3);
t=-1000:1:+1000;
xEllipse = -(a.*t.^2 + b*t + c) * sin(-theta) + cos(-theta)*t;
yEllipse = +(a.*t.^2 + b*t + c) * cos(-theta) + sin(-theta)*t;
plot(xEllipse,yEllipse,'b.');
Example Image or curve fitting, red the original points fitted with a polynomial for each side, plotted in blue.
And my question would be: If I am skipping the fminsearch for minimizing the cost function and finding the best theta angle and just use the 45° theta angle for the coefficient calculation I can also achieve good, sometimes also better cost results as while using the fminseach function. I am not able to understand this at the moment, could you provide a clarification for me. Thank you very much, Christoph

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!