% Adapted from the code written initially by Dr. D. Pelinovsky


clear all
close all

% Data for the problem
T = [77,100,185,239,285];
S = [2.4,3.4,7.0,11.1,19.6];

% Plotting the data
figure(1);
plot(T,S,'*'); 
xlabel('T'); ylabel('S'); 
figure(2);
plot(T,log(S),'*'); 
xlabel('T'); ylabel('log(S)'); 
% answer to (a)

% Constructing and solving normal equations;
% The approximating function: S = a exp( b T )
a11 = sum(T.^2); a12 = sum(T); a21 = sum(T); a22 = sum(ones(1,length(T)));
A = [ a11,a12; a21,a22]; 
b1 = sum(T.*log(S)); b2 = sum(log(S)); b = [ b1; b2 ]; 
c = A \ b  % 
disp('Coefficients a,b from linear regression fit')
a = exp(c(2))
b = c(1) 
% answer to (b)


% Plotting the linear and semilinear graphs;
t = 50 : 0.1 : 300;
s = a*exp(b*t);
figure(1);
hold on;
plot(t,s);  
figure(2);
hold on;
plot(t,log(s)); % answer to (c)


% Determining the error;
disp('Total square error of linear regression fit');
E = sum((log(S)-c(1)*T-c(2)).^2)  
disp('Correlation coefficient of linear regression fit');
E0 = sum((log(S)-c(2)).^2);
R = (E0-E)/E0

