## 'Domain error. To compute complex results from real x, use 'acos(complex(x))' error in matlab?

Najmeh Eskandari

### Najmeh Eskandari (view profile)

さんによって質問されました 2018 年 8 月 11 日

### Aks Lyu (view profile)

さんによって コメントされました 2019 年 12 月 11 日 5:20
Sebastian Castro

### Sebastian Castro (view profile)

さんの 回答が採用されました
hi.i want to simulate ur10 inverse kinematic and I did it.while running there is this error:
Domain error. To compute complex results from real x, use 'acos(complex(x))
how can I fix it?
the error is general and I don't know which line should be changed. there are several acos in the code .
X is the endeffector position and it can not be complex.
I have attached the Simulink file.

Walter Roberson

on 12 Aug 2018
Change them all.
Sebastian Castro

### Sebastian Castro (view profile)

on 13 Aug 2018
Unfortunately, "changing them all" wouldn't help in this case.
It actually doesn't have to do with the input being complex, but rather the input being outside the real valued range [-1,1] of the cos function.
Refer to my Answer below.

サインイン to comment.

## 1 件の回答

### Sebastian Castro (view profile)

Answer by Sebastian Castro

### Sebastian Castro (view profile)

on 13 Aug 2018
Edited by Sebastian Castro

### Sebastian Castro (view profile)

on 13 Aug 2018

Hi Najmeh,
If you supply a value greater than 1 or smaller than -1 to the acos function, you get a complex number. Simulink seems to be complaining about that in your code.
Theta5=acos(argumancos5);%negetive or positive answer?
Seems like at the very first time step, argumancos5 is exactly 1, but due to numerical issues, it's actually slightly higher than 1 which leads to your error. Computing the value argumancos5 - 1 gives me 3.1086e-15... so it's just outside the range in which the cosine is real-valued.
I limited the value of this input to the range [-1, 1] and it seems to have fixed the error:
argumancos5 = max(min(argumancos5,1),-1);
Theta5=acos();%negetive or positive answer?
- Sebastian

Gautami Golani

### Gautami Golani (view profile)

on 2 Jul 2019
Hey Najmeh,
Were you able to resolve this issue?
I'm currently simulating the inverse kinematics of the UR5 and facing the same problem.
Any help would be appreciated.

on 7 Aug 2019
Thank you.
Aks Lyu

### Aks Lyu (view profile)

on 11 Dec 2019 at 5:20
This is the answer!
But why MATLAB does not point out which line (file) the error occures in?

サインイン to comment.

Translated by