MATLAB Grader -- assessing functions

Reference Solution:
function results = drinking_age(x)
if x < 0
results = 'Invalid age'
end
if x >= 21
results = 'You are of drinking age'
else
results = 'You are not of drinking age'
end
end
Assessment:
% Run learner solution.
x = 1;
results = drinking_age(x);
% Run reference solution.
yReference = reference.drinking_age(x);
% Compare.
assessVariableEqual('results', yReference);
The assessment claims all is good : x=25 drinking_age(x)
When I change the >= to ==, the assessment says all is good
function results = drinking_age(x)
if x < 0
results = 'Invalid age'
end
if x == 21
results = 'You are of drinking age'
else
results = 'You are not of drinking age'
end
end

 Accepted Answer

Cris LaPierre
Cris LaPierre on 31 May 2023
Perhaps you could explain what it is you think should be happening?
The value of x used to assess the learner solution is defined in your assessment test:
% Run learner solution.
x = 1; %<---------- Defined here
results = drinking_age(x);
% Run reference solution.
yReference = reference.drinking_age(x);
I assume the code 'x=25' has been added to the 'Code to call your function' field. Since functions must be called in order to run, this field just gives learners a way to test their function before submitting. It has nothing to do with assessing the function.
So, in your case, your assessment test is always checking the functions using x=1. Since 1 is much less than 21, it doesn't matter if you use >=21 or ==21. The else case is still the one that executes.

4 Comments

I'll just make a side comment here that since you don't return after
if x < 0
results = 'Invalid age'
end
the code will keep going and also return a result of
results = 'You are not of drinking age'
You may instead want
if x < 0
results = 'Invalid age'
return
end
your assessment test is always checking the functions using x=1
Thank you, I didn't realize that and thought it was just a "starting point" to define the variable.
I thought it was sending/evaulating the x in the 'code to call your function'
This could explain some other issues I was having. I will go back and update other assignments - thanks for the quick response!
Cris -
Is there a way to assess more than 1 x?
I'd like to test -10 and 30, for example
There are 2 possible ways. First is to have the function written in such a way as to accept a vector as input, and return a corresponding output. The second is to test each input separately.
For the function you have shared, I think the easiest is to either create a separate assessment test for each case, or have a single assessment test all three:
% Test 1
x = 1;
results = drinking_age(x);
yReference = reference.drinking_age(x);
% Compare.
assessVariableEqual('results', yReference);
% Test 2
x = 10;
results = drinking_age(x);
yReference = reference.drinking_age(x);
% Compare.
assessVariableEqual('results', yReference);
% Test 3
x = 30;
results = drinking_age(x);
yReference = reference.drinking_age(x);
% Compare.
assessVariableEqual('results', yReference);
Ultimately, you are designing a learning experience for your students. What will work best for them? If you split this into 3 separate assessment tests, then you can give partial credit if they get some scenarios working but not others. You can also give more specific feedback related to each individual scenario.
If you put everything into a single test, it may look less intimidating to your learners, but can be harder to help them identify their mistake if they are incorrect.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Asked:

on 31 May 2023

Edited:

on 31 May 2023

Community Treasure Hunt

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

Start Hunting!