Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 223207

Submitted on 25 Mar 2013 by Matteo Broggi

Correct

31Size
Leading solution size is 16.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%% Test case 1: Square
xy=[0,0;
    1,0;
    1,1;
    0,1;
    0,0];
err=(abs(perimeter1(xy)-4)/(4))*100;
assert(err<.1)
vecs =
     1     0
     0     1
    -1     0
     0    -1
2
Pass
 
%% Test case 2 : Circle
t=[0:pi/100:2*pi,0]';
xy=[cos(t),sin(t)];
err=(abs(perimeter1(xy)-2*pi)/(2*pi))*100;
assert(err<.1)
vecs =
   -0.0005    0.0314
   -0.0015    0.0314
   -0.0025    0.0313
   -0.0034    0.0312
   -0.0044    0.0311
   -0.0054    0.0309
   -0.0064    0.0308
   -0.0073    0.0305
   -0.0083    0.0303
   -0.0092    0.0300
   -0.0102    0.0297
   -0.0111    0.0294
   -0.0120    0.0290
   -0.0129    0.0286
   -0.0138    0.0282
   -0.0147    0.0278
   -0.0156    0.0273
   -0.0164    0.0268
   -0.0172    0.0263
   -0.0181    0.0257
   -0.0189    0.0251
   -0.0196    0.0245
   -0.0204    0.0239
   -0.0211    0.0232
   -0.0219    0.0226
   -0.0226    0.0219
   -0.0232    0.0211
   -0.0239    0.0204
   -0.0245    0.0196
   -0.0251    0.0189
   -0.0257    0.0181
   -0.0263    0.0172
   -0.0268    0.0164
   -0.0273    0.0156
   -0.0278    0.0147
   -0.0282    0.0138
   -0.0286    0.0129
   -0.0290    0.0120
   -0.0294    0.0111
   -0.0297    0.0102
   -0.0300    0.0092
   -0.0303    0.0083
   -0.0305    0.0073
   -0.0308    0.0064
   -0.0309    0.0054
   -0.0311    0.0044
   -0.0312    0.0034
   -0.0313    0.0025
   -0.0314    0.0015
   -0.0314    0.0005
   -0.0314   -0.0005
   -0.0314   -0.0015
   -0.0313   -0.0025
   -0.0312   -0.0034
   -0.0311   -0.0044
   -0.0309   -0.0054
   -0.0308   -0.0064
   -0.0305   -0.0073
   -0.0303   -0.0083
   -0.0300   -0.0092
   -0.0297   -0.0102
   -0.0294   -0.0111
   -0.0290   -0.0120
   -0.0286   -0.0129
   -0.0282   -0.0138
   -0.0278   -0.0147
   -0.0273   -0.0156
   -0.0268   -0.0164
   -0.0263   -0.0172
   -0.0257   -0.0181
   -0.0251   -0.0189
   -0.0245   -0.0196
   -0.0239   -0.0204
   -0.0232   -0.0211
   -0.0226   -0.0219
   -0.0219   -0.0226
   -0.0211   -0.0232
   -0.0204   -0.0239
   -0.0196   -0.0245
   -0.0189   -0.0251
   -0.0181   -0.0257
   -0.0172   -0.0263
   -0.0164   -0.0268
   -0.0156   -0.0273
   -0.0147   -0.0278
   -0.0138   -0.0282
   -0.0129   -0.0286
   -0.0120   -0.0290
   -0.0111   -0.0294
   -0.0102   -0.0297
   -0.0092   -0.0300
   -0.0083   -0.0303
   -0.0073   -0.0305
   -0.0064   -0.0308
   -0.0054   -0.0309
   -0.0044   -0.0311
   -0.0034   -0.0312
   -0.0025   -0.0313
   -0.0015   -0.0314
   -0.0005   -0.0314
    0.0005   -0.0314
    0.0015   -0.0314
    0.0025   -0.0313
    0.0034   -0.0312
    0.0044   -0.0311
    0.0054   -0.0309
    0.0064   -0.0308
    0.0073   -0.0305
    0.0083   -0.0303
    0.0092   -0.0300
    0.0102   -0.0297
    0.0111   -0.0294
    0.0120   -0.0290
    0.0129   -0.0286
    0.0138   -0.0282
    0.0147   -0.0278
    0.0156   -0.0273
    0.0164   -0.0268
    0.0172   -0.0263
    0.0181   -0.0257
    0.0189   -0.0251
    0.0196   -0.0245
    0.0204   -0.0239
    0.0211   -0.0232
    0.0219   -0.0226
    0.0226   -0.0219
    0.0232   -0.0211
    0.0239   -0.0204
    0.0245   -0.0196
    0.0251   -0.0189
    0.0257   -0.0181
    0.0263   -0.0172
    0.0268   -0.0164
    0.0273   -0.0156
    0.0278   -0.0147
    0.0282   -0.0138
    0.0286   -0.0129
    0.0290   -0.0120
    0.0294   -0.0111
    0.0297   -0.0102
    0.0300   -0.0092
    0.0303   -0.0083
    0.0305   -0.0073
    0.0308   -0.0064
    0.0309   -0.0054
    0.0311   -0.0044
    0.0312   -0.0034
    0.0313   -0.0025
    0.0314   -0.0015
    0.0314   -0.0005
    0.0314    0.0005
    0.0314    0.0015
    0.0313    0.0025
    0.0312    0.0034
    0.0311    0.0044
    0.0309    0.0054
    0.0308    0.0064
    0.0305    0.0073
    0.0303    0.0083
    0.0300    0.0092
    0.0297    0.0102
    0.0294    0.0111
    0.0290    0.0120
    0.0286    0.0129
    0.0282    0.0138
    0.0278    0.0147
    0.0273    0.0156
    0.0268    0.0164
    0.0263    0.0172
    0.0257    0.0181
    0.0251    0.0189
    0.0245    0.0196
    0.0239    0.0204
    0.0232    0.0211
    0.0226    0.0219
    0.0219    0.0226
    0.0211    0.0232
    0.0204    0.0239
    0.0196    0.0245
    0.0189    0.0251
    0.0181    0.0257
    0.0172    0.0263
    0.0164    0.0268
    0.0156    0.0273
    0.0147    0.0278
    0.0138    0.0282
    0.0129    0.0286
    0.0120    0.0290
    0.0111    0.0294
    0.0102    0.0297
    0.0092    0.0300
    0.0083    0.0303
    0.0073    0.0305
    0.0064    0.0308
    0.0054    0.0309
    0.0044    0.0311
    0.0034    0.0312
    0.0025    0.0313
    0.0015    0.0314
    0.0005    0.0314
         0    0.0000
