MATLAB: Curve fitting: general Parabola –> translated and rotated coordinate system

coordinate geometryoptimizationparabola

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

Best Answer

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)];