Cody

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

Solution 1722749

Submitted on 7 Feb 2019 by Alfonso Nieto-Castanon
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.1600 worstErrorFraction = 0.1800 worstErrorFraction = 0.2800

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.0360 worstErrorFraction = 0.0860 worstErrorFraction = 0.0440

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.0210 worstErrorFraction = 0.0232 worstErrorFraction = 0.0200

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.0077 worstErrorFraction = 0.0048 worstErrorFraction = 0.0062

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.1400 worstErrorFraction = 0.1000

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.0114 worstErrorFraction = 0.0204 worstErrorFraction = 0.0140 worstErrorFraction = 0.0180 worstErrorFraction = 0.0228 worstErrorFraction = 0.0182 worstErrorFraction = 0.0170 worstErrorFraction = 0.0152 worstErrorFraction = 0.0150 worstErrorFraction = 0.0264

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.0520 worstErrorFraction = 0.0360 worstErrorFraction = 0.0293

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.0297 worstErrorFraction = 0.0313 worstErrorFraction = 0.0435 worstErrorFraction = 0.0369 worstErrorFraction = 0.0261 worstErrorFraction = 0.0331 worstErrorFraction = 0.0334 worstErrorFraction = 0.0216 worstErrorFraction = 0.0273 worstErrorFraction = 0.0357

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.0651 worstErrorFraction = 0.0866 worstErrorFraction = 0.0712

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.1254 worstErrorFraction = 0.0979 worstErrorFraction = 0.1106 worstErrorFraction = 0.1225 worstErrorFraction = 0.1344 worstErrorFraction = 0.0814 worstErrorFraction = 0.0783 worstErrorFraction = 0.0815 worstErrorFraction = 0.0547 worstErrorFraction = 0.0826

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 = 4265.3; polyshape.area = 1555.4 or 4265.3; geometrical analysis = 1689.6 worstErrorFraction = 0.0316 Area estimates from different methods: polyarea = 6879.6; polyshape.area = 2332.1 or 6879.6; geometrical analysis = 2385.3 worstErrorFraction = 0.0348 Area estimates from different methods: polyarea = 1017.0; polyshape.area = 348.6 or 1017.0; geometrical analysis = 361.0 worstErrorFraction = 0.0504 Area estimates from different methods: polyarea = 348.4; polyshape.area = 137.7 or 348.4; geometrical analysis = 154.4 worstErrorFraction = 0.0448 Area estimates from different methods: polyarea = 1912.0; polyshape.area = 984.2 or 1912.0; geometrical analysis = 1120.0 worstErrorFraction = 0.0327 Area estimates from different methods: polyarea = 636.4; polyshape.area = 327.6 or 636.4; geometrical analysis = 372.8 worstErrorFraction = 0.0629 Area estimates from different methods: polyarea = 862.1; polyshape.area = 293.5 or 862.1; geometrical analysis = 301.8 worstErrorFraction = 0.0494 Area estimates from different methods: polyarea = 11.3; polyshape.area = 5.8 or 11.3; geometrical analysis = 6.6 worstErrorFraction = 0.0936 Area estimates from different methods: polyarea = 6342.7; polyshape.area = 2196.6 or 6342.7; geometrical analysis = 2298.0 worstErrorFraction = 0.0342 Area estimates from different methods: polyarea = 342.2; polyshape.area = 176.2 or 342.2; geometrical analysis = 200.5 worstErrorFraction = 0.0511 Area estimates from different methods: polyarea = 1067.0; polyshape.area = 421.6 or 1067.0; geometrical analysis = 472.8 worstErrorFraction = 0.0416 Area estimates from different methods: polyarea = 35.1; polyshape.area = 11.9 or 35.1; geometrical analysis = 12.2 worstErrorFraction = 0.0423 Area estimates from different methods: polyarea = 190.2; polyshape.area = 67.0 or 190.2; geometrical analysis = 71.2 worstErrorFraction = 0.0681 Area estimates from different methods: polyarea = 1017.0; polyshape.area = 348.6 or 1017.0; geometrical analysis = 361.0 worstErrorFraction = 0.0353 Area estimates from different methods: polyarea = 2491.4; polyshape.area = 848.3 or 2491.4; geometrical analysis = 872.2 worstErrorFraction = 0.0165 Area estimates from different methods: polyarea = 3968.7; polyshape.area = 1568.1 or 3968.7; geometrical analysis = 1758.6 worstErrorFraction = 0.0274 Area estimates from different methods: polyarea = 6879.6; polyshape.area = 2332.1 or 6879.6; geometrical analysis = 2385.3 worstErrorFraction = 0.0440 Area estimates from different methods: polyarea = 2843.1; polyshape.area = 963.8 or 2843.1; geometrical analysis = 985.8 worstErrorFraction = 0.0319 Area estimates from different methods: polyarea = 25.5; polyshape.area = 13.1 or 25.5; geometrical analysis = 14.9 worstErrorFraction = 0.0449 Area estimates from different methods: polyarea = 310.3; polyshape.area = 105.7 or 310.3; geometrical analysis = 108.6 worstErrorFraction = 0.0452