
clear				% Clear previous definitions
figure(1)           % Assign figure number
clf                 % Clear previous figures
hold off            % Start with fresh graph

mytitle = 'Allometric Models for Lake Trout'; 	% Title
xlab = 'Length (cm)'; 		% X-label
ylab = 'Weight (g)'; 		% Y-label

load('fishdat');   % Loads stored data file

[k,a] = powerfit(ltdfish,wtdfish);  % Finds best linear model to log of data
allom = k*ltdfish.^a;               % Defines allometric model at data
err = allom-wtdfish;                % Finds error between model and data
J1 = err*err';                      % Finds sum of square errors
[p1,J2,flag] = fminsearch(@sumsq_nonlin,[k,a],[],ltdfish,wtdfish);
  % Find nonlinear least squares fit to allometric model to data
[p2,J3,flag] = fminsearch(@sumsq_cubic,k,[],ltdfish,wtdfish);
  % Find nonlinear least squares fit to cubic model to data
lt = linspace(0,100,500);	% length domain
W1t = k*lt.^a;              % Allometric model - over domain
W2t = p1(1)*lt.^p1(2);      % Nonlinear best fit - over domain
W3t = p2*lt.^3;             % Cubic model - over domain

plot(lt,W1t,'b-','LineWidth',1.5);  % Plot allometric model
hold on                             % Plots Multiple graphs
plot(lt,W2t,'k-','LineWidth',1.5);  % Plot nonlinear fit model
plot(lt,W3t,'r-','LineWidth',1.5);  % Plot cubic fit model
plot(ltdfish,wtdfish,'ro','LineWidth',1.5);  % Plot data
grid                                % Adds Gridlines
legend({'Allometric','Nonlinear Fit','Cubic Fit','Data'},'location','Northwest', 'FontSize',10,'FontName','Times New Roman');  % Create legend
xlim([0 100]);  % Defines limits of graph
ylim([0 5500]);
fontlabs = 'Times New Roman';   % Font type used in labels
xlabel(xlab,'FontSize',14,'FontName',fontlabs,'interpreter','latex');  
        % x-Label size and font
ylabel(ylab,'FontSize',14,'FontName',fontlabs,'interpreter','latex');  
        % y-Label size and font
title(mytitle,'FontSize',16,'FontName','Times New Roman','interpreter','latex'); 
        % Title size/font
set(gca,'FontSize',12);         % Axis tick font size

print -depsc allom.eps		% Create figure as EPS file
%print -djpeg allom.jpg		% Create figure as JPEG file

