%% M. Turhan ÇOBAN
%% 20.11.2021
function retval=movingLSTSQR(x,xi,yi,n,kk)
%% x independent variable to data fit
%% xi,yi  data set
%% n degree of curve fitting
%% kk degree of weight function coefficient
n1=length(xi);
np1=n+1;
A=zeros(np1,np1);
B=zeros(np1,1);
X=zeros(np1,1);
w=zeros(n1,1);
bl1=true;
while(bl1)
for i=1:n1
  if x==xi(i) 
    yy=yi(i);
    retval=yy;
    bl1=false;
  endif
endfor
if bl1
  %%================  
for i=1:n1
  w(i)=1.0/power(abs(x-xi(i)),kk);
endfor
for i=1:np1
  for j=1:np1
     if i==1 && j==1 
       A(i,j)=n1;
     else  
       for k=1:n1
         xx1=power(xi(k),(i+j-2));
         A(i,j)=A(i,j)+ w(k,1)*xx1;
       endfor
     endif
  endfor   
  for k=1:n1
       if i==1  
         B(i)=B(i)+ w(k,1)*yi(k);
       else 
         xx1=power(xi(k),(i-1));
         B(i)=B(i)+ w(k,1)*xx1*yi(k);
       endif  
  endfor 
endfor
X(:,1)=gauss(A,B);
max=0.0;
for i=1:n+1
 if abs(X(i)) > max 
   max = abs(X(i));
 endif
endfor
retval=poly_func(X,x);
%%===============
endif
bl1=false;
endwhile

endfunction