Cody

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

Solution 1576522

Submitted on 5 Jul 2018 by Tim
• Size: 59
• This is the leading solution.
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.1800 worstErrorFraction = 0.2600 worstErrorFraction = 0.1400

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.0420 worstErrorFraction = 0.0460 worstErrorFraction = 0.0680

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.0288 worstErrorFraction = 0.0242 worstErrorFraction = 0.0172

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.0054 worstErrorFraction = 0.0071 worstErrorFraction = 0.0056

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

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.0134 worstErrorFraction = 0.0290 worstErrorFraction = 0.0240 worstErrorFraction = 0.0246 worstErrorFraction = 0.0216 worstErrorFraction = 0.0238 worstErrorFraction = 0.0168 worstErrorFraction = 0.0106 worstErrorFraction = 0.0142 worstErrorFraction = 0.0160

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.0453 worstErrorFraction = 0.0333 worstErrorFraction = 0.0333

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.0241 worstErrorFraction = 0.0321 worstErrorFraction = 0.0419 worstErrorFraction = 0.0593 worstErrorFraction = 0.0244 worstErrorFraction = 0.0410 worstErrorFraction = 0.0408 worstErrorFraction = 0.0358 worstErrorFraction = 0.0308 worstErrorFraction = 0.0380

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.0544 worstErrorFraction = 0.1019 worstErrorFraction = 0.1096

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.0764 worstErrorFraction = 0.0676 worstErrorFraction = 0.0654 worstErrorFraction = 0.1154 worstErrorFraction = 0.1046 worstErrorFraction = 0.1153 worstErrorFraction = 0.1050 worstErrorFraction = 0.0818 worstErrorFraction = 0.0664 worstErrorFraction = 0.1190

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 = 1263.6; polyshape.area = 428.3 or 1263.6; geometrical analysis = 438.1 worstErrorFraction = 0.0466 Area estimates from different methods: polyarea = 5252.9; polyshape.area = 1800.5 or 5252.9; geometrical analysis = 1864.5 worstErrorFraction = 0.0359 Area estimates from different methods: polyarea = 1535.5; polyshape.area = 559.9 or 1535.5; geometrical analysis = 608.3 worstErrorFraction = 0.0162 Area estimates from different methods: polyarea = 282.8; polyshape.area = 145.6 or 282.8; geometrical analysis = 165.7 worstErrorFraction = 0.0935 Area estimates from different methods: polyarea = 4613.4; polyshape.area = 1682.3 or 4613.4; geometrical analysis = 1827.5 worstErrorFraction = 0.0703 Area estimates from different methods: polyarea = 920.0; polyshape.area = 363.5 or 920.0; geometrical analysis = 407.7 worstErrorFraction = 0.0670 Area estimates from different methods: polyarea = 1061.8; polyshape.area = 359.9 or 1061.8; geometrical analysis = 368.1 worstErrorFraction = 0.0312 Area estimates from different methods: polyarea = 840.5; polyshape.area = 288.1 or 840.5; geometrical analysis = 298.3 worstErrorFraction = 0.0343 Area estimates from different methods: polyarea = 1974.4; polyshape.area = 669.3 or 1974.4; geometrical analysis = 684.6 worstErrorFraction = 0.0281 Area estimates from different methods: polyarea = 136.1; polyshape.area = 53.8 or 136.1; geometrical analysis = 60.3 worstErrorFraction = 0.0459 Area estimates from different methods: polyarea = 2338.1; polyshape.area = 809.7 or 2338.1; geometrical analysis = 847.1 worstErrorFraction = 0.0384 Area estimates from different methods: polyarea = 7379.8; polyshape.area = 2501.7 or 7379.8; geometrical analysis = 2558.7 worstErrorFraction = 0.0257 Area estimates from different methods: polyarea = 1496.2; polyshape.area = 770.1 or 1496.2; geometrical analysis = 876.5 worstErrorFraction = 0.0972 Area estimates from different methods: polyarea = 229.1; polyshape.area = 117.9 or 229.1; geometrical analysis = 134.2 worstErrorFraction = 0.0816 Area estimates from different methods: polyarea = 7249.9; polyshape.area = 2468.7 or 7249.9; geometrical analysis = 2538.1 worstErrorFraction = 0.0181 Area estimates from different methods: polyarea = 862.1; polyshape.area = 293.5 or 862.1; geometrical analysis = 301.8 worstErrorFraction = 0.0381 Area estimates from different methods: polyarea = 1629.2; polyshape.area = 838.6 or 1629.2; geometrical analysis = 954.3 worstErrorFraction = 0.0721 Area estimates from different methods: polyarea = 5681.5; polyshape.area = 1947.4 or 5681.5; geometrical analysis = 2016.6 worstErrorFraction = 0.0344 Area estimates from different methods: polyarea = 2843.1; polyshape.area = 963.8 or 2843.1; geometrical analysis = 985.8 worstErrorFraction = 0.0256 Area estimates from different methods: polyarea = 6399.1; polyshape.area = 2255.4 or 6399.1; geometrical analysis = 2395.3 worstErrorFraction = 0.0445