Please show me the appropriate script I must create to properly execute the func
ID: 3552477 • Letter: P
Question
Please show me the appropriate script I must create to properly execute the function! Cheers!
Explanation / Answer
In a file in your working folder, create a function in the file sierpinski.m. This function returns a matrix representing an image of a Sierpinski carpet fractal. It takes as input the fractal level and an optional data type.
function carpet = sierpinski(nLevels,classname)
if nargin == 1
classname = 'single';
end
mSize = 3^nLevels;
carpet = ones(mSize,classname);
cutCarpet(1,1,mSize,nLevels); % begin recursion
function cutCarpet(x,y,s,cL)
if cL
ss = s/3; % define subsize
for lx = 0:2
for ly = 0:2
if lx == 1 && ly == 1
% remove center square
carpet(x+ss:x+2*ss-1,y+ss:y+2*ss-1) = 0;
else
% recurse
cutCarpet(x + lx*ss, y + ly*ss, ss, cL-1);
end
end
end
end
end
end
Create TestCarpet Test Class
In a file in your working folder, create a new class, TestCarpet, to test the sierpinski function.
classdef TestCarpet < matlab.unittest.TestCase
Define properties Block
Define the properties used for parameterized testing. In the TestCarpet class, define these properties in a property block with the TestParameter attribute.
properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small', 2,'medium', 4, 'large', 6);
side = struct('small', 9, 'medium', 81,'large', 729);
end
The type property contains the different data types you want to test. The level property contains the different fractal level you want to test. The side property contains the number of rows and columns in the Sierpinski carpet matrix and corresponds to the level property. To provide meaningful names for each parameterization value, level and side are defined as structs.
Define Test methods Block
Define the following test methods in the TestCarpet class.
methods (Test)
function testRemainPixels(testCase, level)
% expected number pixels equal to 1
expPixelCount = 8^level;
% actual number pixels equal to 1
actPixels = find(sierpinski(level));
testCase.verifyNumElements(actPixels,expPixelCount);
end
function testClass(testCase, type, level)
testCase.verifyClass(...
sierpinski(level,type), type);
end
function testDefaultL1Output(testCase)
exp = single([1 1 1; 1 0 1; 1 1 1]);
testCase.verifyEqual(sierpinski(1), exp);
end
end
The testRemainPixes method tests the output of the sierpinski function by verifying that the number of nonzero pixels is the same as expected for a particular level. This method uses the level property and, therefore, results in three test elements
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.