MATLAB: How to interpolate an ellipse given data points

ellipse fitinterpolating ellipseinterpolating ellipse datapointsMATLAB

Hi,
I plotted number of concentric ellipses using contour function, where each ellipse represents a flux level which is a function of two currents.
But the matrix which I obatained using contour function contains some x, y coordinates which when plotted gives me an approximate ellipse shape. For example let's say a small ellipse and matlab gives me a diamond shape for it, like in the picure shown
So, my intention is to make all these ellipses more smooth by increasing the number of data points in each ellipse.
I have tried using the code below
and it gives me a weird plots like shown below
image.PNG
I have also removed level,vertices column but still the plot looks weird.
So, is there any way to to get good ellipse shapes, also I should be able to access x and y datapoints after interpolation
Thank you.

Best Answer

The challenge is to get your x to be unique and increasing. If I create a new variable that is the cumulative distance of each point from the first point, that satisfies that condition. I use that as my new x and use it to interp both x and y.
By default, interp1 is linearly adding points between known data points (same slope). To get a more rounded shape, set the interp style to spline.
% https://www.mathworks.com/matlabcentral/answers/86615-how-to-plot-an-ellipse#answer_96122
a=10; % horizontal radius
b=5; % vertical radius
x0=0; % x0,y0 ellipse centre coordinates
y0=0;
t=linspace(-pi,pi,11);
x=x0+a*cos(t);
y=y0+b*sin(t);
plot(x,y)
axis equal
% Now that we have x, y vals, reparameterize and interp.
% Assuming linear interpolation adequately represents the ellipse
dist = sqrt(diff(x).^2+diff(y).^2);
%Reparameterize
tm = cumsum([0 dist]);
t = linspace(0,tm(end));
xInt = interp1(tm,x,t,"spline");
yInt = interp1(tm,y,t,"spline");
hold on
plot(xInt,yInt)