3
Pass
 
%% Test case 4 : Half-circle
t=[0:pi/100:pi,0]';
xy=[cos(t),sin(t)];
err=(abs(perimeter1(xy)-(pi+2))/(2+pi))*100;
assert(err<.1)
vecs =
   -0.0005    0.0314
   -0.0015    0.0314
   -0.0025    0.0313
   -0.0034    0.0312
   -0.0044    0.0311
   -0.0054    0.0309
   -0.0064    0.0308
   -0.0073    0.0305
   -0.0083    0.0303
   -0.0092    0.0300
   -0.0102    0.0297
   -0.0111    0.0294
   -0.0120    0.0290
   -0.0129    0.0286
   -0.0138    0.0282
   -0.0147    0.0278
   -0.0156    0.0273
   -0.0164    0.0268
   -0.0172    0.0263
   -0.0181    0.0257
   -0.0189    0.0251
   -0.0196    0.0245
   -0.0204    0.0239
   -0.0211    0.0232
   -0.0219    0.0226
   -0.0226    0.0219
   -0.0232    0.0211
   -0.0239    0.0204
   -0.0245    0.0196
   -0.0251    0.0189
   -0.0257    0.0181
   -0.0263    0.0172
   -0.0268    0.0164
   -0.0273    0.0156
   -0.0278    0.0147
   -0.0282    0.0138
   -0.0286    0.0129
   -0.0290    0.0120
   -0.0294    0.0111
   -0.0297    0.0102
   -0.0300    0.0092
   -0.0303    0.0083
   -0.0305    0.0073
   -0.0308    0.0064
   -0.0309    0.0054
   -0.0311    0.0044
   -0.0312    0.0034
   -0.0313    0.0025
   -0.0314    0.0015
   -0.0314    0.0005
   -0.0314   -0.0005
   -0.0314   -0.0015
   -0.0313   -0.0025
   -0.0312   -0.0034
   -0.0311   -0.0044
   -0.0309   -0.0054
   -0.0308   -0.0064
   -0.0305   -0.0073
   -0.0303   -0.0083
   -0.0300   -0.0092
   -0.0297   -0.0102
   -0.0294   -0.0111
   -0.0290   -0.0120
   -0.0286   -0.0129
   -0.0282   -0.0138
   -0.0278   -0.0147
   -0.0273   -0.0156
   -0.0268   -0.0164
   -0.0263   -0.0172
   -0.0257   -0.0181
   -0.0251   -0.0189
   -0.0245   -0.0196
   -0.0239   -0.0204
   -0.0232   -0.0211
   -0.0226   -0.0219
   -0.0219   -0.0226
   -0.0211   -0.0232
   -0.0204   -0.0239
   -0.0196   -0.0245
   -0.0189   -0.0251
   -0.0181   -0.0257
   -0.0172   -0.0263
   -0.0164   -0.0268
   -0.0156   -0.0273
   -0.0147   -0.0278
   -0.0138   -0.0282
   -0.0129   -0.0286
   -0.0120   -0.0290
   -0.0111   -0.0294
   -0.0102   -0.0297
   -0.0092   -0.0300
   -0.0083   -0.0303
   -0.0073   -0.0305
   -0.0064   -0.0308
   -0.0054   -0.0309
   -0.0044   -0.0311
   -0.0034   -0.0312
   -0.0025   -0.0313
   -0.0015   -0.0314
   -0.0005   -0.0314
    2.0000   -0.0000
4
Pass
 
%% Test case 4 : Hexagon
side=rand;
x=side*[-1 -0.5 0.5 1 0.5 -0.5 -1];
y=side*sqrt(3)*[0 -0.5 -0.5 0 0.5 0.5 0];
xy=[x',y'];
err=(abs(perimeter1(xy)-(6*side))/(6*side))*100;
assert(err<.1)
vecs =
    0.2922   -0.5061
    0.5844         0
    0.2922    0.5061
   -0.2922    0.5061
   -0.5844         0
   -0.2922   -0.5061