clc
clear all
close all
%%
%Question Q2.i
%Import data from Excel File
filename = 'Data_Input_Q2.xlsx'; %Excel file name
sheet = 'Calibration Curve';%Excel sheet name
xlRange = 'A2:B8' ; %Rectangular range of data to import
data = xlsread(filename,sheet,xlRange);
%%
%Question [Link]
I = data(:,1); % fluorescence intensity data
c = data(:,2); % concentration data
data_size = size(data); %size of matrix data
N = data_size(1); %number of rows of matrix data = number of data points
%%
%Question [Link]
%Calculation of slope b and intercept a
x = c ; %independent variable
y = I ; %dependent variable
mean_x = mean(x); % mean of x-array elements
sum_x = sum(x) ; % sum of x-array elements
sum_sq_x = sum(x.^2) ; % sum of squared x-array elements
mean_y = mean(y); % mean of y-array elements
sum_y = sum(y); % sum of y-array elements
sum_xy = sum(x.*y); % sum of the products of x-array and y-array elements
%Calculation of slope b
b = N*sum_xy - sum_x*sum_y ;
b = b./(N*sum_sq_x - sum_x^2);
disp(' ')
text = sprintf ('The slope is b = %4.3f',b);
disp(text)
%Calculation of intercept a
a = mean_y - b*mean_x ;
disp(' ')
text = sprintf ('The intercept is a = %4.3f',a);
disp(text)
%%
%Question [Link]
fh=figure(1);
hold on
plot(x,y,'sk') % Plot the experimental data
plot(x,b*x+a,'-k') % Plot the regression curve
title('Calibration Curve') %add a title to the plot
xlabel('Concentration [pg/ml]') %add a label to the x axis
ylabel('Fluorescence Intensity [a.u.]') %add a label to the y axis
legend('Experiments','Fit','Location','NorthWest'); %add a legend
saveas(fh,'Figure_Q2.jpg') %save the plot as jpg file
%%
%Question Q2.v
%Calculation of statistics s_yx
y_hat = b*x + a; %y data points from the fit
temp = (y-y_hat).^2;
temp = sum(temp);
s_yx = sqrt( temp/(N-2) ) ; %statistics s_yx
%Calculation of the standard deviation of slope
temp = (x-mean_x).^2;
temp = sum(temp);
temp = sqrt(temp);
s_b = s_yx/temp ;
%Calculation of the standard deviation of intercept
temp = (x-mean_x).^2 ;
temp = sum(temp)*N ;
temp = sum_sq_x/temp ;
temp = sqrt(temp);
s_a = s_yx*temp ;
disp(' ')
text = sprintf ('The standar deviation of the slope is s_b = %5.4f',s_b);
disp(text)
disp(' ')
text = sprintf ('The standar deviation of the intercept is s_a = %5.4f',s_a);
disp(text)
%%
%Question [Link]
%estimate of fluorescein concentration
y0 = 13.5 ; % in a.u.
x0 = (y0 - a)/ b; % in pg/ml
%standard deviation s_x0 of the concentration
temp = (x - mean_x).^2;
temp = b^2 * sum(temp);
temp = (y0-mean_y)^2 / temp;
temp = sqrt( 1 + 1/N + temp);
s_x0 = s_yx/b * temp ;
%Critical values of t for 95% confidence (two-sided) and degrees of freedom df
df = N - 2;
%t 95% confidence two-sided correspond at t for 95+2.5=97.5% confidence
%one-sided
t = tinv(0.975,df); %critial values of t
%95% confidence limit for the concentration
c_lim = t*s_x0;
%%
%Question [Link]
filename_out = 'Data_Output_Q2.csv';
%csvwrite(filename_out,[a, b, s_a, s_b, x0, c_lim])
T = table({'Intercept';'Slope';'Statistics s_a';'Statistics s_b';'Conc
x0';'Confidence Limit'},[a, b, s_a, s_b, x0, c_lim]');
writetable(T,filename_out)