% Exa = 1;

switch Exa
%%%% read data and display
case 1
LEDdata; % load the data
figure(1);
plot(angle,intensity,'*');

%%%% do linear Regression1
case 2
n = 6;
F = ones(length(angle),n);
for k = 1:n
  F(:,k) = angle.^(k-1);
end%for

%% p = inv(F'*F)*(F'*intensity)
[p,int_var,r,p_var] = LinearRegression1(F,intensity);
result = [p,sqrt(p_var)]    % display the estimated values for the parameters

al = (0:1:90)'; % consider angles from 0 to 90 degree
Fnew = ones(length(al),n);
for k = 1:n
  Fnew(:,k) = al.^(k-1);
end

Inew = Fnew*p;
plot(angle,intensity,'*',al,Inew)
grid on
xlabel('angle');ylabel('intensity')

%%%% do linear rescaling
case 3
LEDdata;
scalefactor = 180/pi;
angle = angle/scalefactor;

n = 5;
F = ones(length(angle),n);
for k = 1:n
  F(:,k) = angle.^(k-1);
end

[p,int_var,r,p_var] = LinearRegression1(F,intensity);
result = [p sqrt(p_var)]      % display the estimated values for the parameters

al = ((0:1:90)')/scalefactor; % consider angles from 0 to 90 degree

Fnew = ones(length(al),n);
for k = 1:n
  Fnew(:,k) = al.^(k-1);
end

Inew = Fnew*p;
plot(angle*scalefactor,intensity,'*',al*scalefactor,Inew)
grid on
xlabel('angle');ylabel('intensity')

%%%% do linear rescaling and only even contributions, and QR implementation
case 4
LEDdata;
scalefactor = 180/pi;
angle = angle/scalefactor;
n = 3;
F = ones(length(angle),n);
for k = 1:n
  F(:,k) = angle.^(2*(k-1));
end

[p,int_var,r,p_var] = LinearRegression(F,intensity);
result = [p,sqrt(p_var)]       % display the estimated values for the parameters

al = ((0:1:90)')/scalefactor;  % consider angles from 0 to 90 degree

Fnew = ones(length(al),n);
for k = 1:n
  Fnew(:,k) = al.^(2*(k-1));
end

Inew = Fnew*p;
plot(angle*scalefactor,intensity,'*',al*scalefactor,Inew)
grid on
xlabel('angle');ylabel('intensity')
end  % endswitch
