Comparison Using a Tolerance

87 vues (au cours des 30 derniers jours)
Yong Teng
Yong Teng le 14 Nov 2023
Commenté : Voss le 1 Jan 2024
Hi, can any one help on this question?
The built-in operations x == y and isequal(x,y) will both test whether two numbers x and y have exactly the same value. However, because computers use finite-precision arithmetic, you may often want to test whether two numbers are close, rather than exactly equal. That is,
xy<ϵ
for some small tolerance
ϵ
.
Recall that a function declaration line starts with the keyword function, followed by the same syntax as you would use to call the function.
[out1,out2,...] = fun_name(in1,in2,...)
TASK
Write a function named isequal_tol which takes three scalar inputs, x, y, and a tolerance tol.
It should return a single output with the value true if x and y differ by less than tol and false otherwise.
  8 commentaires
Hemapriya
Hemapriya le 1 Jan 2024
@Vipuj same error is coming . . . can you please help me to solve. . . if you solveduhave
Voss
Voss le 1 Jan 2024

Connectez-vous pour commenter.

Réponses (2)

Anton Kogios
Anton Kogios le 14 Nov 2023
Test cases
isequal_tol(1,5,1e-3) % false
ans = logical
0
isequal_tol(1,1,1e-3) % true
ans = logical
1
isequal_tol(pi,3.141593,1e-3) % true
ans = logical
1
isequal_tol function:
function z = isequal_tol(x,y,tol)
if abs(x-y) < tol
z = true;
else
z = false;
end
end

Cris LaPierre
Cris LaPierre le 12 Déc 2023
Modifié(e) : Cris LaPierre le 12 Déc 2023
The issue in both cases is that there are 2 "isequal_tol.mlx" files open. Please close them both, then either click Reset or navigate away, and then back to this problem (the whole page, not just the task).
  2 commentaires
Benson Moyo
Benson Moyo le 13 Déc 2023
Thank you very much my question was well answered.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Testing Frameworks dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by