Unable to Set Equivalence Between Variables

조회 수: 1 (최근 30일)
John
John 2023년 6월 19일
편집: Stephen23 2023년 6월 19일
I am trying to create a script that generates a mesh for a BVP problem that employs FDM. The script works and is producing the mesh grid I want, but ML is not seeing that the elements I explictly want included in the matrix are there. They need to be explcitly included because these values are the boundaries within the problem and so need to be evaluated at those points directly. I have a part of my code that tells me when one of the boundaries are not included in the mesh and in this instance the radial boundary is 0.03. When I go and check the mesh though, I see it in there:
For context, this each column was created using a linspace and then repeated. I then try to explcitly check directly that element against the definition and it does say they are not equal:
So the only thing I can think of is that there is a precision mismatch occuring between the two and even though they are the same value, because of the precision difference, ML thinks they are different. The elements in the mesh and the definition are all double precision and the order range for that data type is more than enough so I prefer not to use another. I do use round for the Cr definition (Cr==0.03), so perhaps that is causing issues? I also do notice that there are more orders being included in the Cr value than what's in the mesh:
So this may hint at the issue? I thought that it was because of some hidden, nonzero number being present within the mesh node value, so I applied round() with the same order specification both to the mesh and the Cr value again, but that didn't work. So I am stuck for now.
  댓글 수: 2
Stephen23
Stephen23 2023년 6월 19일
편집: Stephen23 2023년 6월 19일
"even though they are the same value, because of the precision difference, ML thinks they are different."
They are not the same value. ML thinks that they are different values for a very simple reason: they are different values.
"I do use round for the Cr definition (Cr==0.03), so perhaps that is causing issues?"
Different calculations producing different values, which a user thinks "must" be the same. This is very common:
This is highly-recommended reading as well:
Your code tests for equivalence of floating point values, which is a buggy/fragile way to write code. You need to change your algorithm to take into account floating point error. The easy, standard, efficient, recommended approach is to compare the absolute difference against a tolerance (which you select), e.g.:
tol = 1e-10;
abs(A-B)<tol
or use ISMEMBERTOL. Do not use rounding, for the reasons explained here:
John
John 2023년 6월 19일
Thanks for the resources @Stephen23, I'll take a look at them

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by