Cody

# Problem 44694. Monte Carlo integration: area of a polygon

Solution 1624410

Submitted on 8 Sep 2018 by William
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   Pass
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) )

3   Pass
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) )

4   Pass
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')

5   Pass
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')

6   Pass
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')

7   Pass
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;

worstErrorFraction = 0.1400 worstErrorFraction = 0.1000 worstErrorFraction = 0.1800

8   Pass
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;

worstErrorFraction = 0.0560 worstErrorFraction = 0.0460 worstErrorFraction = 0.0740

9   Pass
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;

worstErrorFraction = 0.0106 worstErrorFraction = 0.0186 worstErrorFraction = 0.0144

10   Pass
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;

worstErrorFraction = 0.0064 worstErrorFraction = 0.0042 worstErrorFraction = 0.0057

11   Pass
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;

worstErrorFraction = 0.2000 worstErrorFraction = 0.2000 worstErrorFraction = 0.1400

12   Pass
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;

worstErrorFraction = 0.0200 worstErrorFraction = 0.0162 worstErrorFraction = 0.0172 worstErrorFraction = 0.0162 worstErrorFraction = 0.0208 worstErrorFraction = 0.0104 worstErrorFraction = 0.0160 worstErrorFraction = 0.0266 worstErrorFraction = 0.0166 worstErrorFraction = 0.0176

13   Pass
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;

worstErrorFraction = 0.0307 worstErrorFraction = 0.0547 worstErrorFraction = 0.0520

14   Pass
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;

worstErrorFraction = 0.0492 worstErrorFraction = 0.0322 worstErrorFraction = 0.0259 worstErrorFraction = 0.0256 worstErrorFraction = 0.0351 worstErrorFraction = 0.0231 worstErrorFraction = 0.0234 worstErrorFraction = 0.0324 worstErrorFraction = 0.0474 worstErrorFraction = 0.0266

15   Pass
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;

worstErrorFraction = 0.1019 worstErrorFraction = 0.0698 worstErrorFraction = 0.0774

16   Pass
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;

worstErrorFraction = 0.0819 worstErrorFraction = 0.0506 worstErrorFraction = 0.0562 worstErrorFraction = 0.1255 worstErrorFraction = 0.0464 worstErrorFraction = 0.0493 worstErrorFraction = 0.0636 worstErrorFraction = 0.0657 worstErrorFraction = 0.1052 worstErrorFraction = 0.1175

17   Pass
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 = 75.6; polyshape.area = 25.9 or 75.6; geometrical analysis = 26.8 worstErrorFraction = 0.0267 Area estimates from different methods: polyarea = 1585.7; polyshape.area = 549.1 or 1585.7; geometrical analysis = 574.5 worstErrorFraction = 0.0314 Area estimates from different methods: polyarea = 2843.1; polyshape.area = 963.8 or 2843.1; geometrical analysis = 985.8 worstErrorFraction = 0.0336 Area estimates from different methods: polyarea = 862.1; polyshape.area = 293.5 or 862.1; geometrical analysis = 301.8 worstErrorFraction = 0.0247 Area estimates from different methods: polyarea = 1393.7; polyshape.area = 550.7 or 1393.7; geometrical analysis = 617.6 worstErrorFraction = 0.0351 Area estimates from different methods: polyarea = 436.8; polyshape.area = 159.3 or 436.8; geometrical analysis = 173.0 worstErrorFraction = 0.0286 Area estimates from different methods: polyarea = 2634.9; polyshape.area = 1041.1 or 2634.9; geometrical analysis = 1167.6 worstErrorFraction = 0.0638 Area estimates from different methods: polyarea = 7564.2; polyshape.area = 2592.7 or 7564.2; geometrical analysis = 2684.9 worstErrorFraction = 0.0318 Area estimates from different methods: polyarea = 2378.7; polyshape.area = 1224.4 or 2378.7; geometrical analysis = 1393.4 worstErrorFraction = 0.0461 Area estimates from different methods: polyarea = 3043.6; polyshape.area = 1072.7 or 3043.6; geometrical analysis = 1139.3 worstErrorFraction = 0.0323 Area estimates from different methods: polyarea = 2071.1; polyshape.area = 717.2 or 2071.1; geometrical analysis = 750.4 worstErrorFraction = 0.0390 Area estimates from different methods: polyarea = 3567.8; polyshape.area = 1235.6 or 3567.8; geometrical analysis = 1292.6 worstErrorFraction = 0.0304 Area estimates from different methods: polyarea = 537.9; polyshape.area = 184.4 or 537.9; geometrical analysis = 190.9 worstErrorFraction = 0.0317 Area estimates from different methods: polyarea = 1456.9; polyshape.area = 496.1 or 1456.9; geometrical analysis = 510.0 worstErrorFraction = 0.0265 Area estimates from different methods: polyarea = 35.1; polyshape.area = 11.9 or 35.1; geometrical analysis = 12.2 worstErrorFraction = 0.0283 Area estimates from different methods: polyarea = 1263.6; polyshape.area = 428.3 or 1263.6; geometrical analysis = 438.1 worstErrorFraction = 0.0259 Area estimates from different methods: polyarea = 49.0; polyshape.area = 19.4 or 49.0; geometrical analysis = 21.7 worstErrorFraction = 0.0775 Area estimates from different methods: polyarea = 2746.8; polyshape.area = 968.1 or 2746.8; geometrical analysis = 1028.2 worstErrorFraction = 0.0280 Area estimates from different methods: polyarea = 2199.0; polyshape.area = 775.1 or 2199.0; geometrical analysis = 823.1 worstErrorFraction = 0.0228 Area estimates from different methods: polyarea = 1263.6; polyshape.area = 428.3 or 1263.6; geometrical analysis = 438.1 worstErrorFraction = 0.0480

### Community Treasure Hunt

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

Start Hunting!