Matcodes
Matcodes
clc;clear;close all
X = [4 12 8 10 40 120];
% X = [180 100 185 220 55 0];
% X = [r1 r2 r3 r4 Cx Cy ];
% r1: Crank (make sure its always the smallest, also r3+r4>=r1+r2)
% r2: Coupler
% r3: Lever (Rocker)
% r4: Frame
% Cu: x coordinate for coupler point wrt crank-coupler point
% Cv: y coordinate for coupler point wrt crank-coupler point
%%
if Rotate == 1
d = sqrt(R1^2 + R4^2 - 2*R1*R4*cos(th1));
th5 = acos((R3^2+d.^2-R2^2)./(2*R3*d));% angle between rocker and diagonal link
(d)
th5 = [th5(1:end/2) -th5(end/2+1:end)];
end
Ax = R1*cos(th1);% x coordinate for the crank-coupler point
Ay = R1*sin(th1);% y coordinate for the crank-coupler point
a = R4 - R1*cos(th1);% horizontal distance between rocker-frame point and
% projection of crank-coupler point
b = Ay;% vertical projection of crank-coupler point
th6 = atan2(b,a);% angle between frame and diagonal link (d)
th4 = pi - th5 - th6;% angle the rocker makes with horizon
Bx = R3*cos(th4) + R4;% horizontal distance between frame-crank point and
% projection of coupler-rocker point
By = R3*sin(th4);% vertical projection of coupler-rocker point
th2 = atan2((By-Ay),(Bx-Ax));% angle the coupler makes with the horizon
Cx = Ax + Cu*cos(th2) - Cv*sin(th2);% horizontal projection of coupler
% point wrt coupler
Cy = Ay + Cu*sin(th2) + Cv*cos(th2);% vertical projection of coupler
% point wrt coupler
% calculate display (figure) limits
xmin = 1.2*min([min(Cx) -R1 -R3]);
xmax = 1.2*max([max(Cx) R4+max([R3 max(R3*cos(th4))])]);
ymin = 1.2*min([min(Cy) -R1 -R3]);
ymax = 1.2*max([max(Cy) max([R1 R3 R3+Cv])]);
%%
increments = length(th1);
for i=1:increments
link1x(i,:) = r1*cos(th1(i));
link1y(i,:) = r1*sin(th1(i));
link2x(i,:) = linspace(Ax(i),Bx(i),dig);
link2y(i,:) = linspace(Ay(i),By(i),dig);
link3x(i,:) = R4 + r3*cos(th4(i));
link3y(i,:) = r3*sin(th4(i));
Couplx1(i,:) = linspace(Ax(i),Cx(i),dig);
Couply1(i,:) = linspace(Ay(i),Cy(i),dig);
Couplx2(i,:) = linspace(Cx(i),Bx(i),dig);
Couply2(i,:) = linspace(Cy(i),By(i),dig);
end
for k=1:cycles
for i = 1:increments
plot(link1x(i,:),link1y(i,:),'b',link2x(i,:),link2y(i,:),'r',...
link3x(i,:),link3y(i,:),'k',Couplx1(i,:),Couply1(i,:),'r',...
Couplx2(i,:),Couply2(i,:),'r')
hold on
plot([link2x(i,:) ;Couplx1(i,:)],[link2y(i,:);
Couply1(i,:)],'g','linewidth',2)
plot([link2x(i,:) ;Couplx2(i,:)],[link2y(i,:);
Couply2(i,:)],'g','linewidth',2)
plot(0,0,'sk',R4,0,'sk','MarkerSize',12)
plot(0,0,'ok',R4,0,'ok')
plot(Couplx1(i,end),Couply1(i,end),'ok','MarkerSize',6,...
'MarkerFaceColor','g')
axis([xmin xmax ymin ymax])
if Rotate == 1 & i<=increments/2
plot(Couplx1(1:i,end),Couply1(1:i,end),'--g','linewidth',2)
elseif Rotate == 1
plot(Couplx1(1:increments/2,end),Couply1(1:increments/2,end),'--
g','linewidth',2)
plot(Couplx1(increments/2:i,end),Couply1(increments/2:i,end),'--
r','linewidth',2)
else
plot(Couplx1(1:i,end),Couply1(1:i,end),'--g','linewidth',2)
end
clc
title(['\bf',TITLE])
fprintf('Th1 = %5.2f, th5 = %5.2f, D = %7.2f\n',th1(i),th5(i),d(i))
YY = input('Hit Enter ');
hold off
end
end
if Rotate==1
hold on
plot(Couplx1([1 end/2],end),Couply1([1 end/2],end),'hr','MarkerSize',10)
t = 0:0.05:10;
ang_speed = 2;
theta = ang_speed*t;
P1 = [0;0];
P4 = D*[1;0];
P2 = A*[cos(theta); sin(theta)];
E = sqrt(A^2 + D^2 - 2*A*D*cos(theta));
alfa = asin(A*sin(theta)./E);
beta = acos((E.^2 + C^2 - B^2)./(2*E*C));
P3 = [D - C*cos(alfa+beta); C*sin(alfa+beta)];
P3_x = P3(1,:);
P3_y = P3(2,:);
P3_vx = diff(P3_x)./diff(t);
P3_vy = diff(P3_y)./diff(t);
for i=1:length(t);
ani = subplot(2,1,1);
P1_circle = viscircles(P1',0.05);
P2_circle = viscircles(P2(:,i)',0.05);
P3_circle = viscircles(P3(:,i)',0.05);
P4_circle = viscircles(P4',0.05);
str1 = 'P3';
str2 = ['Time elapsed: ' num2str(t(i)) ' s'];
P3_text = text(P3(1,i),P3(2,i)+0.4,str1);
Time = text(-2,6,str2);
pause(0.005);
if i<length(t)
delete(P1_circle);
delete(P2_circle);
delete(P3_circle);
delete(P4_circle);
delete(A_bar);
delete(B_bar);
delete(C_bar);
delete(P3_text);
delete(Time);
vel = subplot(2,1,2);
plot(vel,t(1:i),P3_v(1:i));
set(vel,'XLim',[0 10],'YLim',[0 10]);
xlabel(vel, 'Time (s)');
ylabel(vel, 'Amplitude (m/s');
title(vel,'Speed of P3');
grid on;
end
end