symbolic equivalence very picky, doesn't recognize equivalent expressions??

2 views (last 30 days)
I manually computed the derivatives of a function and am trying to check their correctness with the symbolic differentiation function in matlab. When using isequaln(a,b), where "a" is the derivative I computed and "b" is matlabs computation, matlab returns 0 and 1 for equivalent values of "a" that are simply ordered or written differently.
For example isequaln(2*(c-x)*(c-x),2*(c-x)^2), with syms c x, will return 0 when it should return 1. This isn't the derivative I am computing, just an example of matlab not recognizing equivalent expressions. So even with correct derivatives matlab still may not recognize them as so, which renders it useless for checking my derivatives. Is this normally how isequaln(A,B) works? Or might my symbolic toolbox be buggy?
Any help would be greatly appreciated!
Thanks,
Thomas

Answers (1)

Walter Roberson
Walter Roberson on 3 Nov 2015
Usually for symbolic expressions, instead of isequaln(A,B) you should loop through testing the elements with isAlways
compare_same = true;
if ~isequal(size(A),size(B)) || ~(isnumeric(A) || issymbolic(A)) || ~(isnumeric(B) || issymbolic(B))
compare_same = false;
else
for K = 1 : numel(A)
nA = isnan(A(K));
if nA ~= isnan(B(K));
compare_same = false; %one nan but the other is not
break;
end
if ~nA && ~isAlways(A(K)==B(K), 'Unknown', false)
compare_same = false; %we cannot prove them to be equal
break;
end
end
end
This deliberately returns false if we cannot prove two of the elements to be the same. If the expressions were identical except for some small rewriting then it would figure that out. If the expressions are complex rewriting of each other it might not be able to find a match. But the most common reason for not being able to prove they are the same or that they are different is if there are differences in the expressions or the variables such that the expressions might be the same or different depending upon the values of the unknown variables.
There really is no good answer for the "right" thing to do if there are two expressions that might be the same or might not: the special case might just happen to be true.

Tags

Community Treasure Hunt

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

Start Hunting!