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

function retval = multpoly_least(xi,yi,f,n)
    switch nargin
        case 3
          n=input("degree of polynomial ? ");
      case 2
          f=@mult_f;
          n=input("degree of polynomial ? ");
    end  
    m=length(yi);
    np1=n+1;
    A =zeros(np1,np1);  
    B =zeros(np1,1);
    X =zeros(np1);
    for i=1:np1
        for j=1:np1
            for k=1:m
                    fi=f(xi,i,k);
                    fj=f(xi,j,k);
                    A(i,j)= A(i,j)+fi*fj;
            end # for k=1:m
        end # j=1:np1
        for k=1:m 
            fi=f(xi,i,k);
            yyi=yi(k);
            B(i)= B(i)+fi*yyi;
        end 
    end
    X=gauss1(A,B);
    retval=X;
end