Documentation Center

  • Trial Software
  • Product Updates

logical

Check validity of equation or inequality

Syntax

logical(cond)

Description

logical(cond) checks whether the condition cond is valid.

Input Arguments

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.

Examples

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))
ans =
     0

More About

expand all

Tips

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

  • For symbolic inequalities constructed with ~=, logical returns logical 0 (false) only if the left and right sides are identical. 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 valid and logical 0 if it can prove that the inequality is invalid. If logical cannot determine whether such inequality is valid or not, it throws an error.

  • logical evaluates expressions on both sides of an equation or inequality, but does not simplify or mathematically transform them. To compare two expressions applying mathematical transformations and simplifications, use isAlways.

  • logical typically ignores assumptions on variables.

See Also

| | | | | | | | |

Was this topic helpful?