%section 4.1 computer problem #1

n = 5;              %number of points

%first we compute coefficients of Newton interpolant

x = [1,2,3,-4,5];     %x = x-values for interpolation
y = [2,48,272,1182,2262];     %y = y-values for interpolation

for j=2:n
    for i=n:-1:j
        y(i) = (y(i)-y(i-1))/(x(i)-x(i-j+1)); %old coefficients overwritten
    end
end                 %after this loop y = coefficients of Newton interpolant

disp('the coefficients are...')
y

%now we evaluate the interpolant at a specified point z

z = -1;
p = 1;
w = y(1);

for i=2:n
    p = p*(z-x(i-1));
    w = w + y(i)*p;
end

disp('value of Newton interpolant at -1 is...')
w
