16 views (last 30 days)

Show older comments

The following code creates an animation of a four bar linkage. Can someone explain how you can modify the code to plot the path traced by point P during the animation? I have tried creating an animated line by using a separate for loop and adding plot commands inside the for i = 1:length(theta2) for loop but when that is done the point P's path is plotted but because of the hold on and hold off commands the path is deleted at every instant and at any given moment only the point P is located at during that instant is plotted.

L0 = 7; L1 = 3; L2 = 7.5; L3 = 4.5;

L_PA = 4; alpha =35;

w2 = 5; theta2 = 0:2:360;

for i = 1:length(theta2)

AC(i) = sqrt(L0^2+L1^2 - 2*L0*L1*cosd(theta2(i)));

beta(i) = acosd((L0^2+AC(i)^2-L1^2)/(2*L0*AC(i)));

psi(i) = acosd((L2^2+AC(i)^2-L3^2)/(2*L2*AC(i)));

lamda(i) = acosd((L3^2+AC(i)^2-L2^2)/(2*L3*AC(i)));

theta3(i) = psi(i)-beta(i);

theta4(i) = 180-lamda(i)-beta(i);

if theta2(i)>180

theta3(i) = psi(i)+beta(i);

theta4(i) = 180-lamda(i)+beta(i);

end

Ox(i) = 0; Oy(i) = 0;

Ax(i) = Ox(i) + L1*cosd(theta2(i));

Ay(i) = Oy(i) + L1*sind(theta2(i));

Bx(i) = Ox(i) +Ax(i) + L2*cosd(theta3(i));

By(i) = Oy(i) +Ay(i) + L2*sind(theta3(i));

Cx(i) = L0; Cy(i) = 0;

Px(i) = Ax(i) +L_PA *cosd(alpha + theta3(i));

Py(i) = Ay(i) +L_PA *sind(alpha + theta3(i));

theta5(i) = alpha + theta3(i);

plot([Ox(i) Ax(i)],[Oy(i) Ay(i)], [Ax(i) Bx(i)],...

[Ay(i) By(i)]...

,[Bx(i) Cx(i)],[By(i) Cy(i)],'LineWidth',3);

hold on

plot([Ax(i) Px(i)],[Ay(i) Py(i)],...

[Bx(i) Px(i)],[By(i) Py(i)],'LineWidth',3);

grid on;

axis equal;

axis([-5 15 -5 10]);

drawnow;

hold off

end

Cris LaPierre
on 15 Feb 2021

Edited: Cris LaPierre
on 15 Feb 2021

I would suggest pulling all the calculations out of the for loop. You can take advantage of MATLAB's vector math to calculate all the results and even plot the starting configuration. Then use a loop to update the XData and YData values of the links. This way, you have access to all the data and can follow a similar approach to create a line that traces P.

L0 = 7; L1 = 3; L2 = 7.5; L3 = 4.5;

L_PA = 4; alpha =35;

w2 = 5; theta2 = 0:2:360;

% Convert operators on vectors to elementwise operators

AC = sqrt(L0^2+L1^2 - 2*L0*L1*cosd(theta2));

beta = acosd((L0^2+AC.^2-L1^2)./(2*L0*AC));

psi = acosd((L2^2+AC.^2-L3^2)./(2*L2*AC));

lamda = acosd((L3^2+AC.^2-L2^2)./(2*L3*AC));

% These 2 lines might be a little tricky. Break it into parts to figure out how it works.

theta3 = (theta2<=180).*(psi-beta) + (theta2>180).*(psi+beta);

theta4 = (theta2<=180).*(180-lamda-beta) + (theta2>180).*(180-lamda+beta);

Ox = 0; Oy = 0;

Ax = Ox + L1*cosd(theta2);

Ay = Oy + L1*sind(theta2);

Bx = Ox +Ax + L2*cosd(theta3);

By = Oy +Ay + L2*sind(theta3);

Cx = L0; Cy = 0;

Px = Ax +L_PA *cosd(alpha + theta3);

Py = Ay +L_PA *sind(alpha + theta3);

theta5 = alpha + theta3;

% Create the original figure by plotting just the first points in the vectors

AB = plot([Ox Ax(1)],[Oy Ay(1)], ...

[Ax(1) Bx(1)],[Ay(1) By(1)],...

[Bx(1) Cx],[By(1) Cy],'LineWidth',3);

hold on

ABP = plot([Ax(1) Px(1)],[Ay(1) Py(1)],...

[Bx(1) Px(1)],[By(1) Py(1)],'LineWidth',3);

hold off

grid on;

axis equal;

axis([-5 15 -5 10]);

% Use the loop to step though all the link positions, updating the corresponding X and Y values

for i = 2:length(theta2)

AB(1).XData = [Ox Ax(i)];

AB(1).YData = [Oy Ay(i)];

AB(2).XData = [Ax(i) Bx(i)];

AB(2).YData = [Ay(i) By(i)];

AB(3).XData = [Bx(i) Cx];

AB(3).YData = [By(i) Cy];

ABP(1).XData = [Ax(i) Px(i)];

ABP(1).YData = [Ay(i) Py(i)];

ABP(2).XData = [Bx(i) Px(i)];

ABP(2).YData = [By(i) Py(i)];

drawnow

end

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

Start Hunting!