Documentation

Write Assessments for Function-Based Learner Solutions

For function-based solutions, you can write MATLAB® code using the built-in functions that check for variable equality and keyword or function presence:

  • assessVariableEqual — Check whether a variable in the learner solution equals a specified value within tolerance.

  • assessFunctionPresence — Check for the presence of specific functions or keywords in the learner solution.

  • assessFunctionAbsence — Check that certain functions or keywords are not present in the learner solution.

Execution Model

  • Each assessment you write for a function-based solution typically includes a call to the learner solution. You can provide inputs to the function and evaluate any returned values. You can also call the reference solution to compare its output with the learner solution output.

  • Each assessment runs sequentially and independently of the other assessments. If one assessment fails, the subsequent assessments still run.

  • Variables created in one assessment are not available in the next one. Define all the required variables in each assessment.

  • If code terminates without errors, the assessment result shows a passed status. Otherwise, the assessment results show a failed status.

    If the test is a pretest, the learner can view information about the assessment test by clicking the arrow to the left of the test name, regardless of whether the test passed or failed.

  • When you use assessVariableEqual with function-based problems, use the same name for any output variable created when calling the learner function as you would use in the learner function declaration. The default feedback messages reference the output variable name created in the assessment, and the learner may not recognize the output variable if it does not match the declaration.

Call Learner Solution in Assessments

To evaluate a function that a learner submits as a solution, each assessment calls the learner solution.

An example assessment for a triangleArea function could be:

b = 5;
h = 3;
area = triangleArea(b,h);
areaCorrect = 7.5;
assessVariableEqual('area',areaCorrect)

When the learner clicks Submit, this assessment calls the learner version of triangleArea.

Use Reference Solution to Assess Learner Solution

To compare the learner solution to the reference solution, call both functions.

  • To call the learner solution, use the syntax myFunction.

  • To call the reference solution, use the syntax reference.myFunction.

In both cases, replace myFunction with the name of the function you are using in the solution template.

An example assessment for a triangleArea function could be:

b = 5;
h = 3;
area = triangleArea(b,h);
areaCorrect = reference.triangleArea(b,h);
assessVariableEqual('area',areaCorrect)

Examples

Flip a Vector

The learner must write a function called flipIt that flips an input vector, reversing the order of the values in the vector. The learner should not use the flip function.

Reference Solution

function w = flipIt(v)
    w = v(end:-1:1);
end

Assessment Tests.  Call the learner solution with input vector [1 2 3 4]. Test that the learner solution matches the reference solution and avoids using flip:

v = [1 2 3 4];
w = flipIt(v)
w_correct = reference.flipIt(v);
assessVariableEqual('w',w_correct);
assessFunctionAbsence('flip','FileName','flipIt.m')

Call the learner solution again, this time with input vector [4]:

v = [4];
w = flipIt(v);
assessVariableEqual('w',w_correct);
assessFunctionAbsence('flip','FileName','flipIt.m')

Related Topics