## Author: M. Turhan ÇOBAN
## Horner method
## Created: 2021-06-12
## roots of a polynomial by using synthetic division process 
## f(x)=a0+a1*x+a2*x^2+a3*x^3+a4*x^4+..
## a=[a0,a1,a2,a3,a4,..]   
function retval = horner(a1) 
      format long
      n=length(a1);
      x1=complex(0.56435,0.1);
      n1=n;
      nmax=100;
      tolerance=1.0e-10;
      n1=n+1; 
      ref=3.1;
      n2=n-1;  
      a=a1;   
      for k=1:n2
              n=length(a);
              x=x1;
              xold=complex(1.06435,1.234);
              ref=abs(xold-x);
              b=zeros(n,1);
              c=zeros(n-1,1);
              b(n)=a(n);
              ii=1;
              while ref>tolerance && ii<nmax
                 for i=n-1:-1:1
                     b(i)=a(i)+b(i+1)*x;
                 endfor
                 for i=n-1:-1:1
                   c(i)=b(i+1);
                 endfor
                 c1=flip(c);
                 Q=polyval(c1,x);
                 P=b(1);
                 x=x-P/Q;
                 ref=abs(xold-x);
                 ii=ii+1;
                 xold=x;
              endwhile 
             a=c;
              y(k)=x;
      endfor
      retval=y';  
endfunction