Unable to Set Equivalence Between Variables
2 次查看(过去 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1414284/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1414289/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1414294/image.png)
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
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:
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!