## General polynomial least square curve fitting
## Author: M. Turhan ÇOBAN
## Created: 2021-08-22

function retval = genpoly_least(xi,yi,f,n)
    #Polynomial least square
    switch nargin
        case 3
          n=input("degree of polynomial ? ");
      case 2
          f=@gen_f;
          n=input("degree of polynomial ? ");
    end  
    m=length(xi);
    np1=n+1;
    A =zeros(np1,np1);  
    B =zeros(np1);
    X =zeros(np1);
    for i=1:np1
        for j=1:np1
            for k=1:m
                    pp=xi(k)^(i+j);
                    A(i,j)= A(i,j)+f(xi(k),i)*f(xi(k),j);
            end # for k=1:m
        end # j=1:np1
        for k=1:m 
            B(i)= B(i)+f(xi(k),i)*yi(k);
        end 
    end
    X=gauss1(A,B);
    retval=X;
endfunction