%disp('+++++++++++++++++++++++++++++++++++++++++++++++++++')
%disp('+Part II: least squares approximation of functions+')
%disp('+++++++++++++++++++++++++++++++++++++++++++++++++++')

%disp('+++++++++++++++++++++++++++++++++++++++++++++++++++')
%disp('++++++++++Least squares approximation++++++++++++++')
%disp('+++++++++++++of Runge''s function+++++++++++++++++++')
%disp('++++++++++by a polynomial of degree n++++++++++++++')
%disp('+++++++++++++++++++++++++++++++++++++++++++++++++++')

%clear all

%we compute the least squares approximation
%to the function (1+x^2)^(-1) on the interval [-5,5]
% by a polynomial of degree n. the data are the values
%of the function at m equidistant point on that interval

%n=input('enter the degree of the polynomial: ');


%z=linspace(-5,5)';
%plot(z, 1./(1+z.^2)), hold on

t=[-5: 10/(m-1): 5]';
J=[n:-1:0];
V=repmat(t,1,n+1).^repmat(J,length(t),1);
b=1./(1+t.^2)+(rand(length(t),1)-.5)*noiselevel;
%plot(t,b)
%null=input(' ');

x=V\b;

%plot(z,polyval(x,z),'r')

%disp('The norm of the error in this least square approximation  is: ')
polynomialLeastErrorNormalized=(norm(1./(1+t.^2)-polyval(x,t),2))/sqrt(m);

%hold off


