%%% Matlab commands to study egg data
% To run: you can enter individual lines at the Matlab command line OR just
% type
% >> EggDataPlots
% (or whatever the file name is) and matlab will run the WHOLE script
%
% Note that "%" in front of anything is a "comment": Matlab ignores it
%
% IF YOU'RE EVER NOT SURE ABOUT A COMMAND, YOU CAN ALWAYS TYPE
% >> help command
% (replacing command with whatever, e.g. plot or polyfit)
% and the help file will come up.
%
% MODIFY FOR THE LOG-LOG PLOT AND SEMILOG PLOT IN THE HOMEWORK
%
%% First write in data
egg = [1700, 165.4, 94.5, 34, 14, 0.6]; % mass of egg in grams
adult = [113380, 4536, 3629, 1020, 283, 3.6]; % mass of adult in grams
%% regular plot, i.e. plot on a LINEAR scale
figure(1);
plot(egg, adult,'.','MarkerSize',25); % the command is plot(x variable,y variable,symbol,size of symbol)
% the following commands are to make the plot prettier
xlim([0.5 1710]); % indicates x-range to use
xlabel('Mass of egg (g)','FontName','Times','FontSize',20); % label for y-axis with font and font size
ylabel('Mass of adult (g)','FontName','Times','FontSize',20); % label for x-axis with font and font size
set(gca,'FontName','Times','FontSize',18); % gca means "get current axes", changes axis font and font size
% and these are to make it nice for the purpose of exporting the plot to a
% .jpg. Don't play with these until you're really comfortable with plotting
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0 0 12 8.5]);
% FINALLY to export the plot to a .pdf file. Change PlotFileName to whatever you want the file to be named.
print -f1 -djpeg 'PlotFileName.jpg' % -f1 is the "figure handle". if the figure number is 2, use -f2, etc.
% The plot named "PlotFileName.jpg" will be in your working directory
% Type
% >> help print
% at the command line to see other file types for export.
%% log-log plot % This is the plot that will show the data as a (near) straight line
figure(2);
plot(log(egg),log(adult),'.','MarkerSize',25); % the command is plot(x variable,y variable,symbol,size of symbol)
% the following commands are to make the plot prettier
xlim([log(0.5) log(1710)]); % indicates x-range to use
xlabel('log(Mass of egg (g))','FontName','Times','FontSize',20); % label for y-axis with font and font size
ylabel('log(Mass of adult (g))','FontName','Times','FontSize',20); % label for x-axis with font and font size
set(gca,'FontName','Times','FontSize',18); % gca means "get current axes", changes axis font and font size
% and these are to make it nice for the purpose of exporting the plot to a
% .jpg. Don't play with these until you're really comfortable with plotting
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0 0 12 8.5]);
% FINALLY to export the plot to a .pdf file. Change PlotFileName to whatever you want the file to be named.
print -f2 -djpeg 'PlotFileName.jpg' % -f2 is the "figure handle". if the figure number is 3, use -f3, etc.
% The plot named "PlotFileName.jpg" will be in your working directory
% Type
% >> help print
% at the command line to see other file types for export.
%% NOW LET'S DO THE LINEAR FITTING
P = polyfit(log(egg),log(adult), 1);
% P = polyfit(X,Y,N) finds the coefficients of a polynomial P(X) of
% degree N that fits the data Y best in a least-squares sense. P is a
% row vector of length N+1 containing the polynomial coefficients in
% descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
% We're inputting X = log(egg), Y = log(adult), N=1
% >> P(1)
% will give the slope of the line, and
% >> P(2)
% will give the y-intercept.
% >> P
% will print out
% P =
% P(1) P(2)
% (filling in whatever numbers P(1) and P(2) are)
%
% Thus we find log(adult) = P(1)*log(egg) + P(2).
%
% Taking exponentials of both sides, recover adult = b*egg^m
% where b = exp(P(2)) and m = P(1).
%% log log plot with linear fit
figure(3);
plot(log(egg),log(adult),'.','MarkerSize',25); % the command is plot(x variable,y variable,symbol,size of symbol)
hold on; % lets you add lines or other curves to the plot
plot(log(egg),P(1)*log(egg) + P(2),'r','LineWidth',2); % the command is plot(x variable,y variable,linecolor,width of line)
hold off; % now you can't superimpose plots any more
% the following commands are to make the plot prettier
xlim([log(0.6) log(1710)]); % indicates x-range to use
xlabel('log(Mass of egg (g))','FontName','Times','FontSize',20); % label for y-axis with font and font size
ylabel('log(Mass of adult (g))','FontName','Times','FontSize',20); % label for x-axis with font and font size
set(gca,'FontName','Times','FontSize',18); % gca means "get current axes", changes axis font and font size
% and these are to make it nice for the purpose of exporting the plot to a
% .jpg. Don't play with these until you're really comfortable with plotting
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0 0 12 8.5]);
% FINALLY to export the plot to a .pdf file. Change PlotFileName to whatever you want the file to be named.
print -f3 -djpeg 'PlotFileName.jpg' % -f2 is the "figure handle". if the figure number is 1, use -f1, etc.
% The plot named "PlotFileName.jpg" will be in your working directory
% Type
% >> help print
% at the command line to see other file types for export.
%%% THIS LAST FIGURE IS WHAT YOU'RE MEANT TO TURN IN, WITH THE ANTLER OR
%%% EBOLA DATA, IN THE HOMEWORK