I'm new to matlab I'm having trouble with this one.
古いコメントを表示
Write a function called mypi which consumes a number that specifies the required accuracy and approximates the value of pi to that accuracy. Use the following algorithm to approximate pi:
Think about a quarter circle inside of a unit square (quarter circle has area pi/4). Pick a random point inside the square. If it is in the quarter circle, you get a "hit", otherwise it's a "miss". The approximate area of the quarter circle will be given by the number of "hits" divided by the number of points chosen.
The function should repeat the process of counting hits and misses until at least 10,000 tries have been made or pi is within the required accuracy. It should return the the estimate for pi.
5 件のコメント
Ingrid
2016 年 1 月 27 日
what have you tried? if you do not show any attempt you probably have no problem with the matlab coding but with the problem itself
jose sanchez
2016 年 1 月 27 日
編集済み: Guillaume
2016 年 1 月 27 日
>> N = 1e6;
>> X = rand(N,1);
>> Y = rand(N,1);
>> I = sqrt(X.^2 + Y.^2)<=1;
>> 4*nnz(I)/N
ans = 3.1395
Ingrid
2016 年 1 月 27 日
Stephen - how would you check with this vectorized solution if the precision is found sooner?
Stephen23
2016 年 1 月 27 日
I did not mean to imply that this was a complete solution for the OP: I would have given it as an Answer if it was! My comment simply shows how vectorized code can be used to estimate π using this concept, i.e. the "basic task".
To meet the precision condition requires either a loop of some kind, or else calculating a large number of X&Y values and finding the first subset which meets the condition.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!