Can anyone explain why I am getting an error here? This was a code given to my by my teacher for a project, but it will not work for me. It is for a linkage analysis.
3 views (last 30 days)
Show older comments
I attached the full code below that is giving me an error. It is for a machine dynamics project, and was a
code given to us by the professor in the class. I am unable to get a hold of them, so thought I would post here.
The error is included as well for context.
*Note: The inputs for the code, in case that is relevant, are:
G = 34 deg
R = 3.2
f = 53.5845
h = 21.3672
beta = 155 deg
gamma = 145 deg
the2_start = 225 deg
There is a seperate code attached to this where the inputs are entered.
I am not having issues with that code so it did not seem relevant.
Thanks
ERROR:
Error using atan2
Inputs must be real.
Error in fivebar_position (line 60)
the51(i)=atan2(BB-g1(i)*sin(gammar),AA-g1(i)*cos(gammar));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FULL CODE:
function [the2, the3, the4, trans1, Px, Py, g2, the52, trans2]=fivebar_position(a,b,c,d,P,delta3,the2_start,f,beta,h,gamma)
deg2rad=pi/180; % convert degree to radian
rad2deg=180/pi; % convert radian to degree
K1=d/a; % Eq. (4.8a)
K2=d/c; % Eq. (4.8a)
K3=(a^2-b^2+c^2+d^2)/(2*a*c); % Eq. (4.8a)
K4=d/b; % Eq. (4.11b)
K5=(c^2-d^2-a^2-b^2)/(2*a*b); % Eq. (4.11b)
the2=239*deg2rad; % generate an array for theta2
n=length(the2); % length of the array
for i=1:n
A=cos(the2(i))-K1-K2*cos(the2(i))+K3;
B=-2*sin(the2(i));
C=K1-(K2+1)*cos(the2(i))+K3;
D=cos(the2(i))-K1+K4*cos(the2(i))+K5;
E=-2*sin(the2(i));
F=K1+(K4-1)*cos(the2(i))+K5;
disc=sqrt(B^2-4*A*C);
temp=2*atan2(-B-disc,2*A); % for open configuration
if (temp <= 0) % make theta4 positve
the4(i)=2*pi+temp;
else
the4(i)=temp;
end
disc=sqrt(E^2-4*D*F);
temp=2*atan2(-E-disc,2*D); % for open configuration
if (temp <= 0) % make theta3 positive
the3(i)=2*pi+temp;
else
the3(i)=temp;
end
temp=abs(the3(i)-the4(i)); % transmission angles
if (temp > 1.5*pi)
trans1(i)=2*pi-temp;
elseif (temp > pi)
trans1(i)=temp-pi;
elseif (temp > pi/2)
trans1(i)=pi-temp;
else
trans1(i)=temp;
end
Px(i)=a*cos(the2(i))+P*cos(the3(i)+delta3); % coupler point
Py(i)=a*sin(the2(i))+P*sin(the3(i)+delta3); % x and y
end
for i=1:n
AA=a*cos(the2(i))+P*cos(the3(i)+delta3)-d-h*cos(beta);
BB=a*sin(the2(i))+P*sin(the3(i)+delta3)-h*sin(beta);
C1=-2*(AA*cos(gamma)+BB*sin(gamma));
C2=AA^2+BB^2-f^2;
dis=sqrt(C1^2-4*C2);
g1(i)=(-C1+sqrt(C1^2-4*C2))/2;
g2(i)=(-C1-sqrt(C1^2-4*C2))/2;
the51(i)=atan2(BB-g1(i)*sin(gamma),AA-g1(i)*cos(gamma));
the52(i)=atan2(BB-g2(i)*sin(gamma),AA-g2(i)*cos(gamma));
if the51(i) < 0
the51(i)=the51(i)+2*pi;
end
if the52(i) < 0
the52(i)=the52(i)+2*pi;
end
trans2(i)=pi/2-(the52(i)-gamma);
if trans2(i) > pi/2
trans2(i)=pi-trans2(i);
end
end
0 Comments
Answers (1)
Sam Chak
on 3 Apr 2025
Hi @Ben
If the error message says that the inputs of atan2() must be real, then one or both of the inputs must be complex. What in the world of MATLAB functions could possibly produce a complex number? My mathematical instincts suggest that the usual suspect is the square root function!
sqrt(-4)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!