% goldenRectangle
% Spring CS3734
% Assignment 1, problem 1
% Which of these familiar rectangles is closest to a golden rectangle
% whose aspect ratio is given by $\phi = (1+\sqrt{5})/2$?
%   3-by-5 inch index card
%   8.5-by-11 inch US letter paper
%   8.5-by-14 inch US legal paper
%   9-by-12 foot rug
%   9:16 "letterbox" TV picture
%   768-by-1024 pixel computer monitor
% Problem adopted from C. Moler, NCM.
% K. Ming Leung, 02/04/05

clear all; format short;
phi = 0.5*(1+sqrt(5)),  % the golden ratio

% Input the widths and lengths:
Width =  [ 5  11  14 12 16 10 1024];
Height = [ 3 8.5 8.5  9  9  8  768];

% Find the aspect ratio:
AspectRatio = Width./Height,

% Find the relative errors
RelativeError = abs(1-AspectRatio/phi),

% Find the case with aspect ratio closest to the golden ratio:
% idx is the index pointing to that case.
[minError, idx] = min(RelativeError);

% The following displays a character string by converting 
% numbers to strings using the MATLAB num2str function:
%disp(['The ' num2str(Width(idx)) ' by ' num2str(Height(idx)) ' geometry is the closest to a golden rectangle.']);

% Results can also be displayed using C-like formatting statements:
fprintf(1,'The %5.1f by %5.1f geometry is the closest to a golden rectangle.', ...
    Width(idx), Height(idx));
% The correct result should display: 
% The  14.0 by   8.5 geometry is the closest to a golden rectangle.
