In: Mechanical Engineering
Write Matlab code and run them for the following:
a) Motion of a point in a circle
b) Motion of a line in a circle
c) Motion of slider crank mechanism
a)
radius=[10 15 20 30]
velocity=[5 2.5 15 20]
colors=['y','m','c','r'] % This is to just make the points
different colors
p=zeros(4,1); %here we will store the handles to delete the
point
hold on
for i=1:4 %Loop to create multiple circles
th=0:pi/50:2*pi;
xunit=radius(i)*cos(th);
yunit=radius(i)*sin(th);
h=plot(xunit,yunit);
p(i)=plot(xunit(1),yunit(1),'o','MarkerFaceColor',colors(i),'Color',colors(i));
%creates a point on each graph
end
time=[0:0.001:100]; %time vector in seconds
for t=1:length(time)
for i=1:4 %Loop to create multiple circles
delete(p(i)); %delete the old point
%computes the new angle for each point as velocity*time
xunit=radius(i)*cos(velocity(i)*time(t));
yunit=radius(i)*sin(velocity(i)*time(t));
%creates a point on each graph
p(i)=plot(xunit(1),yunit(1),'o','MarkerFaceColor',colors(i),'Color',colors(i));
end
pause(0.01); %wait 0.01 seconds so the plot is displayed
end
hold off
b)
pC = [4; 10]; % initial position of A
vC = [2.3100; -0.5500]; % initial velocity of A
dt = 0.1; % time step for animation
clf; % Clear the figure
axis(50*[0 1 0 1],'square'); % Set axis limits
grid on;
hold on; % Keep plot from erasing
for t=0:dt:15 % t is time variable
plot(pC(1),pC(2),'ro') % Plot circle
pC = pC + vC*dt; % update position
pause(dt) % pause for animation
end
hold off;
title('Motion animation');
xlabel('x (m)');
ylabel('y (m)');
C)
close all
clear all
% Analysis of a slider-crank mechanism
%Section 1: Input data and prepare problem
r1=0; %ground
r2=1;%crank shaft
r3=2;%connecting rod
h=0.5;%offset
%fixed link angle is zero
theta10=0;
% Initialize joints
joint(1).type = 'r';% Revolute
joint(1).memb =[1 2];% Between member 1 and 2
joint(2).type = 'r'; % Revolute
joint(2).memb = [2 3]; % Between member 2 and 3
joint(3).type = 'r'; % Revolute
joint(3).memb =[3 4]; % Between member 3 and 4
joint(4).type = 'p'; % Prismatic
joint(4).memb = [4 1]; % Between member 4 and 1
%Initial Guess
theta20=45;
theta30=-10;
theta40=0;
R1x0=0;
R1y0=0;
R2x0=(r2/2)*cosd(theta20);
R2y0=(r2/2)*sind(theta20);
R3x0=(r2)*cosd(theta20)+(r3/2)*cosd(theta30);
R3y0=(r2)*sind(theta20)+(r3/2)*sind(theta30);
R4x0=(r2)*cosd(theta20)+(r3)*cosd(theta30);
R4y0=(r2)*sind(theta20)+(r3/2)*sind(theta30);
%input parameters
t(1)=0;
dt=0.1;%seconds
omega2=1; % input velocity (rad/s)
imax=10; %number of time steps
converge=0;
epsilon1=0.001; %dtheta accuracy
epsilon2=0.001;%C accuracy
%Define the local position vectors
u1O=[0;0];
u2O=[-r2/2;0];
u2A=[r2/2;0];
u3A=[-r3/2;0];
u3B=[r3/2;0];
u4B=[0;0];
u4C=[0;0];
u1C=[0;h];
%Initialize variables
Rx(1:4,1:imax)=0;
Ry(1:4,1:imax)=0;
theta(1:4,1:imax)=0;
Rxd(1:4,1:imax)=0;
Ryd(1:4,1:imax)=0;
thetad(1:4,1:imax)=0;
Rxdd(1:4,1:imax)=0;
Rydd(1:4,1:imax)=0;
thetadd(1:4,1:imax)=0;
%Section 2: Position Analysis
i=1;
while i<=imax
t(i)=(i)*dt;
%input motion
theta(2,i)=theta20+omega2*t(i)*(180/pi);
%adjust initial conditions based on the step #
if i==1
Rx(2,i)=R2x0;
Ry(2,i)=R2y0;
theta(2,i)=theta20;
Rx(3,i)=R3x0;
Ry(3,i)=R3y0;
theta(3,i)=theta30;
Rx(4,i)=R4x0;
Ry(4,i)=R4y0;
theta(4,i)=theta40;
else
Rx(2,i)=Rx(2,i-1);
Ry(2,i)=Ry(2,i-1);
theta(2,i)=theta(2,i-1);
Rx(3,i)=Rx(3,i-1);
Ry(3,i)=Ry(3,i-1);
theta(3,i)=theta(3,i-1);
Rx(4,i)=Rx(i-1);
Ry(4,i)=Ry(i-1);
theta(4,i)=theta(4,i-1);
end;