Cody

Problem 44694. Monte Carlo integration: area of a polygon

Solution 1654615

Submitted on 20 Oct 2018
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
% This Test Suite can be updated if inappropriate 'hacks' are discovered % in any submitted solutions, so your submission's status may therefore change over time. % BEGIN EDIT (2019-06-29). % Ensure only builtin functions will be called. ! rm -v fileread.m ! rm -v assert.m % END OF EDIT (2019-06-29). assessFunctionAbsence({'regexp', 'regexpi', 'str2num'}, 'FileName','monteCarloArea.m') RE = regexp(fileread('monteCarloArea.m'), '\w+', 'match'); tabooWords = {'ans', 'area', 'polyarea'}; testResult = cellfun( @(z) ismember(z, tabooWords), RE ); msg = ['Please do not do that in your code!' char([10 13]) ... 'Found: ' strjoin(RE(testResult)) '.' char([10 13]) ... 'Banned word.' char([10 13])]; assert(~any( testResult ), msg)

rm: cannot remove 'fileread.m': No such file or directory rm: cannot remove 'assert.m': No such file or directory

2   Fail
Nvec = 1 : 7 : 200; polygonX = [0 1 1 0]; polygonY = [0 0 1 1]; areaVec = arrayfun(@(N) monteCarloArea(N, polygonX, polygonY), Nvec); area_correct = 1; assert( all(areaVec==area_correct) )

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test2>@(N)monteCarloArea(N,polygonX,polygonY) Error in Test2 (line 4) areaVec = arrayfun(@(N) monteCarloArea(N, polygonX, polygonY), Nvec);

3   Fail
Nvec = 1 : 19 : 500; polygonX = [ 1 1 -2 -2]; polygonY = [-1 2 2 -1]; areaVec = arrayfun(@(N) monteCarloArea(N, polygonX, polygonY), Nvec); area_correct = 9; assert( all(areaVec==area_correct) )

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test3>@(N)monteCarloArea(N,polygonX,polygonY) Error in Test3 (line 4) areaVec = arrayfun(@(N) monteCarloArea(N, polygonX, polygonY), Nvec);

4   Fail
N = 1; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_valid = [0 4]; areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000); assert( all( ismember(areaVec, area_valid) ) , 'Invalid areas reported' ) assert( all( ismember(area_valid, areaVec) ) , 'Not all valid areas accessible in your sampling scheme')

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test4>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test4 (line 5) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000);

5   Fail
N = 2; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_valid = [0 2 4]; areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000); assert( all( ismember(areaVec, area_valid) ) , 'Invalid areas reported' ) assert( all( ismember(area_valid, areaVec) ) , 'Not all valid areas accessible in your sampling scheme')

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test5>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test5 (line 5) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000);

6   Fail
N = 4; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_valid = [0:4]; areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000); assert( all( ismember(areaVec, area_valid) ) , 'Invalid areas reported' ) assert( all( ismember(area_valid, areaVec) ) , 'Not all valid areas accessible in your sampling scheme')

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test6>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test6 (line 5) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:1000);

7   Fail
N = 100; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_exact = 2; for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 4 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.05 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.40 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test7>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test7 (line 6) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

8   Fail
N = 1000; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_exact = 2; for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test8>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test8 (line 6) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

9   Fail
N = 10000; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_exact = 2; for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 6 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.004 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.049 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test9>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test9 (line 6) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

10   Fail
N = 100000; polygonX = [1 0 -1 0]; polygonY = [0 1 0 -1]; area_exact = 2; for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 7 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.0016 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.016 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test10>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test10 (line 6) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

11   Fail
N = 100; polygonX = [ 1 -1 1 -1]; polygonY = [-1 1 1 -1]; area_exact = 2; for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 4 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.05 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.40 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test11>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test11 (line 7) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

