Comparison Using a Tolerance
조회 수: 87 (최근 30일)
이전 댓글 표시
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.
답변 (2개)
Anton Kogios
2023년 11월 14일
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
댓글 수: 0
Cris LaPierre
2023년 12월 12일
편집: Cris LaPierre
2023년 12월 12일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Testing Frameworks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!