Cody

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

Solution 1576521

Submitted on 5 Jul 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   Fail
% 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

Please do not do that in your code! Found: ans. Banned word.

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.1800 worstErrorFraction = 0.2400

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.0480 worstErrorFraction = 0.0440 worstErrorFraction = 0.0420

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.0168 worstErrorFraction = 0.0244 worstErrorFraction = 0.0190

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.0069 worstErrorFraction = 0.0055 worstErrorFraction = 0.0059

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.1200 worstErrorFraction = 0.2200 worstErrorFraction = 0.1600

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.0168 worstErrorFraction = 0.0132 worstErrorFraction = 0.0174 worstErrorFraction = 0.0132 worstErrorFraction = 0.0202 worstErrorFraction = 0.0144 worstErrorFraction = 0.0128 worstErrorFraction = 0.0122 worstErrorFraction = 0.0172 worstErrorFraction = 0.0330

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.0413 worstErrorFraction = 0.0320

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.0292 worstErrorFraction = 0.0317 worstErrorFraction = 0.0390 worstErrorFraction = 0.0462 worstErrorFraction = 0.0216 worstErrorFraction = 0.0273 worstErrorFraction = 0.0632 worstErrorFraction = 0.0270 worstErrorFraction = 0.0304 worstErrorFraction = 0.0318

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.0667 worstErrorFraction = 0.0974 worstErrorFraction = 0.1004

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.0577 worstErrorFraction = 0.0882 worstErrorFraction = 0.0482 worstErrorFraction = 0.0784 worstErrorFraction = 0.1015 worstErrorFraction = 0.0750 worstErrorFraction = 0.0919 worstErrorFraction = 0.0830 worstErrorFraction = 0.1339 worstErrorFraction = 0.0494

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 = 3567.8; polyshape.area = 1235.6 or 3567.8; geometrical analysis = 1292.6 worstErrorFraction = 0.0429 Area estimates from different methods: polyarea = 5739.4; polyshape.area = 2093.0 or 5739.4; geometrical analysis = 2273.5 worstErrorFraction = 0.0421 Area estimates from different methods: polyarea = 25.5; polyshape.area = 13.1 or 25.5; geometrical analysis = 14.9 worstErrorFraction = 0.0891 Area estimates from different methods: polyarea = 436.8; polyshape.area = 159.3 or 436.8; geometrical analysis = 173.0 worstErrorFraction = 0.0212 Area estimates from different methods: polyarea = 4578.4; polyshape.area = 1809.0 or 4578.4; geometrical analysis = 2028.8 worstErrorFraction = 0.0369 Area estimates from different methods: polyarea = 5827.5; polyshape.area = 1984.3 or 5827.5; geometrical analysis = 2040.2 worstErrorFraction = 0.0274 Area estimates from different methods: polyarea = 1491.3; polyshape.area = 525.6 or 1491.3; geometrical analysis = 558.2 worstErrorFraction = 0.0417 Area estimates from different methods: polyarea = 2746.8; polyshape.area = 968.1 or 2746.8; geometrical analysis = 1028.2 worstErrorFraction = 0.0275 Area estimates from different methods: polyarea = 5143.6; polyshape.area = 1812.9 or 5143.6; geometrical analysis = 1925.4 worstErrorFraction = 0.0342 Area estimates from different methods: polyarea = 3361.8; polyshape.area = 1152.3 or 3361.8; geometrical analysis = 1193.3 worstErrorFraction = 0.0276 Area estimates from different methods: polyarea = 229.1; polyshape.area = 117.9 or 229.1; geometrical analysis = 134.2 worstErrorFraction = 0.1191 Area estimates from different methods: polyarea = 760.9; polyshape.area = 268.2 or 760.9; geometrical analysis = 284.8 worstErrorFraction = 0.0342 Area estimates from different methods: polyarea = 310.3; polyshape.area = 105.7 or 310.3; geometrical analysis = 108.6 worstErrorFraction = 0.0293 Area estimates from different methods: polyarea = 441.0; polyshape.area = 174.2 or 441.0; geometrical analysis = 195.4 worstErrorFraction = 0.0397 Area estimates from different methods: polyarea = 1095.7; polyshape.area = 386.2 or 1095.7; geometrical analysis = 410.1 worstErrorFraction = 0.0323 Area estimates from different methods: polyarea = 3680.2; polyshape.area = 1454.1 or 3680.2; geometrical analysis = 1630.8 worstErrorFraction = 0.0252 Area estimates from different methods: polyarea = 561.6; polyshape.area = 190.4 or 561.6; geometrical analysis = 194.7 worstErrorFraction = 0.0279 Area estimates from different methods: polyarea = 2061.9; polyshape.area = 1061.3 or 2061.9; geometrical analysis = 1207.8 worstErrorFraction = 0.0953 Area estimates from different methods: polyarea = 215.5; polyshape.area = 73.4 or 215.5; geometrical analysis = 75.4 worstErrorFraction = 0.0272 Area estimates from different methods: polyarea = 348.4; polyshape.area = 137.7 or 348.4; geometrical analysis = 154.4 worstErrorFraction = 0.0610

### Community Treasure Hunt

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

Start Hunting!