#!/usr/bin/octave-cli

if(nargin != 3)
	exit(1);
endif

infile=argv(){1};
bfile=argv(){2};
outfile=argv(){3};

data=load(infile);
x1=data(:,1:size(data,2)-1);
Y=data(:,size(data,2):size(data,2));
X=ones(rows(x1),2);
# X(:,1)=1
X(:,2)=x1;
B=(X'*X)^(-1)*X'*Y
save("-ascii",bfile,"B");
gridX=linspace(min(x1),max(x1),100)';
gridY=B(1,1) .+ gridX .* B(2,1);
out=zeros(size(gridX,1),2);
out(:,1)=gridX;
out(:,2)=gridY;
save("-ascii",outfile,"out");
