Documentation |
Check validity of equation or inequality
logical(cond)
cond |
Equation, inequality, or vector or matrix of equations or inequalities. You also can combine several conditions by using the logical operators and, or, xor, not, or their shortcuts. |
Use logical to check whether 1 if less than 2:
logical(1 < 2)
ans = 1
Check if the following two conditions are both valid. To check if several conditions are valid at the same time, combine these conditions by using the logical operator and or its shortcut &.
syms x logical(1 < 2 & x == x)
ans = 1
Check this inequality. Note that logical evaluates the left side of the inequality.
logical(4 - 1 > 2)
ans = 1
logical also evaluates more complicated symbolic expressions on both sides of equations and inequalities. For example, it evaluates the integral on the left side of this equation:
syms x logical(int(x, x, 0, 2) - 1 == 1)
ans = 1
Check the validity of this equation using logical. Without an additional assumption that x is nonnegative, this equation is invalid.
syms x logical(x == sqrt(x^2))
ans = 0
Use assume to set an assumption that x is nonnegative. Now the expression sqrt(x^2) evaluates to x, and logical returns 1:
assume(x >= 0) logical(x == sqrt(x^2))
ans = 1
Note that logical typically ignores assumptions on variables:
syms x assume(x == 5) logical(x == 5)
ans = 0
To compare expressions taking into account assumptions on their variables, use isAlways:
isAlways(x == 5)
ans = 1
For further computations, clear the assumption on x:
syms x clear
Do not use logical to check equations and inequalities that require simplification or mathematical transformations. For such equations and inequalities, logical might return unexpected results. For example, logical does not recognize mathematical equivalence of these expressions:
syms x logical(sin(x)/cos(x) == tan(x))
ans = 0
logical also does not realize that this inequality is invalid:
logical(sin(x)/cos(x) ~= tan(x))
ans = 1
To test the validity of equations and inequalities that require simplification or mathematical transformations, use isAlways:
isAlways(sin(x)/cos(x) == tan(x))
ans = 1
isAlways(sin(x)/cos(x) ~= tan(x))
Warning: Cannot prove 'sin(x)/cos(x) ~= tan(x)'. ans = 0