clear all
close all
clc

x=[1 2 3 4 5];
y=[2 3 4 5 6];
[h] = DifferenzeDivise(x,y)
xx=-1
[P] = PolNewton(xx,x,h)

function [h] = DifferenzeDivise(x,y)
%calcolo delle differenze divise 
n=length(x)-1;
h=y;
for i=2:n
    for j=n:-1:i-1
       h(j+1)=(h(j+1)-h(j))./(x(j+1)-x(j))
end
end

function [P] = PolNewton(xx,x,h)
%valutazione del polinomio di newton in un determinato punto
    n=length(h);
    P=h(n);

    for i=n-1:-1:0
        P=h(i+1)+(xx-x(i+1)).*P;
    end
end