Skip to content

Instantly share code, notes, and snippets.

@cancinconn
Last active January 26, 2018 05:07
Show Gist options
  • Save cancinconn/38524f17b3e82382053d31d3c30118c0 to your computer and use it in GitHub Desktop.
Save cancinconn/38524f17b3e82382053d31d3c30118c0 to your computer and use it in GitHub Desktop.
MATLAB code to automate the search for a relation between the diagonals of regular polygons and the metallic ratios.
sideLength = 1;
differenceThreshold = 0; % show results that differ at most this much.
%differenceThreshold = 0.0000000001;
metallicCount = 1000000; % to be safe, make sure this is greater than sideLength * (the maximum value of n)
metallicRatios = zeros(1, metallicCount);
for ratioNumber = 1:metallicCount
metallicRatios(1, ratioNumber) = (ratioNumber + sqrt(4 + ratioNumber^2))/2;
end
tic
parfor n = 1:4000 % determines which polygons we examine.
if (mod(n, 400) == 0)
disp(["Checking Polygon with ", num2str(n), " sides."]);
drawnow;
end
internalAngleSum = (n-2) * 180;
singleAngle = internalAngleSum / n; % unused atm.
exteriorAngle = 360/n;
exteriorAngleRad = exteriorAngle*pi/180 ;
for diagCount = 1:floor(n/2) - 1 %diagCount describes which diagonal this is (1st, 2nd etc.)
diagStart = [0, 0];
diagEnd = [0, 0];
%trace the edges of the polygon up to the end of the diagonal, then measure distance:
for rotCount = 1:(diagCount+1)
%rotate each line segment by the correct amount and stick it onto the end of the last one
theta = rotCount * exteriorAngleRad;
rotMtx = [cos(theta) -sin(theta) ;sin(theta) cos(theta)] ;
rotatedLineSegmentEnd = [0, sideLength] * rotMtx;
diagEnd = diagEnd + rotatedLineSegmentEnd;
end
distance = norm(diagEnd - diagStart); % this is the length of the diagonal
ratio = distance / sideLength;
for ratioNumber = floor(ratio):ceil(ratio) % don't need to check past these limits
if (abs(ratio - metallicRatios(ratioNumber)) <= differenceThreshold)
text = ["Diagonal #", num2str(diagCount), "of a",num2str(n),"sided polygon is related to ratio #", num2str(ratioNumber)];
disp(text);
format long
disp(ratio);
disp(metallicRatios(ratioNumber));
end
end
end
end
toc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment