How to fix Error using atan2 Inputs must be real?

Function
function ConcLin = Line(A,B)
%% Linear Moving along desired path
% **** Linear First Movement ***
% Calling Inverse Kinematics to
%% number of increments
ResLin = 100;
%% Equations
DeltaX = (B(1,1) - A(1,1)) / ResLin;
DeltaY = (B(1,2) - A(1,2)) / ResLin;
DeltaZ = (B(1,3) - A(1,3)) / ResLin;
%% Looping through each point of the line
tic;
AnglesLin = zeros(6); %preallocating memory
for f = 1:ResLin
%% calculating actual point
A(1,1) = A(1,1) + DeltaX * f;
A(1,2) = A(1,2) + DeltaY * f;
A(1,3) = A(1,3) + DeltaZ * f;
AnglesLin(f,:) = IKine(A);
end
toc
%% Increments of Time
tic;
TimeLin = zeros(1); %preallocating memory
for clockLin = 1:ResLin
TickTockLin = clockLin*0.1+10;
TimeLin(clockLin,:) = TickTockLin;
end
toc
%% Concatenating Time & Angles. Time will be the first column
ConcLin = [TimeLin,AnglesLin];
Main
%% Coordinates Input
% LINE desired Paths. If Input method is used, comment out this block of code.
% *****We will define these as one matrix 1x12*****
Lin1 = [750, -75, 670, 0, 0, 1, 0, -1, 0, 1, 0, 0];%A
% Xa = Lin1(1,1); Ya = Lin1(1,2); Za = Lin1(1,3);
Lin2 = [750, -75, 550, 0, 0, 1, 0, -1, 0, 1, 0, 0];%B
% Xb = Lin2(1,1); Yb = Lin2(1,2); Zb = Lin2(1,3);
Lin3 = [750, 75, 550, 0, 0, 1, 0, -1, 0, 1, 0, 0];
Lin4 = [750, 75, 670, 0, 0, 1, 0, -1, 0, 1, 0, 0];
Lin5 = [750, 0, 700, 0, 0, 1, 0, -1, 0, 1, 0, 0];
Lin6 = [750, 0, 550, 0, 0, 1 0, -1, 0, 1, 0, 0];
%% Calling Linear Motion
ConcLin = Line(Lin1, Lin2);

5 Comments

Torsten
Torsten on 26 Jan 2022
Edited: Torsten on 26 Jan 2022
Make sure that the two parameters you pass to atan2 are real, not complex.
E.g. the sqrt from the screenshot can become complex if the argument is < 0.
@Torsten Can you please give an example?
Is there a method or built-in-function that can be use to keep or hold it as a real number?
Torsten
Torsten on 26 Jan 2022
Edited: Torsten on 26 Jan 2022
1-(K/p3)^2 becomes negative, so the sqrt of this expression gives a complex result.
Since I don't know what this expression means, I can give no further advice.
You're assuming d3/p1 is strictly less than or equal to 1. What guarantee do you have that this is the case?
If it was ever so slightly greater than 1:
d3 = 2;
p1 = 2 - eps(2); % Just barely less than 2
s = (d3/p1)
s = 1.0000
s > 1 % true
ans = logical
1
sqrt(1-s^2) % complex
ans = 0.0000e+00 + 2.1073e-08i
You can use min and max to ensure s is strictly in a desired range.
Thank you. Here is the solution.:
When we move from previous point to next point, the coordinate equals to the previous coordinate plus the Delta, so we don't need to multiply f.
Before:
for f = 1:ResLin
%% calculating actual point
A(1,1) = A(1,1) + DeltaX * f;
A(1,2) = A(1,2) + DeltaY * f;
A(1,3) = A(1,3) + DeltaZ * f;
AnglesLin(f,:) = IKine(A);
Correct:
for f = 1:ResLin
%% calculating actual point
A(1,1) = A(1,1) + DeltaX;
A(1,2) = A(1,2) + DeltaY;
A(1,3) = A(1,3) + DeltaZ;
AnglesLin(f,:) = IKine(A);

Sign in to comment.

 Accepted Answer

When we move from previous point to next point, the coordinate equals to the previous coordinate plus the Delta, so we don't need to multiply f.
Before:
for f = 1:ResLin
%% calculating actual point
A(1,1) = A(1,1) + DeltaX * f;
A(1,2) = A(1,2) + DeltaY * f;
A(1,3) = A(1,3) + DeltaZ * f;
AnglesLin(f,:) = IKine(A);
Correct:
for f = 1:ResLin
%% calculating actual point
A(1,1) = A(1,1) + DeltaX;
A(1,2) = A(1,2) + DeltaY;
A(1,3) = A(1,3) + DeltaZ;
AnglesLin(f,:) = IKine(A);
Now it works. @Torsten @Steven Lord Thanks for helping. I learn a lot from these discussions. Cheers!

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!