2

It seems to be very basic question, but I wonder when I plot x values against y values, what interpolation technique is used behind the scene to show me the discrete data as continuous? Consider the following example:

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

My guess is it is a Lagrangian interpolation?

Ress
  • 667
  • 1
  • 7
  • 24

3 Answers3

2

No, it's just a linear interpolation. Your example uses a quite long dataset, so you can't tell the difference. Try plotting a short dataset and you'll see it.

Dave Kielpinski
  • 1,152
  • 1
  • 9
  • 20
2

MATLAB's plot performs simple linear interpolation. For finer resolution you'd have to supply more sample points or interpolate between the given x values.

For example taking the sinus from the answer of FamousBlueRaincoat, one can just create an x vector with more equidistant values. Note, that the linear interpolated values coincide with the original plot lines, as the original does use linear interpolation as well. Note also, that the x_ip vector does not include (all) of the original points. This is why the do not coincide at point (~0.8, ~0.7).

enter image description here

Code

x = 0:pi/4:2*pi;
y = sin(x);

x_ip    = linspace(x(1),x(end),5*numel(x));
y_lin   = interp1(x,y,x_ip,'linear');
y_pch   = interp1(x,y,x_ip,'pchip');
y_v5c   = interp1(x,y,x_ip,'v5cubic');
y_spl   = interp1(x,y,x_ip,'spline');

plot(x,y,x_ip,y_lin,x_ip,y_pch,x_ip,y_v5c,x_ip,y_spl,'LineWidth',1.2)
set(gca,'xlim',[pi/5 pi/2],'ylim',[0.5 1],'FontSize',16)

hLeg = legend(...
    'No Interpolation','Linear Interpolation',...
    'PChip Interpolation','v5cubic Interpolation',...
    'Spline Interpolation');
set(hLeg,'Location','south','Fontsize',16);

By the way..this does also apply to mesh and others

[X,Y] = meshgrid(-8:2:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;

figure
mesh(Z)

enter image description here

Community
  • 1
  • 1
embert
  • 7,336
  • 10
  • 49
  • 78
  • Thanks for responses, the concept is linear interpolation, i think it would be good idea if matlab plot function had an argument about the interpolation method. – Ress Feb 07 '15 at 21:58
  • That's exactly what I thought, @hamiddashti - I was suprised, when I did not find something alike in the `plot` docs. – embert Feb 08 '15 at 07:55
1

No, Lagrangian interpolation with 200 equally spaced points would be an incredibly bad idea. (See: Runge's phenomenon).

The plot command simply connects the given (x,y) points by straight lines, in the order given. To see this for yourself, use fewer points:

x = 0:pi/4:2*pi;
y = sin(x);
plot(x,y)

output