简体   繁体   中英

Why Matlab fails to check the equality of this trigonometric expression

The isequaln() is testing symbolic objects for equality as stated in the documentation. However, this is not the case with the following script.

syms a
f1=cos(a)^2;
f2=1-sin(a)^2;
isequaln(f1,f2)
ans =
  logical
   0

Matlab fails to return the correct answer. Does Matlab check the equality for symbolic expressions merely as comparing strings (ie a typical scenario for regular expressions) or what does exactly Matlab do under the hood?

At the bottom of the documentation page, there is a section called "Tips", which contains the following item:

isequaln(A,B) checks if A and B are the same size and their contents are syntactically the same expression, treating NaN values as equal. To check whether the mathematical comparison A == B holds for all values of variables in A and B , use isAlways(A == B) .

(emphasis mine)

isAlways does what you want:

syms a
f1 = cos(a)^2;
f2 = 1-sin(a)^2;
isAlways(f1 == f2)

This outputs true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM