About Matlab Code

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..

1 件のコメント

Techit
Techit 2011 年 7 月 12 日
yeah, thx for all comments :)
but i need some guild line for a code
because i can't understand that youtube comments
thanks agian
and i'm sorry for my newbie in this matlab

サインインしてコメントする。

 採用された回答

Paulo Silva
Paulo Silva 2011 年 7 月 12 日

0 投票

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.

9 件のコメント

Techit
Techit 2011 年 7 月 12 日
Paulo, Thx for your answer !
I want to know about code of this >> Monte Carlo-method is used here to approximate pi <<
Paulo Silva
Paulo Silva 2011 年 7 月 12 日
It's fixed now, thanks Sean for pointing out the obvious error on the code
Techit
Techit 2011 年 7 月 12 日
@Paulo Silva , Can u show me about code, If i want differnce color between point in/out a circle,
Paulo Silva
Paulo Silva 2011 年 7 月 12 日
I don't know what you want, just guessing, add this code to the end of the code I provided in my answer
clf
idx=(x.^2+y.^2)<r;
hold on
plot(x(~idx),y(~idx),'r')
plot(x(idx),y(idx))
Techit
Techit 2011 年 7 月 12 日
Yess, u understand that i want to ask, I have last Question .. how i display output(Pi), like cout or printf?
Paulo Silva
Paulo Silva 2011 年 7 月 12 日
fprintf('The aproximated value of pi is %g\n',(2*r)^2*in/n)
Techit
Techit 2011 年 7 月 12 日
I want to show Pi on Figure, Can I do?
if it can, what's a code ??
Paulo Silva
Paulo Silva 2011 年 7 月 12 日
text(-0.5,1.1,['aproximmated \pi value=' num2str((2*r)^2*in/n)])
Techit
Techit 2011 年 7 月 12 日
Thank you very much for your help.
> ขอบคุณครับ < I'm from Thailand :)

サインインしてコメントする。

その他の回答 (2 件)

Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日

0 投票

I think the answer to your question is already answered in the video comments.

9 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 7 月 12 日
Agree
Techit
Techit 2011 年 7 月 12 日
I want to know about Code of this clip :)
Techit
Techit 2011 年 7 月 12 日
i can't imagine it cus i'm just a beginner
Paulo Silva
Paulo Silva 2011 年 7 月 12 日
Sean and Oleg are right, the answer is there and it's easy enough for anyone to replicate the code in just a few simple lines.
Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日
okay,
help rand
help le
help power
is LITERALLY all you need.
Oleg Komarov
Oleg Komarov 2011 年 7 月 12 日
Division? (troll)
Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日
still need the divide though... darn.
Walter Roberson
Walter Roberson 2011 年 7 月 12 日
Shouldn't that be
help rdivide
rather than ldivide ?
Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日
hmmm lt or le? The video says lt, but points on the circumference are part of the circle...

サインインしてコメントする。

Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日

0 投票

Alright - darkness golf
f=@(n)etc
I can do it with 38 characters (lt); 39 (le).

カテゴリ

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by