I get complex numbers while using "acosd" function

25 次查看(过去 30 天)
Hello everyone,
I would get complexe number when I use acosd function to caclulate cosine inverse of a value, which I am pressty sure is in the range of [-1,1]. Look at the example below. Can anyone let me know where the problem is?
GRD_u=[-0.6372,1.7170];
A_s_uniqe=[0.3479,-0.9375];
y=acosd(dot(GRD_u,A_s_uniqe)/sqrt(sum(GRD_u.^2))/sqrt(sum(A_s_uniqe.^2)))

采纳的回答

VBBV
VBBV 2023-6-26
编辑:VBBV 2023-6-26
Probably you might have evaluated the equation using incorrect parenthesis enclosing only the dot product for acosd function and dividing the rest of terms in sqrt function as shown below
GRD_u=[-0.6372,1.7170];
A_s_uniqe=[0.3479,-0.9375];
% the dot product is out of range [-1 1]
dot(GRD_u,A_s_uniqe)
ans = -1.8314
% you might have used incorrect parenthesis while evaluating equation
%------------------------->>>
y=acosd(dot(GRD_u,A_s_uniqe))/sqrt(sum(GRD_u.^2))/sqrt(sum(A_s_uniqe.^2))
y = 98.2871 -37.9687i
y=acosd(dot(GRD_u,A_s_uniqe)/sqrt(sum(GRD_u.^2))/sqrt(sum(A_s_uniqe.^2)))
y = 179.9990
Otherwise, it seems to return real value

更多回答(2 个)

Mrinal Anand
Mrinal Anand 2023-6-25
You are getting a complex number because the acosd() function is only defined for values in the range of [-1, 1]. If the function argument is outside this range, it will return a complex number.
In your code, the argument passed to acosd() is calculated as the dot product of GRD_u and A_s_uniqe divided by the product of their magnitudes. If the magnitude of either GRD_u or A_s_uniqe is less than the dot product of the two vectors, then the argument passed to acosd() will be greater than 1, which is outside the defined range.
  1 个评论
VBBV
VBBV 2023-6-26
@Mrinal Anand, The magnitude of both GRD_u or A_s_uniqe vectors are not less than dot product but its the usage of incorrect parenthesis in equation that would result in complex valued number
GRD_u=[-0.6372,1.7170];
A_s_uniqe=[0.3479,-0.9375];
% dot product
dot(GRD_u,A_s_uniqe)
ans = -1.8314
% Magnitude if both vectors
Mag1 = sqrt(GRD_u(1)^2 + GRD_u(2)^2)
Mag1 = 1.8314
Mag2 = sqrt(A_s_uniqe(1)^2 + A_s_uniqe(2)^2)
Mag2 = 1.0000

请先登录,再进行评论。


dpb
dpb 2023-6-25
编辑:dpb 2023-6-25
GRD_u=[-0.6372,1.7170];
A_s_uniqe=[0.3479,-0.9375];
y=acosd(dot(GRD_u,A_s_uniqe)/sqrt(sum(GRD_u.^2))/sqrt(sum(A_s_uniqe.^2)))
y = 179.9990
format longE
arg=dot(GRD_u,A_s_uniqe)/sqrt(sum(GRD_u.^2))/sqrt(sum(A_s_uniqe.^2))
arg =
-9.999999998594938e-01
The particular set of values does't cause the problem, but argument is pretty close to -1; it's possible rounding error in floating point numbers could cause a calculation to be just on the other side for a given case.
I'm also guessing that the numbers above are rounded manual inputs from a set of calculations and that for the actual computed values, the above issue occurred -- but the lack of precision with only four decimal places changes the results.
Would have to post the exact values/code that created the problem, but I'll bet if you put the calc in a try...catch block and verify the actual value when it errors or assert() that the abs(value) is <=1 before the call you'll find it's close, but on the wrong side of the tracks.
  1 个评论
dpb
dpb 2023-6-25
编辑:dpb 2023-6-25
The solution will be to use acosd(sign(x)*min(abs(x),1)) in order to bound the calculation to be within [-1,1].
You probably also want to wrap that inside a test to confirm that it is only a rounding isse and not that the code has gone completely off the rails so that you're not just silently passing through nonsensical results.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by