MATLAB: How to interpolate/ curve-fit 1D scattered data

curve fittinginterpolationMATLAB

Hi all,
I am trying to interpolate between these points, ideally I'd like to have a smooth"ish" curve passing through the points. I'm not sure how to proceed.
I have tried to convert the data to polar coordinates and use interp1:
[thi,ki]=cart2pol(xi,yi);
dth=0.01
th_int=-pi:dth:p
ki_int= interp1(thi,ki ,th_int,'pchip','extrap');
But I've been getting the "The grid vectors must contain unique points." error. I then tried a different approach by fitting a smoothing spline to the polar data.
cfit=fit(thi',ki','smoothingspline')
ki_int = feval(cfit,th_int)';
It seems to work pretty well, but if I transform it back to Cartesian coordinates
[xint,yint]=pol2cart(th_int,ki_int);
I get the following figure: the fit is good except in that one region.
Any help would be greatly appreciated, it seems like it should be a fairly easy problem to solve but I can't figure it out.
Thanks,
Clemence
Ps: I'm using the R2016a academic version and I have the curve-fitting toolbox.

Best Answer

Some fitted curves allow the end point slopes to be specified, e.g. csape and spline: you should fit to the polar data and also:
  1. define the slopes at the both ends to be the same slope, and
  2. make the line ends meet at a data point: do not extrapolate! (offset the polar data by the angle of the first point)
Do both of those and the line ends will meet up and have a smooth curve. There is an example in spline that fits a circle to some points, you may be able to adapt that. Otherwise csape has many options for controlling the end slope (and so is more complicated to use, but will definitely do what you want).