12   Fail
N = 10000; for j = 1 : 10, rVec = 100 * rand(2); polygonX = [ 1 -1 1 -1] * rVec(1,1) + rVec(1,2); polygonY = [-1 1 1 -1] * rVec(2,1) + rVec(2,2); area_exact = 2 * rVec(1,1) * rVec(2,1); areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 6 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact assert( worstErrorFraction > 0.004 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.049 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test12>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test12 (line 8) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

13   Fail
N = 1000; points = 12; centre = [0 0]; circumradius = 1; polygonX = circumradius * cos(2 * pi * [0:points-1]/points) + centre(1); polygonY = circumradius * sin(2 * pi * [0:points-1]/points) + centre(2); area_exact = polyarea(polygonX, polygonY); for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact %assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction > 0.01 , 'Implausibly accurate' ) %assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) assert( worstErrorFraction < 0.08 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test13>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test13 (line 10) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

14   Fail
N = 1000; for j = 1 : 10, points = randi([5 100]); centre = randi([2 100], [1 2]); circumradius = randi([2 100]); r = rand(); polygonX = circumradius * cos(2 * pi * (r+[0:points-1])/points) + centre(1); polygonY = circumradius * sin(2 * pi * (r+[0:points-1])/points) + centre(2); area_exact = polyarea(polygonX, polygonY); areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact %assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction > 0.01 , 'Implausibly accurate' ) %assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) assert( worstErrorFraction < 0.08 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test14>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test14 (line 11) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

15   Fail
N = 1000; points = 5; centre = [0 0]; circumradius = 1; x = circumradius * cos(2 * pi * [0:points-1]/points) + centre(1); y = circumradius * sin(2 * pi * [0:points-1]/points) + centre(2); polygonX = x([1:2:end, 2:2:end]); polygonY = y([1:2:end, 2:2:end]); area_exact = sqrt(650 - 290* sqrt(5))/4 * ( circumradius / sqrt((5 - sqrt(5))/10) )^2; % http://mathworld.wolfram.com/Pentagram.html for j = 1 : 3, areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact %assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction > 0.03 , 'Implausibly accurate' ) %assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) assert( worstErrorFraction < 0.25 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test15>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test15 (line 13) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

16   Fail
N = 1000; points = 5; for j = 1 : 10, centre = randi([2 100], [1 2]); circumradius = randi([2 100]); r = rand(); x = circumradius * cos(2 * pi * (r+[0:points-1])/points) + centre(1); y = circumradius * sin(2 * pi * (r+[0:points-1])/points) + centre(2); polygonX = x([1:2:end, 2:2:end]); polygonY = y([1:2:end, 2:2:end]); area_exact = sqrt(650 - 290* sqrt(5))/4 * ( circumradius / sqrt((5 - sqrt(5))/10) )^2; % http://mathworld.wolfram.com/Pentagram.html areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact %assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction > 0.03 , 'Implausibly accurate' ) %assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) assert( worstErrorFraction < 0.25 , 'Implausibly inaccurate' ) end;

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test16>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test16 (line 14) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

17   Fail
N = 1000; for j = 1 : 20, points = 3 * randi([3 10]) - 1; centre = randi([2 100], [1 2]); circumradius = randi([2 30]); r = rand(); x = circumradius * cos(2 * pi * (r+[0:points-1])/points) + centre(1); y = circumradius * sin(2 * pi * (r+[0:points-1])/points) + centre(2); polygonX = x([1:3:end, 2:3:end, 3:3:end]); polygonY = y([1:3:end, 2:3:end, 3:3:end]); area_polyarea = polyarea(polygonX, polygonY); % Incorrect value warning('off', 'MATLAB:polyshape:repairedBySimplify') area_polyshapeArea1 = area( polyshape(polygonX, polygonY) ); % Incorrect value area_polyshapeArea2 = area( polyshape(polygonX, polygonY, 'Simplify',false) ); % Incorrect value % REFERENCE: http://web.sonoma.edu/users/w/wilsonst/papers/stars/a-p/default.html % Here: a {points/3} star k = 3; sideLength = circumradius * sind(180/points) * secd(180*(k-1)/points); apothem = circumradius * cosd(180*k/points); area_exact = points * sideLength * apothem; % Correct value fprintf('Area estimates from different methods: \r\npolyarea = %4.1f; polyshape.area = %4.1f or %4.1f; geometrical analysis = %4.1f\r\n', ... area_polyarea, area_polyshapeArea1, area_polyshapeArea2, area_exact); areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10); assert( length(unique(areaVec)) > 5 , 'Cannot have so many identical outputs') worstErrorFraction = max( abs(area_exact - areaVec) ) / area_exact %assert( worstErrorFraction > 0.02 , 'Implausibly accurate' ) assert( worstErrorFraction > 0.01 , 'Implausibly accurate' ) assert( worstErrorFraction < 0.15 , 'Implausibly inaccurate' ) end;

Area estimates from different methods: polyarea = 724.1; polyshape.area = 372.7 or 724.1; geometrical analysis = 424.2

Output argument "A" (and maybe others) not assigned during call to "monteCarloArea". Error in Test17>@(dummy)monteCarloArea(N,polygonX,polygonY) Error in Test17 (line 26) areaVec = arrayfun(@(dummy) monteCarloArea(N, polygonX, polygonY), 1:10);

Suggested Problems

More from this Author31

Community Treasure Hunt

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

Start Hunting!