Example for interpolation
Contents
Interpolation with seven equidistant nodes
We approximate the function f(x) = 1/(1+x^2) on the interval [-3,3] using the seven equidistant nodes -3,...,3.
The interpolation error f(x)-p(x) is small in the center, but very large near the endpoints of the interval [-3,3].
f = @(x) 1./(1+x.^2); % define function f (using ./ and .^ , so it also works for vectors x) xp = -3:.001:3; % use these x values for plotting x = -3:3; % equidistant nodes -3,-2,...,2,3 y = f(x); % find values of f at nodes x d = divdiff(x,y); % use divided difference algorithm to find coefficients d yp = evnewt(d,x,xp); % evaluate interpolating polynomial at points xp plot(xp,f(xp),xp,yp,x,y,'ko'); grid on % plot function f and interpolating polynomial, mark given points with black 'o' legend('f(x)','p(x)') title('f(x)=(1+x^2)^{-1} and interpolating polynomial p(x)')
