%% script file to solve Exercise 1

data = load('Ex06_01.TXT');
density = data(:,1);      %% read density data in first column
conductivity = data(:,2); %% read conductivity data in second column

figure(1);
plot(density,conductivity,'*'); %% basic plot
grid on
xlabel('density');          %% labeling axis
ylabel('conductvity');      %% labeling axis
axis([0.04 0.07 0.15 0.3])  %% choose suitable domains


%%% apply linear regression, using a straight line

n = length(density);   %% construct the matrix
F = ones(n,2);
F(:,2) = density;
[p,y_var,r,p_var]=LinearRegression(F,conductivity);

OptimalValues = p
StandardDeviations = sqrt(p_var)
RelativeDeviations = abs(StandardDeviations./p)

figure(2);
conductivityReg = F*p;
plot(density,conductivity,'+','MarkerSize',15,...
     density,conductivityReg); %% basic plot
grid on
xlabel('density');          %% labeling axis
ylabel('conductvity');      %% labeling axis
axis([0.04 0.07 0.15 0.3])  %% choose suitable domains


figure(3);
Ndensity = linspace(0.04,0.07,11)';
Fn=ones(11,2); Fn(:,2) = Ndensity;
Nconductivity = Fn*p;
plot(density,conductivity,'+','MarkerSize',15,...
    Ndensity,Nconductivity); %% basic plot
grid on
xlabel('density');          %% labeling axis
ylabel('conductvity');      %% labeling axis
axis([0.04 0.07 0.15 0.3])  %% choose suitable domains


print('Example1.png')