Error using atan2 Inputs must be real.

2 views (last 30 days)
this programme and function keep saying :
*Error using atan2 Inputs must be real. line 4 'function file'
*Error line 14 'main programme
i made 'px,py,pz' as coments cause i dont know its supose to help
ps :didn't with it either
Main programme :
clear
clc
clf
a2=6.5;a3=0;d3=1.9;d4=6;
% px=[-0.7;-2.52;-1.13;-4.60;1.00;1.68];
% py=[11.70;10.10;1.54;7.10;5.00;1.18];
% pz=[-0.90;-1.63;-6.93;-1.38;-3.00;-6.94];
t=0:23;
px=30*t'.*cos(t');
py=1200-(30*t'.^2)/(50*pi);
pz=30*t'.*sin(t');
[theta,t1,t2,t3,t4,t5,t6]=part2(a2,a3,d3,d4,px,py,pz);
pxx=cos(t1).*(sin(t2+t3)*d4+a3*cos(t2+t3)+a2*cos(t2))-d3*sin(t1);
pyy=sin(t1).*(sin(t2+t3).*cos(t5)+sin(t2+t3)*d4+a3*cos(t2+t3)+a2*cos(t2));
pzz=cos(t2+t3)*d4-a3*sin(t2+t3)-a2*sin(t2);
figure(1),plot(t,t6,'r')
hold on
plot(t,t1,'k')
plot(t,t2,'b')
plot(t,t3,'m')
plot(t,t4,'g')
plot(t,t5,'c')
grid on
hod off
figure(2)
plot3(0.001*px,0.005*py,0.001*pz,'r',pxx,pyy,pzz,'b')
grid on
hold off
The function file :
function [theta,t1,t2,t3,t4,t5,t6]=part2(a2,a3,d3,d4,px,py,pz)
t1=atan2(px(1),py(1))-atan2(d3,sqrt(px(1)^2+py(1)^2-d3^2));
k=(px(1)^2+py(1)^2+pz(1)^2-a2^2-a3^2-d3^2-d4^2)/2*a2;
t3=atan2(a3,d4)-atan2(k,sqrt(a3^2+d4^2-k(1)^2));
t23=antan2((-a3-a2*cos(t3))*pz(1)-(cos(t1)*px(1)+sin(t1)*py(1))*(d4-a2*sin(t3)),(a2*sin(t3)-d4)*pz(1)+(a3+a2*cos(t3))*(cos(t)*px(1)+sin(t1)*px(1)+sin(t1)*py(1)));
t2=t23-t3;t4=0;t5=0;t6=0;
c1=cos(t1);s1=sin(t1);c2=cos(t2);s2=sin(t2);c3=cos(t3);s3=sin(t3);c23=cos(t23);s23=sin(t23);c4=cos(t4);c5=cos(t5);s5=sin(t5);c6=cos(t6);s6=sin(t6);
r13=-c1*(c23*c4*s5+s23*c5)-s1*s4*s5;
r23=-s1*c(23*c4*s5+s23*c5)+c1*s4*s5;
r33=s23*c4*s5-c23*c5;
r11=c1*(c23*(c4*c5*c6-s4*s6)-s23*s5*c6)+s1*(s4*c5*c6+c4*s6);
r21=s1*(c23*(c4*c5*c6-s4*s6)-s23*s5*c6)-c1*(s4*c5*c6+c4*s6);
r31=-s23*(c4*c5*c6-s4*s6)-c23*s5*c6;
for i=2:24
t1(i,1)=atan2(px(i,1),py(i,1))-atan2(d3,sqrt(px(i,1)^2+py(i,1)^2-d3^2));
k(i,1)=(px(i,1)^2+p(i,1)^2+pz(i,1)^2-a2^2-a3^2-d3^2-d4^2)/2*a2;
t3(i,1)=atan2(a3,d4)-atan2(k(i,1),sqrt(a3^2+d4^2-k(i,1)^2));
t23(i,1)=atan2((-a3-a2*cos(t3(i-1,1)))*pz(i,1)-cos(t1(i-1,1))*px(i,1)+sin(t1(i-1,1))*py(i,1)*(d4-a2*sin(t3(i-1,1))),(a2*sin(t3(i-1,1))-d4)*pz(i,1)+(a3+a2*cos(t3(i-1,1)))*(cos(t1(i-1,1))*px(i,1)+sin(t1(i-1,1))*px(i,1)+sin(t1(i-1,1))*py(i,1)));
t2(i,1)=t23(i,1-t3(i,1));
c1=cos(t1(i-1,1));s1=sin(t1(i-1,1));c2=cos(t2(i-1,1));s2=sin(t2(i-1,1));c3=cos(t3(i-1,1));
s3=sin(t3(i-1,1));c23=cos(t23(i-1,1));s23=sin(t23(i-1,1));c4=cos(t4(i-1,1));s4=sin(t4(i-1,1));
c5=cos(t5(i-1,1));s5=sin(t5(i-1,1));c6=cos(t6(i-1,1));s6=cos(t6(i-1,1));
r13(i-1,1)=-c1*(c23*c4*s5+s23*c5)-s1*s4*s5;
r23(i-1,1)=-s1*(c23*c4*s5+s23*c5)+c1*s4*s5;
r33(i-1,1)=s23*c4*s5-c23*c5;
t4(i,1)=atan2(-r13(i-1,1)*s1+r23(i-1,1)*c1,-r13(i-1,1)*c1*c23-r23(i-1,1)*s1*c23+r33(i-1,1)*s23);
s5=-r13(i-1,1)*(c1*c23*c4+s1*s4)+r23(i-1,1)*(s1*c23*c4-c1*s4)-r33(i-1,1)*s23*c4;
c5=r11(i-1,1)*(-c3*s23)+r23(i-1,1)*(-s1*s23)+r33(i-1,1)*(-c23);
t5(i,1)=atan2(s5,c5);
r11(i-1,1)=c1*(c23*(c4*c5*c6-s4*s6)-s23*s5*c6)+s1*(s4*c5*c6+c4*s6);
s6=-r11(i-1,1)*(c1*c23*s4-s1*c4)-r21(i-1,1)*(s1*c23*s4+c1*c4)+r31(i-1,1)*s23*s4;
c6=r11(i-1,1)*((c1*c23*c4+s1*s4)*c5-c1*s23*s5)+r21(i-1,1)*((s1*c23*c4-c1*s4)*c5-c1*s23*s5)-r31(i-1,1)*(s23*c4*c5+c23*s5);
t6(i,1)=atan2(s6,c6);
end
theta=[t1,t2,t3,t4,t5,t6]

Accepted Answer

Star Strider
Star Strider on 14 Jan 2021
If you want to compute the phase angle of a complex number, use the angle function.
  2 Comments
abouda tebba
abouda tebba on 17 Jan 2021
It wasnt requested for But ill try it thank you

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 17 Jan 2021
Set an error breakpoint and run your code. When MATLAB stops on that line, look at the values you're passing into atan2 in the call on that line. My guess is that while you expect the value whose square root you're passing into atan2 to be either positive or zero it's actually very slightly negative due to floating point issues.
x = 0.3-0.2-0.1
x = -2.7756e-17
In exact arithmetic, x would be 0. But none of one-tenth, two-tenths, or three-tenths can be exactly represented in double precision so it's not. So sqrt(x) is complex not 0.
sqrt(x)
ans = 0.0000e+00 + 5.2684e-09i

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!