
1==1;

function a=polinterp(x,y)
#polynomial interpolation Lagrange
A=vander(x);
if(rows(y)==1) y=y';endif
a=A\y;
endfunction

#Runge example

f=@(x) 1./(1+x.*x);
a=-5;b=5;

s=linspace(a,b,500);

for n=[4,8,12,16,20],
 x=linspace(a,b,n+1);
 xc=(a+b)/2 + 0.5*(b-a)*cos(pi*(0.5+(0:n))./(n+1));
 y=f(x);
 yc=f(xc);
 p=polinterp(x,y);
 pc=polinterp(xc,yc);
 plot(s,fs=f(s),s,ps=polyval(p,s),s,pcs=polyval(pc,s));pause(1);
 e=norm(fs-ps,'inf');
 ec=norm(fs-pcs,'inf');
 printf("[%d] %e %e \n",n,e,ec);
end

#ill conditioned of vandermonde matrix
for n=[5 10 20 30 35 40 45 50 60]
 a=0;b=1;
 x=linspace(a,b,n+1);
 y=randn(size(x));#random values
 p=polinterp(x,y);
 e=norm(polyval(p,x)-y,'inf') #error at interpolation nodes
endfor
#linear regression

function a=linregr(x,y)
#linear regression
#LLSP with A=[x,ones(size(x))] and rhs y
# x,y  columns
A=vander(x,2);
if(rows(y)==1) y=y';end
a=A\y;
endfunction

