• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
2%
SHARE
TWEET

# pi_approximation

a guest Apr 13th, 2013 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. % prepare figure and axis
2. hFig = figure('menu','none', 'DoubleBuffer','on');
3. hAx = axes('Parent',hFig, 'XLim',[0 1], 'YLim',[0 1], 'Box','on', ...
4.         'XLimMode','manual', 'YLimMode','manual');
5. axis(hAx, 'equal', 'square')
6.
7. % points and text
8. hLine = line('XData',[], 'YData',[], 'LineStyle','none', 'Marker','.', 'Color','b', 'MarkerSize',1);
9. hTxt = text(0.8,0.9, '0', 'HorizontalAlignment','center', 'BackgroundColor','w');
10.
11. % draw circle arc
12. theta = linspace(0,pi/2,100);
13. hCircle = line(cos(theta), sin(theta), 'Color','r', 'LineWidth',4);
14. %uistack(hCircle,'top')
15.
16. % incrementally increase number of points drawn
17. X = rand(1e5,2);
18. N = round(linspace(1,size(X,1),200));
19. for i=1:numel(N)
20.         % update points
21.         set(hLine, 'XData',X(1:N(i),1), 'YData',X(1:N(i),2))
22.
23.         % update pi approximation text
24.         piApprox = 4*( sum(sum(X(1:N(i),:).^2,2)<=1) ./ N(i) );
25.         set(hTxt, 'String',sprintf('%.8f',piApprox))
26.
27.         % refresh
28.         drawnow
29.         if ~ishandle(hFig), break; end
30. end
RAW Paste Data
Top