MATLAB: Getting a plot to wrap from right to left

polyfitsplinewrap plot

Hello, All. I have a set of points to plot and fit a function (the end result should be a sine function). Here's the code I'm using and it works just like you'd expect it to:
x = [-65.0600; -0.5310; 71.5820; 137.4960; -153.9860; -87.7790];
y = [0.1000; 18.9260; 11.4630; -12.4760; -18.9610; 1.5900];
plot(x, y, 'ro', 'MarkerSize', 10);
interpolatedX = linspace(min(x), max(x), 500);
interpolatedY = spline(x,y,interpolatedX);
hold on;
plot(interpolatedX, interpolatedY, 'b-', 'LineWidth', 3);
grid on;
xlabel('longitude');
ylabel('latitude');
The problem is it's not what I need. Rather than try to explain, I've attached a .jpg of what I'm trying to achieve. I'd prefer to have the plot to wrap from right to left, as shown. If that's not possible, the entire plot can shift. The bottom line is that the interpolated line needs to start with x(1),y(1), end with x(6),y(6) and fit a sine function. Thanks for the help!

Best Answer

Hi Bill,
You have six points whose real starting point is evidently at x = -65. The idea here is to take the two leftmost x points and put them to the right by adding 360 degrees. Then do the spline. That's shown in figure 1. To get a plot like the example you have to cut out a part of that curve and move it back to the left. That's the process for figure 2.
x = [-65.0600; -0.5310; 71.5820; 137.4960; -153.9860; -87.7790];
y = [0.1000; 18.9260; 11.4630; -12.4760; -18.9610; 1.5900];
% move two x points to the right
x1 = x;
x1(x1<-75) = x1(x1<-75) + 360;
interpolatedX = linspace(min(x1), max(x1), 500);
interpolatedY = spline(x1,y,interpolatedX);
figure(1)
plot(x1,y,'ro',interpolatedX,interpolatedY,'b')
% cut out the part greater than 160 degrees, move it to the left
ind = interpolatedX > 160;
piece1X = interpolatedX(ind)-360;
piece1Y = interpolatedY(ind);
piece2X = interpolatedX(~ind);
piece2Y = interpolatedY(~ind);
figure(2)
plot(piece1X,piece1Y,'b',piece2X,piece2Y,'b','LineWidth', 3)
hold on
plot(x,y,'ro','MarkerSize', 10)
hold off
Related Question