Main Content

logical

Determine if symbolic equation, inequality, or condition is true

Description

tf = logical(cond) checks if the conditions in cond are true and returns an array of logical values. To test conditions that require assumptions or mathematical transformations, use isAlways instead.

example

Examples

collapse all

Test if 3/5 is less than 2/3.

tf = logical(sym(3)/5 < sym(2)/3)
tf = logical
   1

To check if several conditions are true at the same time, combine them by using logical operators. For example, check if 1 is less than 2 and if exp(log(x)) == x. Note that when you define a condition that uses other functions, such as exp and log, these functions are evaluated when defining the condition.

syms x
cond1 = 1 < 2 & exp(log(x)) == x
cond1 = x=x
tf = logical(cond1)
tf = logical
   1

For multiple conditions, you can represent the conditions as a symbolic array.

cond2 = [1 < 2; exp(log(x)) == x]
cond2 = 

(1x=x)

tf = logical(cond2)
tf = 2x1 logical array

   1
   1

Test an inequality that has a symbolic type on the left side and a numeric type on the right side. The expressions on both sides have compatible data types, and the inequality is true.

tf = logical(sym(11)/4 - sym(1)/2 > 2)
tf = logical
   1

logical also checks the validity of equations and inequalities involving other functions, such as int. For example, the left side of this equation that contains an integral evaluates to sym(1). Both sides of the equation are equal, and logical returns 1 (true).

syms x
tf = logical(int(x,x,0,2) - 1 == 1)
tf = logical
   1

logical does not simplify or apply mathematical transformations when checking if a condition is true. For example, check the equality of (x+1)2 and x2+2x+1 using logical.

syms x
tf = logical((x+1)^2 == x^2+2*x+1)
tf = logical
   0

Simplify the condition represented by the symbolic equation using simplify. The simplify function returns the symbolic logical constant symtrue because the equation is always true for all values of x.

cond = simplify((x+1)^2 == x^2+2*x+1)
cond = symtrue

Using logical on symtrue converts the symbolic logical constant to logical 1 (true).

tf = logical(cond)
tf = logical
   1

To check an equation that requires simplifications, you can use isAlways instead.

tf = isAlways((x+1)^2 == x^2+2*x+1)
tf = logical
   1

Check if an equation that involves a function, such as sqrt, is true. Without an additional assumption that x is nonnegative, sqrt(x^2) does not evaluate to x.

syms x
tf = logical(x == sqrt(x^2))
tf = logical
   0

Use assume to set an assumption that x is nonnegative. Now the expression sqrt(x^2) evaluates to x, and logical returns 1 because x == x is true.

assume(x >= 0)
tf = logical(x == sqrt(x^2))
tf = logical
   1

Note that the logical function itself ignores assumptions on symbolic variables.

syms x
assume(x == 5)
tf = logical(x == 5)
tf = logical
   0

To compare expressions taking into account assumptions on their variables, use isAlways.

tf = isAlways(x == 5)
tf = logical
   1

For further computations, clear the assumption on x by recreating it using syms.

syms x

The logical function does not simplify or apply mathematical transformations when checking if a condition is true. For example, logical does not recognize the mathematical equivalence of this equation.

syms x
tf = logical(sin(x)/cos(x) == tan(x))
tf = logical
   0

logical also considers this inequality to be true.

tf = logical(sin(x)/cos(x) ~= tan(x))
tf = logical
   1

To test the validity of equations and inequalities that require simplification or mathematical transformations, use isAlways instead. isAlways issues a warning when returning false for undecidable inputs.

tf = isAlways(sin(x)/cos(x) == tan(x))
tf = logical
   1

tf = isAlways(sin(x)/cos(x) ~= tan(x))
Warning: Unable to prove 'sin(x)/cos(x) ~= tan(x)'.
tf = logical
   0

Input Arguments

collapse all

Input, specified as a symbolic equation, inequality, or a symbolic array of equations or inequalities. You also can combine several conditions by using the logical operators and, or, xor, not, or their shortcuts.

Tips

  • For symbolic equations, logical returns logical 1 (true) only if the left and right sides are equal. Otherwise, it returns logical 0 (false).

  • For symbolic inequalities constructed with ~=, logical returns logical 0 (false) only if the left and right sides are equal. Otherwise, it returns logical 1 (true).

  • For all other inequalities (constructed with <, <=, >, or >=), logical returns logical 1 if it can prove that the inequality is true and logical 0 if it can prove that the inequality is false. If logical cannot determine whether an inequality is true or false, it returns an error.

  • logical does not simplify or mathematically transform a conditional statement. To compare a conditional statement applying mathematical transformations and simplifications, use isAlways.

  • If you use logical to check a conditional statement that involves a symbolic type, then the data types of the compared expressions must be compatible. For example, logical(1==sym(1)) returns 1 (true). If the expressions do not have compatible data types, then logical returns an error. For example, syms f(x) g(y); tf = logical(f~=g) returns an error.

  • logical ignores assumptions on symbolic variables.

Version History

Introduced in R2012a