Loïc Quéval
Loïc Quéval

Reputation: 43

Matlab symbolic

I am trying to compare two simple expressions using Matlab symbolic toolbox. For some reason, the code returns 0. Any idea ?

syms a b c
A = (a/b)^c
B = a^c/b^c
isequal(A,B)

Upvotes: 4

Views: 166

Answers (2)

DecimalTurn
DecimalTurn

Reputation: 4127

It seems like MATLAB has a hard time telling that two expressions are the same when (potentially) fractional exponents are involved.

So, one solution, as suggested by Mikhail is to restrict the values of c to be only integers although, as discussed in the Math.SE question jodag posted, there is nothing wrong with fractional exponents in this case.

Hence, since this restriction to integers is not necessary for the statement to be true, another solution is to use simplify function on the expression for B but allowing it to run more simplification steps in order to get the most simplified expression.

syms a b c
A = (a/b)^c
B = a^c/b^c
isequal(A,simplify(B,'step',4))

Four steps is actually the smallest number that worked for me, but that could vary across versions of MATLAB I'm assuming. To be sure, I would include more, but for really large expressions, this could become computationally intensive, so some judgment is necessary. Note that, you could also use the 'Seconds' option to limit the amount of time allowed for simplification.

Upvotes: 2

Mikhail
Mikhail

Reputation: 8028

In general what you wrote isn't true, under the right "assumptions" it becomes true: for example, assuming c is an integer you can trick MATLAB into expanding A

clc; clear all;
syms a 
syms b 
syms c integer
A = (a/b)^c;
B = simplify((a^c)/(b^c));
disp(isequal(A,B));
disp(A);
disp(B);

1

a^c/b^c
a^c/b^c

Upvotes: 0

Related Questions