Simple assertion giving incorrect result?

2 views (last 30 days)
Justin Wyss-Gallifent
Justin Wyss-Gallifent on 11 Jul 2020
Edited: Walter Roberson on 11 Jul 2020
Here's a simple version of something that's arising in some code. The student needs to use vpasolve. The reference solution is this:
syms x
x1 = vpasolve(exp(abs(0.1*x))+2*sin(x)==0,-2)
This reference solution returns a value:
x1 =
-2.4487454187687442582374719820392
When the student puts in a different seed value for vpasolve:
syms x
x1 = vpasolve(exp(abs(0.1*x))+2*sin(x)==0,-3)
The learner solution returns the same value as the reference solution:
x1 =
-2.4487454187687442582374719820392
However doing a simple assertion test of whether these two variables are equal fails. Why would this happen?

Answers (2)

Justin Wyss-Gallifent
Justin Wyss-Gallifent on 11 Jul 2020
I'm going to answer my own question here and leave it for reference. This is because vpasolve returns a symbolic expression which is slightly different even though all digits (shown) are the same. Symbolic variables have no sense of tolerance and so this fails a straight-up variable comparison. This was fixed by changing the test to Matlab code as:
x1 = double(x1)
assessVariableEqual('x1', double(referenceVariables.x1))
  2 Comments
Walter Roberson
Walter Roberson on 11 Jul 2020
assessVariableEqual() is part of Grader. It uses a tolerance, but only for numeric items (symbolic items are not considered numeric for this purpose.)

Sign in to comment.


Walter Roberson
Walter Roberson on 11 Jul 2020
Symbolic floating point values have some guard digits that are not displayed when you output the number
  2 Comments
Walter Roberson
Walter Roberson on 11 Jul 2020
consider that if you have
A = 1
B = A*(1-eps)
then no matter which numeric "format" statement you choose, A and B will display the same (well, other than format hex). They will not compare equal because floating point has no sense of tolerance. assessVariableEqual is not part of MATLAB, it is part of the Grader tool.
Perhaps you are saying that assessVariableEqual has no sense of tolerance for symbolic values, but any tolerance it does have is part the grader. MATLAB itself has no approximately_equal operator, just ismembertol function call.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by