• Remix
  • Share
  • New Entry

  • Adam Danz

  • /
  • Visualize patterns in fractions - easy remix!

on 21 Nov 2023
  • 12
  • 33
  • 0
  • 0
  • 1312
drawframe(1);
This animation shows the first 10000 decimal places of the fraction 333/106 which is an approximation of pi. It looks very different from the first 10000 dp of pi! It uses symbolic variables and variable-precision arithmetic to acquire the decimal values.
Explore patterns in fractions
Here's an easy way to explore patterns in fractions and it's an easy remix!
Simply replace the first line of code in the drawframe function with a fraction using symbolic variables or symbolic expressions.
Here are some fun ones to try
  • 1/sym(9973)
  • 1/sym(9679)
  • 1/sym(81)
  • 1/sym(23)
  • 1/sym(49)
  • 1/sym(42)
  • 1/sym(31)
  • 1/sym(61)
Try 2/11, 22/7
Explore magic sevenths: 1/7, 2/7, 3/7, 4/7, 5/7, 6/7
function drawframe(f)
% Enter the fraction using symbolic variables
symfrac = sym(333) / sym(106);
% Don't change this line
animate(f, symfrac)
end
function animate(f, s)
persistent x y d
if f>24; return; end
n = 10080; % must be divisible by 24
if f==1 || isempty(x)
decimalChar = char(vpa(mod(abs(s),1),n+3));
decvec = decimalChar(3:end-3) - '0';
% Assign each digit an angular coordinate based on its value 0:9
nDecimals = numel(decvec);
theta = ((0:36:324)+(0:36/nDecimals:36)')*pi/180;
ang = theta(sub2ind(size(theta),1:nDecimals,decvec+1));
% Compute the length of each line segment; used to set color
[x,y] = pol2cart(ang,1);
[~,~,d] = uniquetol(hypot(diff(x),diff(y)));
% Plot line segements using the edge property of a Patch object
% Plot segments using patch so we can control transparency within one
% graphics object.
clf
set(gcf, 'Color','k');
axes(Position = [0 0 1 1]);
hold on
axis equal padded off
colormap(turbo(10))
% Labels
gap = 3; % gap between segments in degrees
startpt = ((0:36:324) + gap/2)*pi/180; % starting point of each segment, radians
segAng = (0:0.02:1)'.*((36-gap)*pi/180) + startpt; % angular coordinates for segments
radius = 1.08;
[segx,segy] = pol2cart(segAng,radius);
plot(segx,segy,'-w',LineWidth=1)
% add bounds labels
midAng = ((0:36:324)+18) * pi/180;
tradius = radius + .08;
[tx,ty] = pol2cart(midAng,tradius);
text(tx, ty, string(0:9), ...,
FontUnits='normalized',...
FontSize=0.06, ...
Color='w', ...
HorizontalAlignment='center',...
VerticalAlignment='middle');
% Show the fraction in title
ttl = title(char(s),'Color','w','Interpreter','none',...
'FontUnits','normalized','FontSize', 0.07);
set(gca,'TitleHorizontalAlignment', 'Left')
ttl.Position(2) = 1;
end
nFrames = 24; % Animate for the first second only
frameIdx = [1,find(mod(1:n,n/nFrames)==0)];
fidx = frameIdx(f) : frameIdx(f+1);
p = patch(gca, XData=x(fidx), YData=y(fidx), FaceColor='none', EdgeAlpha=0.1);
set(p,'FaceVertexCData', [d(fidx(1:end-1));nan], 'EdgeColor','Flat')
end
Animation
Remix Tree