Dear MATLAB community,
I am running 'if' statement and encounter a problem as such:
The logical returns 'false', but the values themselves do equilibrate. Confused.
Thanks,

2 个评论

It doesn't appear to be an order of operations problem, but I'd throw explicit ()s to force the proper comparison just to double-check.
Then, try looking more than 4 digits past the decimal. Either format long, or fprintf(1,'%.12f\n',ans)
Having a | at that location is not syntactically valid. It is not possible to have a numeric operator directly before or after a logical operator.

请先登录,再进行评论。

 采纳的回答

Star Strider
Star Strider 2017-1-4

1 个投票

I can’t run your code because I don’t have the data. however I believe you’re encountering a problem with Operator Precedence. The equality up to the or operator is being evaluated as a single expression, and then being compared to ‘cosd(theta)’.
Judicious use of parentheses could resolve that, but given the structure of the line you quote, I make no guarantees.

4 个评论

Thanks for your reply. I tried forcing with (),but still it is giving me false answer. The code is as follow:
0 0.707106781186548 1.41421356237310 2.12132034355964 2.82842712474619 0 0.707106781186548 1.41421356237310 2.12132034355964 2.82842712474619 0 0.707106781186548 1.41421356237310 2.12132034355964 2.82842712474619 0 0.707106781186548 1.41421356237310 2.12132034355964 2.82842712474619 0 0.707106781186548 1.41421356237310 2.12132034355964 2.82842712474619 0.353553390593274 1.06066017177982 1.76776695296637 2.47487373415292 0.353553390593274 1.06066017177982 1.76776695296637 2.47487373415292 0.353553390593274 1.06066017177982 1.76776695296637 2.47487373415292 0.353553390593274 1.06066017177982 1.76776695296637 2.47487373415292
0 0 0 0 0 0.707106781186548 0.707106781186548 0.707106781186548 0.707106781186548 0.707106781186548 1.41421356237310 1.41421356237310 1.41421356237310 1.41421356237310 1.41421356237310 2.12132034355964 2.12132034355964 2.12132034355964 2.12132034355964 2.12132034355964 2.82842712474619 2.82842712474619 2.82842712474619 2.82842712474619 2.82842712474619 0.353553390593274 0.353553390593274 0.353553390593274 0.353553390593274 1.06066017177982 1.06066017177982 1.06066017177982 1.06066017177982 1.76776695296637 1.76776695296637 1.76776695296637 1.76776695296637 2.47487373415292 2.47487373415292 2.47487373415292 2.47487373415292
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439 0.866025403784439
This is the matrix xn(3x41) containing a bunch of coordinates.
l = 1;
theta = 60;
These are the parameters. This logical statement:
(abs(xn(1,29) - xn(1,5) )) == (0.5 * sqrt(2) * l * cosd(theta))
is still returning false whereas the values on both sides are the same.
Thanks,
My pleasure.
No, they’re not the same:
check = (abs(xn(1,29) - xn(1,5) )) - (0.5 * sqrt(2) * l * cosd(theta))
check =
-3.77475828372553e-15
This was my second hypothesis. For details, see Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero?
Thank you! Happy New Year and good luck with your work!
As always, my pleasure!
Happy New Year to you too! I wish you the same!

请先登录,再进行评论。

更多回答(1 个)

Yan
1. with command vpa taking below 15 precision digits seems to work ok:
vpa(0.5 * sqrt(2) * l * cosd(theta),10)== vpa(abs(xn(1,29) - xn(1,5) ),10)
ans =
0.35355339059327376208252680100941 == 0.35355339059327376208252680100941
>> n=0;if vpa(0.5 * sqrt(2) * l * cosd(theta),10)== vpa(abs(xn(1,29) - xn(1,5) ),10) n=1; end
n
=
1
2. when not reducing the amoung of digits used there is a small difference:
(0.5 * sqrt(2) * l * cosd(theta))-(abs(xn(1,29) - xn(1,5) ))
=
3.774758283725532e-15
the compared values:
(0.5 * sqrt(2) * l * cosd(theta))
=
0.353553390593274
>> (abs(xn(1,29) - xn(1,5) ))
=
0.353553390593270
cosd.m is not available with command type
type cosd.m
%COSD Cosine of argument in degrees.
% COSD(X) is the cosine of the elements of X, expressed in degrees.
% For odd integers n, cosd(n*90) is exactly zero, whereas cos(n*pi/2)
% reflects the accuracy of the floating point value for pi.
%
% Class support for input X:
% float: double, single
%
% See also ACOSD, COS.
% Copyright 1984-2010 The MathWorks, Inc.
% Built-in function.
.
but the comment hints that function cos is subject to precision of pi, therefore cosd may have the accurate value while perhaps the values of xn may be the ones that have too many decimals.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

3 个评论

Thank you John. Happy New Year and good luck with your work!
John BG
John BG 2017-1-5
编辑:Jan 2017-1-16
happy 2017 to you too.
but I provided a valid answer I kindly ask you to consider marking my answer as the Accepted Answer for your question.
Awaiting answer
John BG
"the comment hints" and "the value of xn may be the ones that have too many decimal places". Hypotheses.
Star Strider provided a specific numeric analysis and a link to the faq on the topic, and did so hours before you posted your answer.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Variables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by