% prepare figure and axis hFig = figure('menu','none', 'DoubleBuffer','on'); hAx = axes('Parent',hFig, 'XLim',[0 1], 'YLim',[0 1], 'Box','on', ... 'XLimMode','manual', 'YLimMode','manual'); axis(hAx, 'equal', 'square') % points and text hLine = line('XData',[], 'YData',[], 'LineStyle','none', 'Marker','.', 'Color','b', 'MarkerSize',1); hTxt = text(0.8,0.9, '0', 'HorizontalAlignment','center', 'BackgroundColor','w'); % draw circle arc theta = linspace(0,pi/2,100); hCircle = line(cos(theta), sin(theta), 'Color','r', 'LineWidth',4); %uistack(hCircle,'top') % incrementally increase number of points drawn X = rand(1e5,2); N = round(linspace(1,size(X,1),200)); for i=1:numel(N) % update points set(hLine, 'XData',X(1:N(i),1), 'YData',X(1:N(i),2)) % update pi approximation text piApprox = 4*( sum(sum(X(1:N(i),:).^2,2)<=1) ./ N(i) ); set(hTxt, 'String',sprintf('%.8f',piApprox)) % refresh drawnow if ~ishandle(hFig), break; end end