Problem with plotting a mechanism

5 views (last 30 days)
Terry Geraldsen
Terry Geraldsen on 17 Oct 2020
Answered: Satwik on 27 Mar 2025
Hello! I've never used MATLAB's Answer forum so I hope I'm doing this correctly... I am working on writing a program that: first finds the unknown values of the variables for the mechanism, then plots the position of a mechanism and highlights a certain point's path of motion (Point D). I was able to plot the mechanism's links but I cannot figure out how to plot the path of point D. I'm presuming this is the incorrect way to use the plot function. TL;DR: Is there a way for me to keep the path of point D when it cycles the for loop? (For discretion of the class project and protection of my academic honesty, I'm only including the section of code that involves the plotting. If needed I can post the full code)
for theta2=3.316:0.1:11.316
%Function that solves the unknown variables for each value of theta2:
[theta3, r3, theta5, r6, theta4] = theta(theta2, theta3, r3, theta5, r6, theta4);
%Values of data measured:
r3values(forcount)=r3;
r6values(forcount)=r6;
t3values(forcount)=theta3;
t5values(forcount)=theta5;
t4values(forcount)=theta4;
%Plotting the data
%Link r2 (Driving link)
plot([r2*cos(theta2) 0], [r2*sin(theta2) 0],'bo-');hold on;
%Link r3
plot([r1*cos(theta1) r2*cos(theta2)],[r1*sin(theta1) r2*sin(theta2)],'bo-');hold on;
%Link r4
plot([r1*cos(theta1) r4*cos(theta4)],[r1*sin(theta1) 0.9+r4*sin(theta4)],'bo-');hold on;
%Vector rD (Not in the mechanism, but vector points to the coupler point D)
plot([r1*cos(theta1) r4*cos(theta3)+rd5*cos(theta5+thetaoff5)], [r1*sin(theta1) ...
r1+r4*sin(theta3)+rd5*sin(theta5+thetaoff5)],'bo-');hold on;
%%Coupler point D path [!!What I'm having trouble with!!]
plot(r4*cos(theta3)+rd5*cos(theta5+thetaoff5),...
r1+r4*sin(theta3)+rd5*sin(theta5+thetaoff5),'--g','linewidth',1.5);hold on;
%r6
plot([r4*cos(theta4) r6*cos(theta6)],[0.9+r4*sin(theta4) 0.9+r6*sin(theta6)],'bo-');hold off;
%done
%plot([r1*cos(theta1) 0],[r1*sin(theta1) 0],'bo-');hold off;
axis([-5 5 -5 5]);
grid on;
pbaspect([1 1 1]);
pause(0.1);
forcount = forcount + 1;
end

Answers (1)

Satwik
Satwik on 27 Mar 2025
To keep the path of point D visible throughout the entire simulation, we can accumulate the coordinates of point D during each iteration of the loop and plot them all together after the loop completes. Here is a modified version of the code which implements this:
% Initialize arrays to store the path of point D
xD_path = [];
yD_path = [];
for theta2 = 3.316:0.1:11.316
% Function that solves the unknown variables for each value of theta2:
[theta3, r3, theta5, r6, theta4] = theta(theta2, theta3, r3, theta5, r6, theta4);
% Values of data measured:
r3values(forcount) = r3;
r6values(forcount) = r6;
t3values(forcount) = theta3;
t5values(forcount) = theta5;
t4values(forcount) = theta4;
% Calculate the coordinates of point D
xD = r4 * cos(theta3) + rd5 * cos(theta5 + thetaoff5);
yD = r1 + r4 * sin(theta3) + rd5 * sin(theta5 + thetaoff5);
% Store the coordinates of point D
xD_path = [xD_path, xD];
yD_path = [yD_path, yD];
% Plotting the mechanism
hold off; % Clear the plot for the current frame
% Link r2 (Driving link)
plot([r2*cos(theta2) 0], [r2*sin(theta2) 0], 'bo-'); hold on;
% Link r3
plot([r1*cos(theta1) r2*cos(theta2)], [r1*sin(theta1) r2*sin(theta2)], 'bo-'); hold on;
% Link r4
plot([r1*cos(theta1) r4*cos(theta4)], [r1*sin(theta1) 0.9+r4*sin(theta4)], 'bo-'); hold on;
% Vector rD
plot([r1*cos(theta1) xD], [r1*sin(theta1) yD], 'bo-'); hold on;
% r6
plot([r4*cos(theta4) r6*cos(theta6)], [0.9+r4*sin(theta4) 0.9+r6*sin(theta6)], 'bo-'); hold on;
% Plot the path of point D
plot(xD_path, yD_path, '--g', 'LineWidth', 1.5);
% Formatting
axis([-5 5 -5 5]);
grid on;
pbaspect([1 1 1]);
pause(0.1);
forcount = forcount + 1;
end
% Final plot of the path of point D
figure;
plot(xD_path, yD_path, '--g', 'LineWidth', 1.5);
xlabel('x');
ylabel('y');
title('Path of Point D');
axis([-5 5 -5 5]);
grid on;
pbaspect([1 1 1]);
Please refer to the following documentation for more information on the 'plot' function: https://www.mathworks.com/help/matlab/ref/plot.html
I hope this helps!

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!