Cody

Problem 44694. Monte Carlo integration: area of a polygon

Solution 1624409

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

This solution is outdated. To rescore this solution, sign in.

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. 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)

Please do not do that in your code! Found: area area. 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.2800 worstErrorFraction = 0.1600 worstErrorFraction = 0.2200

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.0740 worstErrorFraction = 0.1080 worstErrorFraction = 0.0480

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.0196 worstErrorFraction = 0.0210 worstErrorFraction = 0.0164

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.0063 worstErrorFraction = 0.0057 worstErrorFraction = 0.0051

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.1000 worstErrorFraction = 0.1800 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.0160 worstErrorFraction = 0.0190 worstErrorFraction = 0.0184 worstErrorFraction = 0.0198 worstErrorFraction = 0.0234 worstErrorFraction = 0.0178 worstErrorFraction = 0.0184 worstErrorFraction = 0.0190 worstErrorFraction = 0.0148 worstErrorFraction = 0.0132

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.0267 worstErrorFraction = 0.0280 worstErrorFraction = 0.0307

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.0239 worstErrorFraction = 0.0245 worstErrorFraction = 0.0277 worstErrorFraction = 0.0284 worstErrorFraction = 0.0227 worstErrorFraction = 0.0249 worstErrorFraction = 0.0203 worstErrorFraction = 0.0195 worstErrorFraction = 0.0261 worstErrorFraction = 0.0553

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.0820 worstErrorFraction = 0.0590 worstErrorFraction = 0.1158

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.0855 worstErrorFraction = 0.0601 worstErrorFraction = 0.1084 worstErrorFraction = 0.0614 worstErrorFraction = 0.0719 worstErrorFraction = 0.0924 worstErrorFraction = 0.0971 worstErrorFraction = 0.0813 worstErrorFraction = 0.0843 worstErrorFraction = 0.0765

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 = 4025.1; polyshape.area = 1418.7 or 4025.1; geometrical analysis = 1506.7 worstErrorFraction = 0.0246 Area estimates from different methods: polyarea = 5827.5; polyshape.area = 1984.3 or 5827.5; geometrical analysis = 2040.2 worstErrorFraction = 0.0304 Area estimates from different methods: polyarea = 181.0; polyshape.area = 93.2 or 181.0; geometrical analysis = 106.0 worstErrorFraction = 0.0889 Area estimates from different methods: polyarea = 3043.6; polyshape.area = 1072.7 or 3043.6; geometrical analysis = 1139.3 worstErrorFraction = 0.0324 Area estimates from different methods: polyarea = 5469.0; polyshape.area = 1894.0 or 5469.0; geometrical analysis = 1981.5 worstErrorFraction = 0.0318 Area estimates from different methods: polyarea = 6142.0; polyshape.area = 2239.8 or 6142.0; geometrical analysis = 2433.0 worstErrorFraction = 0.0280 Area estimates from different methods: polyarea = 3610.2; polyshape.area = 1316.5 or 3610.2; geometrical analysis = 1430.1 worstErrorFraction = 0.0289 Area estimates from different methods: polyarea = 3706.4; polyshape.area = 1270.4 or 3706.4; geometrical analysis = 1315.6 worstErrorFraction = 0.0390 Area estimates from different methods: polyarea = 61.4; polyshape.area = 22.4 or 61.4; geometrical analysis = 24.3 worstErrorFraction = 0.0302 Area estimates from different methods: polyarea = 840.5; polyshape.area = 288.1 or 840.5; geometrical analysis = 298.3 worstErrorFraction = 0.0373 Area estimates from different methods: polyarea = 478.0; polyshape.area = 246.0 or 478.0; geometrical analysis = 280.0 worstErrorFraction = 0.0683 Area estimates from different methods: polyarea = 5143.6; polyshape.area = 1812.9 or 5143.6; geometrical analysis = 1925.4 worstErrorFraction = 0.0281 Area estimates from different methods: polyarea = 4446.0; polyshape.area = 1523.9 or 4446.0; geometrical analysis = 1578.1 worstErrorFraction = 0.0389 Area estimates from different methods: polyarea = 245.7; polyshape.area = 89.6 or 245.7; geometrical analysis = 97.3 worstErrorFraction = 0.0541 Area estimates from different methods: polyarea = 544.4; polyshape.area = 215.1 or 544.4; geometrical analysis = 241.2 worstErrorFraction = 0.0348 Area estimates from different methods: polyarea = 3034.1; polyshape.area = 1040.0 or 3034.1; geometrical analysis = 1076.9 worstErrorFraction = 0.0528 Area estimates from different methods: polyarea = 2061.9; polyshape.area = 1061.3 or 2061.9; geometrical analysis = 1207.8 worstErrorFraction = 0.0706 Area estimates from different methods: polyarea = 2723.1; polyshape.area = 933.4 or 2723.1; geometrical analysis = 966.5 worstErrorFraction = 0.0263 Area estimates from different methods: polyarea = 1891.0; polyshape.area = 648.2 or 1891.0; geometrical analysis = 671.2 worstErrorFraction = 0.0331 Area estimates from different methods: polyarea = 982.7; polyshape.area = 358.4 or 982.7; geometrical analysis = 389.3 worstErrorFraction = 0.0678