Comparison Using a Tolerance
Show older comments
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,
∣x−y∣<ϵ
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 Comments
Steven Lord
on 14 Nov 2023
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Yong Teng
on 14 Nov 2023
Pratyush
on 14 Nov 2023
Hi Yong,
I understand that you want a function that tells wether a value is in range [actual value - tolerance, actual value - tolerance]. Here is the MATLAB function for it:
function result = isequal_tol(x, y, tol)
% Check if the absolute difference between x and y is less than tol
if abs(x - y) < tol
result = true;
else
result = false;
end
end
Benson Moyo
on 9 Dec 2023
I also have a challenge with this task. I have all the logic and the code runs and give correct results but I am not sure of what is expected on this task.
Vipuj
on 12 Dec 2023
i am facing a situation where i am not able to figure about what is wrong in btw the code, instead of my code i also used above given code to pass assessment in matlab fundamental ,but answers are coming incorrect.
PLEASE ANYONE HELP ME OUT.
Dyuman Joshi
on 12 Dec 2023
@Vipuj, that looks like a bug. So does the problem faced by OP.
@Steven Lord @Cris LaPierre, I request you to please forward this to the concerned department/person.
Voss
on 1 Jan 2024
@Hemapriya: See if this answer works for you:
Answers (2)
Test cases
isequal_tol(1,5,1e-3) % false
isequal_tol(1,1,1e-3) % true
isequal_tol(pi,3.141593,1e-3) % true
isequal_tol function:
function z = isequal_tol(x,y,tol)
if abs(x-y) < tol
z = true;
else
z = false;
end
end
Cris LaPierre
on 12 Dec 2023
Edited: Cris LaPierre
on 12 Dec 2023
0 votes
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).
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!