Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

pi_approximation

By: a guest on Apr 13th, 2013  |  syntax: MatLab  |  size: 0.96 KB  |  hits: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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