How can I estimate pi by using a unit hexagon in Matlab?
조회 수: 3 (최근 30일)
이전 댓글 표시

We can do easily on estimate pi using 1-by-1 square tiling, where codes are below. But how about “hexagons”? Still use for loop?
% Pi Via Tiling
% Enter the disk radius...
clc
n = input('Enter an integral radius n: ');
% Tile just the first quadrant, then multiply by four...
N1 = 0;
for k = 1:n
% Add in the number of uncut tiles in row k...
m = floor(sqrt(n^2 - k^2));
N1 = N1 + m;
end
% Display the estimate...
rho_n = 4*N1/n^2;
clc
fprintf('n = %d\n',n)
fprintf('rho_n = %12.8f\n',rho_n)
fprintf('Error = %12.8f\n',abs(pi-rho_n))
댓글 수: 2
Image Analyst
2017년 3월 19일
The problem statement doesn't mention pi. So why do you want to measure it?
답변 (2개)
John D'Errico
2017년 3월 19일
편집: John D'Errico
2017년 3월 19일
I won't do your homework for you. So you need to think. I'll give you a few (huge) hints:
First, is there some symmetry you can take advantage of? It won't kill you if you cannot see this, but symmetries are often valuable tools to employ. They reduce the programming effort, and the time to solve a problem.
Can you define the coordinates of the center of any tile? The homework even gave you a big hint there, in the form of * versus o tiles. Come on. This is basic geometry.
If you know the center of any tile, then can you find the coordinates of the 6 corners of the hex? Again, basic geometry, plus a little trig. Start with a hex centered at (0,0). Are all the hexes of fundamentally the same shape and size? Is there some simple, additive operation you can do?
If a hex is FULLY inside the circle then you will count it. How can you tell if a hex is inside a circle? Is there a simple scheme you can find, based on the vertices of the hex in question?
When you see a problem too big and difficult for you to get your head around, break it into small, bite sized chunks. Make big problems into small ones, then solve the small problems, one at a time. Put it all together, and you are done. This advice applies to any problem you will be forced to solve in the future.
Or, if all else fails, you can just do this:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
Actually, that last digit was rounded off. It would take REALLY small hexagons to get that degree of accuracy though. :)
댓글 수: 6
John D'Errico
2017년 4월 3일
편집: John D'Errico
2017년 4월 3일
Is this homework? :) It still sort of is homework, IF you are using it to learn MATLAB, or anything about mathematics. You gain by the doing. I never have a problem in helping someone who is making an effort though, someone who wants to learn.
I'll come back to this later when I have more time to read through it. Must run out this am though.
The one thing I saw immediately is this:
if y_star==sqrt(3)/2
A BAD idea. NEVER test for exact equality of floating point numbers like that. Use a tolerance instead. Perhaps this:
if abs(y_star - sqrt(3)/2) < 10*eps
Image Analyst
2017년 3월 19일
A little bit of code adapted from the FAQ to get you started:
r = 11.1234; % Whatever.
X = [0 : 3 : ceil(r), 0 : -3 : floor(-r)];
Y = [0 : 3 : ceil(r), 0 : -3 : floor(-r)];
[x1, y1] = meshgrid(X, Y);
x2 = x1 + 1.5;
y2 = y1 + 1.5;
x = [x1(:), x2(:)];
y = [y1(:), y2(:)];
plot(x, y, 'b.', 'MarkerSize', 20);
grid on;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% Plot the circle.
centerX = 0;
centerY = 0;
hold on;
rectangle('Position',[centerX - r, centerY - r, r*2, r*2],...
'Curvature',[1,1],...
'EdgeColor', 'r',...
'LineWidth', 2,...
'FaceColor','none');
axis square;

See if you can figure out, from the first code chunk in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F how to count how many of the (x,y) points are inside of the circle.
Use input or inputdlg() to ask the user for the radius.
댓글 수: 2
John D'Errico
2017년 3월 19일
A requirement was that a hexagon needs to be ENTIRELY inside the circle to be counted. So be careful.
Image Analyst
2017년 3월 20일
Yes, it's a bit tricky. I didn't show the hexagons themselves, just their centers, and you can't just see if the centers are within the circle. You have to see if any little tip of the hexagon extends outside of the circle. There will be math ahead, so be prepared.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!