Azzera filtri
Azzera filtri

Why does checking for equality sometimes fail when using the Symbolic Toolbox?

7 visualizzazioni (ultimi 30 giorni)
Why does checking for equality with 'isequal' sometimes fail when using the Symbolic Toolbox? For example:
>> syms x y >> isequal(x*y+x, x*(y+1)) ans = logical 0

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 13 Set 2024 alle 0:00
Modificato: MathWorks Support Team il 13 Set 2024 alle 17:40
In general, symbolic expressions can have two notions of equality.
1) Mathematical Equality: Two expressions are mathematically equivalent if they produce the same result for every possible value of the symbolic variables that they are built from. Mathematical equality takes into account assumptions on variables and mathematical transformations. For instance, the expressions 'x' and 'sqrt(x^2)' are not mathematically equivalent in general. However, if x is assumed to be nonnegative with
>> assume(x >= 0)
Then the above expressions are indeed mathematically equivalent.
To test for mathematical equivalence between expressions, you should use the 'isAlways' function described in the following link:
The 'isAlways' function operates on conditions and tests that the condition is always true for every possible value of the input variables. Thus, to test if two expressions, 'expr1' and 'expr2' are mathematically equal, you can use the following command
>> isAlways(expr1 == expr2)
The 'logical' function is similar to the 'isAlways' function except that it does not always take variable assumptions or mathematical transformations into account. For more information about the 'logical' function, see the following link:
2) Structural Equality: Two expressions are structurally equivalent if they are recursively identical. When testing for structural equality we treat the symbolic expressions as simple MATLAB objects without any mathematical meaning. We say that two MATLAB symbolic objects are structurally equal if they have the same attributes and identical child expressions.
The 'isequal' and 'isequaln' functions test for structural equality instead of mathematical equality. Thus, the following commands return false even though the expressions are mathematically equivalent:
>> syms x y>> isequal(x*y+x, x*(y+1))
For more information about the 'isequal' function, see the following link:

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by