Unable to Set Equivalence Between Variables

1 次查看(过去 30 天)
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