% ask matlab to plot lagrange poly of degree n
function polyEx(n)
% need n+1 interp points
dx = 6/n;
x = -3:dx:3 % x are the interpolation points
assert(length(x) == n+1); % not sure how roundoff handled
y=0*x; % y is the interpolating data
y(6) = 1; % set one of the values to 1
hold off
plot(x,y,'b+') % plot data points to make sure ok
axis([-3.5 3.5 -.1 1.1]) % use small enough axes to see
pause
% ask matlab finds the poly: p = poly coeffs
p = polyfit(x,y,n);
% evaluate poly for plotting
t=-4:.1:4; % same interval as interp points but many more
z=polyval(p,t); % ask matlab to evaluate
hold on
plot(t,z,'r')
axis([-4 4 -2 2])
% axis auto