disp ('                This is the code of Newton  method.Is writen by Ali Ashouri')
disp (' ')
disp (' ')
syms x;
n=input('  Enter your number of data of xi or fi  (n)  :  ');
n=n+1;
y=cell(1,n);
f=cell(1,n);
Z=cell(n-1,n-1);
for i=1:n
    b=input('   Enter your data of xi  :   ');
    c=input('   Enter your data of fi   :   ');
    y(1,i)={b};
    f(1,i)={c};
end;
for i=1:n-1
for j=1:n-1
        Z(i,j)={0};
end;
end;
disp (' ')
disp(['  xi : ',y])
disp(['  fi  : ',f])
    for j=1:n-1
        Z(j,1)={(cell2mat(f(1,j+1))-cell2mat(f(1,j)))./(cell2mat(y(1,j+1))-cell2mat(y(1,j)))};
    end;
    for i=2:n-1
        for j=1:n-i
            Z(j,i)={(cell2mat(Z(j+1,i-1))-cell2mat(Z(j,i-1)))./(cell2mat(y(1,j+i))-cell2mat(y(1,j)))};
        end;
    end;
    Q=0;
  disp(Z)
  for i=1:n-1
      U=1;
      for j=1:i
          U=U.*(x-cell2mat(y(1,j)));
      end;
      U=U.*(cell2mat(Z(1,i)));
      Q=Q+U;
  end;
  P=Q+cell2mat(f(1,1));
  r=['    P(x) : ',P];
  disp(r);
u=input('    Enter your data of x  :    ');
T=subs(P,x,u);
disp(' ')
disp(['      The fi of your x is  :  ',num2str(sym2poly(T))]);