MATLAB: About Matlab Code

golf

I want to ask about matlab's code of this clip >>http://www.youtube.com/watch?v=EbC0eP0Dq9A<<
Step for Algorithm .. 1. fix number of point for random . 2. when it ploted, separate which point in a circle and another out of circle. 3. after point are random finish, calculate ratio in a picture. by Ratio = (Rectangle's areas * all of point in a circle) / number of point for random
Sorry, If I spell grammar wrong..

Best Answer

  • clf
    r=1;
    rectangle('Position',[-1,-1,2*r,2*r],'Curvature',[1,1])
    axis([-1 1 -1 1])
    hold on
    n=1000;
    x=-1+2*r*rand(n,1);
    y=-1+2*r*rand(n,1);
    plot(x,y,'r.');
    in=sum((x.^2+y.^2)<r);
    (2*r)^2*in/n %Thanks Sean, (2*r)^2 is the area of the circle
    Increasing n gives better approximations for the value of pi.