How can I assess Symbolic Units and use assessVariableEqual to admit tolerances in MATLAB Grader simultaneously ?

I created a MATLAB Grader problem where the students are supposed to check some literature tables to get some information about the material properties in order to answer it. The answer might vary a little depending of the source of their tables so I used coded tests to modify the tolerance.
assessVariableEqual('myVariable', referenceVariables.myVariable,'RelativeTolerance',0.03);
It worked fine. But now I am intending for the students to do the same problem including physical units. I am trying to assess that as part of the tests and I'm having dificulties with the assessVariableEqual. Whenever I include the sym units as part of the problem, the test tolerances seem to dissapear; it will grade correct only if the value is exaclty the one on the reference solution.
I tried separateUnits and then assess for the value and for the units separately, but now it shows that the variable I compare should be a type char and not sym.It is mentioned in the documentation that the learner variable is type char, but the correct (reference variable) is any data type. So I try to convert the learner variable into char and then assess, but now it says the value is not correct.

Answers (1)

I believe this answer is related:
Your observation is correct - when your variable is symbolic, they must be exactly equal. It is not possible to set a symbolic tolerance.
As an aside, you will see similar behavior with numberic variables if your variable and tolerance value are not of the same data type (e.g. variable is uint8 and tolerance is double). When the variable and tolerance data types are different, the tolderance is ignored.
If you could share a specific example, we could see if a workaround is possible.

5 Comments

I looked a little bit more into it. Here is a possible workaround. For this example, I'm assuming the task is to define the acceleration due to gravity with units.
Let's say the reference solution defines it in
u = symunit;
g = 9.81*u.m/u.s^2
Let's say the learner defines it using
u = symunit;
g = 32.2*u.ft/u.s^2
If I wanted to accept either (with tolerance on the value), my MATLAB Code assessment might be
% Ensure learner's solution is in the same units as reference solution
u = symunit;
f = unitConvert(g,u.m/u.s^2)
% separte units
[Data,Units] = separateUnits(f)
[refData,refUnits] = separateUnits(referenceVariables.g)
% convert symbolic number to double so tolerances can be used
g = double(Data)
% compare
assessVariableEqual('g',double(refData),'RelativeTolerance',0.03)
assessVariableEqual('Units',refUnits)
Note that the test for units may not be necessary, as if the conversion works, it will be in the expected units. If not, the conversion will generate an error, and the assessment test will fail.
I am intentionally overwriting the learner's variable g. This is because, if the value is incorrect, the error message for assessVariableEqual will include the name of the variable in quotes. I want this to be a variable name the learner recognizes.
This idea works. I used that code with my own problem and it does the job for assessing the values g including tolerances.
Though, I am still strugling with the assessVariableEqual() for the units. I'm letting the students work by themselves on the unit convertion as part of the problem. So, I omitted this part.
% Ensure learner's solution is in the same units as reference solution
f = unitConvert(g,u.m/u.s^2)
Then I tried to assess for the Units, but unfortunately it doesnt worked for me.
UPDATE:
I did acomplish the assessment for the Units using a conditional and assigning a known number to compare it (converting it to boolean).
% Define variable Units as a boolean (or double): 1 if it matches, 0 if it doesnt
if RefUnits == Units
Units = 1;
else
Units = 0;
end
% Use assessVariableEqual to compare the Variable and its known values: 1 if it matches, 0 if it doesnt
assessVariableEqual('Units',1,'Feedback','Check your units');
What are the units you are trying to assess? Could you share what the reference and learner values should be?
The value should be the temperature of a fluid after a thermal process, the Units are Celsius.
I was checking and it seems that if the learner uses Fahrenheit it does prompt the error; the test works fine. But if the learner uses Kelvin it goes through, as if it was on Celsius; the test "doesnt work".
So I believe the point is that a single unit of u.Kelvin and u.Celsius represent the same amount of temperature. And its right, if you add 2 °C to something its the same as if you add 2 K. The actual difference is the shift of the Celsius scale.
Therefore the main issue here is about how to handle this kind of problem, where you are asking the student for a temperature (e.g Triple point of water : 273.16 K) and the test wont differentiate that from 273.16 C
Ok, I see now. The issue is that Units is still symbolic. I would suggest either using an assert statement with isequal to check the units, or to first convert the units to string using symunit2str.
Assuming the reference solution is defined as follows
u = symunit;
% Triple point of water
T = 273.16*u.Kelvin
and the learner solution is
u = symunit;
% Triple point of water
T = 273.16*u.Celsius
Option 1: assert. I've included a custom error message that tells the user if the solution fails due to incorrect units.
% separte units
[Data,Units] = separateUnits(T)
[refData,refUnits] = separateUnits(referenceVariables.T)
assert(isequal(Units,refUnits),'Your temperature units are incorrect')
Optino 2: assessVariableEqual. Note that, if incorrect, the error message will include the variable name 'Units'. This is not a variable the learner created, so may actually confuse the learner.
% separte units
[Data,Units] = separateUnits(T)
[refData,refUnits] = separateUnits(referenceVariables.T)
Units = symunit2str(Units)
% compare
assessVariableEqual('Units',symunit2str(refUnits))

Sign in to comment.

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!