logical
Check validity of equation or inequality
Syntax
Description
Examples
Test Condition Using logical
Use logical
to check if 3/5
is
less than 2/3
:
logical(sym(3)/5 < sym(2)/3)
ans = logical 1
Test Equation Using logical
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 = 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
:
assume(x >= 0) logical(x == sqrt(x^2))
ans = logical 1
Note that logical
typically ignores assumptions
on variables.
syms x assume(x == 5) logical(x == 5)
ans = logical 0
To compare expressions taking into account assumptions on their
variables, use isAlways
:
isAlways(x == 5)
ans = logical 1
For further computations, clear the assumption on x
by recreating it
using syms
:
syms x
Test Multiple Conditions Using logical
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 = logical 1
Test Inequality Using logical
Check this inequality. Note that logical
evaluates
the left side of the inequality.
logical(sym(11)/4 - sym(1)/2 > 2)
ans = logical 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 = logical 1
Compare logical
and isAlways
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 = logical 0
logical
also does not realize that this inequality
is invalid:
logical(sin(x)/cos(x) ~= tan(x))
ans = logical 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 = logical 1
isAlways(sin(x)/cos(x) ~= tan(x))
Warning: Unable to prove 'sin(x)/cos(x) ~= tan(x)'. ans = logical 0
Input Arguments
Tips
For symbolic equations,
logical
returns logical1
(true
) only if the left and right sides are identical. Otherwise, it returns logical0
(false
).For symbolic inequalities constructed with
~=
,logical
returns logical0
(false
) only if the left and right sides are identical. Otherwise, it returns logical1
(true
).For all other inequalities (constructed with
<
,<=
,>
, or>=
),logical
returns logical1
if it can prove that the inequality is valid and logical0
if it can prove that the inequality is invalid. Iflogical
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, useisAlways
.logical
typically ignores assumptions on variables.