% Filename: Bluefish.m
% M-file to
%    - enter bluefish data
%    - rescale bluefish data
%    - compute equation for least squares regression line
%    - plot least squares regression line on a graph with the data
%    - compute the correlation coefficient
% LSR = Least Squares Regression

% Enter year array
year = [1940 : 5 : 1990];
% Rescale year array to get x data
x = (year - 1940)/5;

% Enter pounds of bluefish caught
y = [15000
     150000
     250000
     275000
     270000
     280000
     290000
     650000
     1200000
     1500000
     2750000]'; 
% The ' after the array changes the column array to a row array

% Find the equation for the LSR line
C = polyfit(x,log(y),1);
% Display the equation
fprintf('Eqn for LSR: ln y = %f x + %f\n',C(1),C(2))

% Find the lnyhat value for each x value
lnyhat = polyval(C,x);

% Plot the data and the LSR line
plot(x,log(y),'k.',x,lnyhat,'k-')
xlabel('Year (rescaled)')
ylabel('ln(Pounds of bluefish caught)')
xlim([min(x)-1 max(x)+1]) %Set x-axis a little wider than data

% Find the correlation coefficient
rho = corrcoef(x,log(y));
% Display the correlation coefficient
fprintf('rho = %f\n', rho(1,2))