function[Err, c]=least_square_fh(a,b,x,d,xi,yi)
% -----------------------------------------------------------------
% Using the least square method with F-H's basis functions, this function
% computes the approximation of data (xi,yi).
%
% INPUTS:
% a,b - endpoints of the interval
% x - vector of nodes
% d - degree of polynomial of F-H's interpolant
% (xi,yi) - 2 column-matrix with data, that we want to approximate
% 
% OUTPUTS:
% Err - error
% c - vector of coefficients of the least square solution
% 
%  ---------------------------------------------------------------
% Authors: 
%      Stefano De Marchi and Cinzia Bandiziol 
%      Department of Pure and Applied Math.
%      University of Padova
%           
% Version: May 2015		
%  ----------------------------------------------------------------

n=size(x,1);
w_fh=weights(d,x);

C=eval_bi(x,w_fh,xi);

A=C';

% Compute the Q-R decomposition
[Q,R]= qr(A);

b_fin=Q'*yi;
b_red=b_fin(1:n);
b_2=b_fin(n+1:end);
R_red=R(1:n,:);

% Solve the linear system and find the least square solution
z=R_red\b_red;
c=z;

points_plot=linspace(a,b,100)';

Err=norm(b_2,2);