# Can you help and show if any where ive gone wrong with any codes

6 views (last 30 days)
Daniel Martelli on 13 Sep 2021
Commented: Tanmay Das on 30 Dec 2021 at 12:30
This code is only for Position Analysis
clc; close all; clear; format compact;
r1 = 100;
r2 = 50;
r3 = 780;
r4 = 80;
l4 = 125;
% Angle theta 2
th2 = pi/3:2*pi/36:2*pi+pi/3;
n = length(th2)
% Calculations
L = sqrt(r1^2+r2^2-2*r1*r2.*cos(th2));
Sphi = (r2./L).*sin(th2);
Cphi = (r2^2-r1^2-L.^2)./(-2*r1.*L);
phi = atan2(Sphi, Cphi);
th3 = acos((r4^2-r3^2-L.^2)./(-2*r3.*L))-phi;
Cgamma = (r3^2-r4^2-L.^2)./(-2*r4.*L);
Sgamma = (r3/r4).*sin(th3+phi);
gamma = atan2(Sgamma, Cgamma);
th4 = pi-phi-gamma;
% Use this to find the 3rd join on link 4
all = th4-eta;
% Position of joints
xO2 = 0;
yO2 = 0;
xA = r2*cos(th2);
yA = r2*sin(th2);
xB = xA+r3*cos(th3);
yB = yA+r3*sin(th3);
xO4 = r1;
yO4 = 0;
xC5 = xO4+l4*cos(all);
yC5 = yO4+l4*sin(all);
% These are used to plot the length of each link in the Draw-Linkage
Xcrank = [xO2, xA(1)];
Ycrank = [yO2, yA(1)];
Xcoupler = [xA(1), xB(1)];
Ycoupler = [yA(1), yB(1)];
Xrocker = [xB(1), xO4];
Yrocker = [yB(1), yO4];
Xground = [-1.2*r2, r1+10];
Yground = [yO2, yO4];
XC4C5 = [xO4, xC5(1)];
YC4C5 = [yO4, yC5(1)];
XBC5 = [xB(1), xC5(1)];
YBC5 = [yB(1), yC5(1)];
Xcrank, Ycrank, 'r', Xcoupler, Ycoupler, 'b',...
Xrocker, Yrocker, 'g', XC4C5, YC4C5, 'g',...
XBC5, YBC5, 'g', Xground, Yground, '--k', 'linewidth', 1.2);
axis([-r2-r3 2*r2+r3 -2*r2 r4]);
axis equal
for i=1:n
Xcrank = [xO2, xA(i)];
Ycrank = [yO2, yA(i)];
Xcoupler = [xA(i), xB(i)];
Ycoupler = [yA(i), yB(i)];
Xrocker = [xB(i), xO4];
Yrocker = [yB(i), yO4];
Xground = [-1.2*r2, r1+10];
Yground = [yO2, yO4];
XC4C5 = [xO4, xC5(i)];
YC4C5 = [yO4, yC5(i)];
XBC5 = [xB(i), xC5(i)];
YBC4 = [yB(i), yC5(i)];
Xcrank, Ycrank, 'r', Xcoupler, Ycoupler, 'b',...
Xrocker, Yrocker, 'g', XC4C5, YC4C5, 'g',...
XBC5, YBC5, 'g', Xground, Yground, '--k', 'linewidth', 1.2);
axis([-r2-r3 2*r2+r3 -2*r2 r4]);
axis equal
F(i) = getframe;
pause(1/20);
end
movie(F, 2);

Tanmay Das on 19 Sep 2021
Hi,
atan2(Y,X) returns the four-quadrant inverse tangent (tan-1) of Y and X, which must be real. In the given code, Sgamma is a vector of complex quantities. So it generates an error that "Inputs must be real".
##### 2 CommentsShowHide 1 older comment
Tanmay Das on 30 Dec 2021 at 12:30
In your code, you have written these lines:
Sgamma = (r3/r4).*sin(th3+phi);
gamma = atan2(Sgamma, Cgamma);
where Sgamma comes out to be a complex quantity. (You may try printing the value of Sgamma in command window)
However, 'atan2' function only takes real input arguments. So, it gnerated the error that "Inputs must be real"