# Complex answer of acos function

5 views (last 30 days)
Aleks Boehler on 7 Sep 2020
Edited: John D'Errico on 7 Sep 2020
Hello,
in my programm I'm using the following function:
function t1 = t1_function(u_a0,u_cs0,u_e,l,cs,cp)
t1 = (1/(realsqrt((1/(l*cs))+(1/(l*cp)))))*acos(1-(2*u_a0*(1+(cp/cs))/(u_e+u_a0-u_cs0)));
end
The goal is to use different values of u_e, u_a0 and u_cs0 and get the time value t1.
I know that the acos function acos(x) will output a complex answer if the value of x is not between -1 and 1. So in the main file I sorted all the values for u_e, u_a0 and u_cs0 out where the inside of the acos bracket gets out of range.
% Constants
C_s = 390e-12;
C_p = 251e-12;
C_A = 28e-9;
L = 370e-6;
R_Last = 400;
I_Last = 0;
% For Input Value
q_Array = [400 0 -400];
% For saving data
i=1;
u_e_Array = [];
u_cs0_Array = [];
u_A0_Array = [];
t1_Array = [];
for a=1:1:3
u_e = q_Array(a);
for u_A0 = 0:10:400
for u_cs0 = -1500:10:1500
x = 1-(2*u_A0*(1+(C_p/C_s))/(u_e+u_A0-u_cs0)); % Here I make sure that the Value inside the acos bracket
% stays between -1 and 1
if (x > -1) && (x < 1)
u_e_Array(i) = u_e;
u_cs0_Array(i) = u_cs0;
u_A0_Array(i) = u_A0;
t1 = t1_function(u_A0,u_cs0,u_e,L,C_s,C_p);
t1_Array(i) = t1;
i = i+1;
end
end
end
end
Even though I checked the values of x for each iteration and it stayed in between -1 and 1, Matlab calculates a complex value for t1. Can anybody explain to me why?

John D'Errico on 7 Sep 2020
Edited: John D'Errico on 7 Sep 2020
The acos of a number that exceeds 1 or -1 is complex. This is the ONLY way that can happen, at least unless you have defined your own acos function, or the argument itself is already complex.
>> acos(1 + eps)
ans =
0 + 2.1073e-08i
>> acos(-1 - eps)
ans =
3.1416 - 2.1073e-08i
>> acos(1 + i)
ans =
0.90456 - 1.0613i
And it certainly LOOKS as if those arguments were between -1 and 1, right?
>> 1 + eps
ans =
1
>> -1 - eps
ans =
-1
Surely your eyes could not deceive you. Or could they? My point is, even though the number 1+eps LOOKS like it is 1, and gets reported as being 1 to as close as the command window can show, acos can tell the difference.
So, even though you are absolutely, 110% positive the arguments to acos are always falling in the interval [-1,1], I would bet you are mistaken here. I think I'm going to make money on that bet too. If you get a complex result some of the time, then there are only two ways that can happen (again, unless you have defined your own acos function.)

Sulaymon Eshkabilov on 7 Sep 2020
Hi,
It has been checked and your script is not giving ' complex' values for t1 or t1_Array. It is working ok.
Good luck

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by