isapprox - Determine approximate equality - MATLAB (original) (raw)
Determine approximate equality
Since R2024b
Syntax
Description
`TF` = isapprox([A](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24),[B](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24))
determines which corresponding elements in the two input arrays are approximately equal and returns a logical array. TF
contains 1
(true
) where the corresponding elements are within the region of approximate equality, and 0
(false
) otherwise. The default relative and absolute tolerances are both 1e-15
, or ifA
or B
is of type single
,5e-7
.
`TF` = isapprox([A](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24),[B](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24),[tol](#mw%5F814e52ea-1b8c-497b-ad1f-05837f036edd))
determines approximate equality using the relative and absolute tolerances set by the tolerance level tol
. Specify tol
as"verytight"
, "tight"
, "loose"
, or"veryloose"
.
`TF` = isapprox([A](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24),[B](#mw%5Fffdebbe3-dffd-4707-921f-4e2cc80c6a24),[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments. For example,isapprox(A,B,RelativeTolerance=1e-10)
determines approximate equality using the specified relative tolerance and an absolute tolerance of0
.
Examples
Compare Two Values
Create two numeric scalar values and compare them for equality. The ==
operator returns logical 0
(false
) because the values differ by a very small amount and are not exactly equal.
A = sin(3/4*pi); B = 1/sqrt(2); A == B
To account for round-off error, compare the values for approximate equality. The isapprox
function returns logical 1
(true
) because the difference between the values of A
and B
is within the default tolerance.
Accept More Noise
Compare the value of π in double precision, which has 15 digits after the decimal point, and elements in a numeric vector for approximate equality.
B = [2*asin(1) integral(@(x) 1./sqrt(1-x.^2),-1,1) round(pi,10) 22/7]
B = 1×4
3.141592653589793 3.141592653589721 3.141592653600000 3.142857142857143
TF = 1x4 logical array
1 0 0 0
The isapprox
function returns logical 1
(true
) for only the first element. The difference between all other elements in B
and the value of A
is greater than the default tolerance. To accept more noise in the input data, specify the relative and absolute tolerance level as "tight"
.
TF2 = isapprox(A,B,"tight")
TF2 = 1x4 logical array
1 1 0 0
The isapprox
function returns logical 1
(true
) for only the first and second elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "loose"
.
TF3 = isapprox(A,B,"loose")
TF3 = 1x4 logical array
1 1 1 0
The isapprox
function returns logical 1
(true
) for the first three elements. To accept even more noise in the input data, specify the relative and absolute tolerance level as "veryloose"
.
TF4 = isapprox(A,B,"veryloose")
TF4 = 1x4 logical array
1 1 1 1
Specify Absolute Tolerance
Compare two sets of measurements from two different instruments for approximate equality. Specify the absolute tolerance as 0.02
, which is the known precision of the instruments. If you specify just the absolute tolerance, the relative tolerance is 0
.
A = [1.00 2.00 3.10 NaN]; B = [0.99 2.01 3.15 NaN]; TF = isapprox(A,B,AbsoluteTolerance=0.02)
TF = 1x4 logical array
1 1 0 0
Input Arguments
A
, B
— Input data
arrays
Input data, specified as arrays. A
and B
must either be the same size or have sizes that are compatible. If the input arrays are complex, then they must be of typesingle
or double
.
Specifying isapprox(A,B)
is the same as specifyingisapprox(B,A)
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Complex Number Support: Yes
tol
— Relative and absolute tolerance level
"verytight"
(default) | "tight"
| "loose"
| "veryloose"
Relative and absolute tolerance level, specified as one of the values in this table. For more information, see Region of Approximate Equality for Tolerances.
If you specify tol
, you cannot specify theRelativeTolerance
or AbsoluteTolerance
name-value arguments.
Tolerance Level | Tolerance When Both A and B Are of Any Type Other than single | Tolerance When Either A orB Is of Type single |
---|---|---|
"verytight" | 1e-15 | 5e-7 |
"tight" | 1e-12 | 1e-6 |
"loose" | 1e-8 | 1e-4 |
"veryloose" | 1e-3 | 1e-2 |
Data Types: string
| char
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Example: TF = isapprox(A,B,RelativeTolerance=1e-10)
RelativeTolerance
— Relative tolerance
scalar in range [0, 1)
Relative tolerance, specified as a scalar in the range [0, 1)
.
The relative tolerance defines the maximum allowed difference between approximately equal elements as a fraction of the element with greater magnitude.
- If you do not specify the
RelativeTolerance
orAbsoluteTolerance
name-value arguments, the relative tolerance is1e-15
, or ifA
orB
are of typesingle
,5e-7
. - If you specify the
RelativeTolerance
name-value argument and do not specify theAbsoluteTolerance
name-value argument, the absolute tolerance is0
.
Example: isapprox(A,B,RelativeTolerance=1e-10)
uses a relative tolerance of 1e-10
and an absolute tolerance of0
.
Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8)
uses a relative tolerance of 1e-10
and an absolute tolerance of1e-8
.
Data Types: double
| single
AbsoluteTolerance
— Absolute tolerance
nonnegative scalar
Absolute tolerance, specified as a nonnegative scalar.
The absolute tolerance defines the maximum allowed difference between approximately equal elements as a fixed number.
- If you do not specify the
RelativeTolerance
orAbsoluteTolerance
name-value arguments, the absolute tolerance is1e-15
, or ifA
orB
are of typesingle
,5e-7
. - If you specify the
AbsoluteTolerance
name-value argument and do not specify theRelativeTolerance
name-value argument, the relative tolerance is0
.
Example: isapprox(A,B,AbsoluteTolerance=1e-8)
uses an absolute tolerance of 1e-8
and a relative tolerance of0
.
Example: isapprox(A,B,RelativeTolerance=1e-10,AbsoluteTolerance=1e-8)
uses a relative tolerance of 1e-10
and an absolute tolerance of1e-8
.
Data Types: double
| single
Algorithms
Region of Approximate Equality
Tolerance | Algorithm | Region of Approximate Equality |
---|---|---|
Relative tolerance | |A−B | ≤relTol×max( |
Absolute tolerance | |A−B | ≤absTol, where A and B are corresponding elements in the input arrays and absTol is the absolute tolerance. |
Relative and absolute tolerances | |A−B | ≤max(absTol,relTol×max( |
Version History
Introduced in R2024b