机械原理第八版课后习题8-16
main.m
%% 公共汽车门开闭仿真
clear;clc;
%% 变量定义
x_A = 0; y_A = 0;
L_DE = sqrt(400^2-50^2);
L_AB = 420;
A = [x_A,y_A];
a = 0:0.1:90/57.3;
E_locus = zeros(length(a),2);
D_locus = zeros(length(a),2);
figure(1)
% plot([-800,850],[-110,-110],'r-')
pit_num = 1;
f_n = 'textimage.gif';
%% 更新点坐标'
L_BC = 400;
% syms x y k b x1 y1 x2 y2
z = 1;
for alpha = a
B(1) = L_AB*cos(alpha); B(2) = -L_AB*sin(alpha);
syms x y
[x,y] = solve(y+110,(x-B(1))^2+(y-B(2))^2-L_BC^2);
answer = vpa([x,y],9);
answer = double(answer(2,:));
C(1) = answer(1); C(2) = answer(2);
syms k b
[b,k] = solve(abs(B(2)-k*B(1)-b)==50*sqrt(k^2+1),abs(C(2)-k*C(1)-b)==160*sqrt(k^2+1));
s = double(k);
b0 = double(b);
syms x1 y1
[x1,y1] = solve(y1-s*x1-b0,y1-B(2)+(1/s)*x1-(1/s)*B(1));
x1 = double(x1);
y1 = double(y1);
D(1) = x1;D(2) = y1;
D_locus(z,1) = x1; D_locus(z,2) = y1;
syms x2 y2
[x2,y2] = solve(y2-s*x2-b0,y2-C(2)+(1/s)*x2-(1/s)*C(1));
x2 = double(x2);
y2 = double(y2);
E(1) = x2;E(2) = y2;
E_locus(z,1) = x2; E_locus(z,2) = y2;
hold off
pause(0.01)
drawlineinthefigure(A,B)
drawlineinthefigure(B,D)
drawlineinthefigure(D,E)
drawlineinthefigure(E,C)
z = z+1;
F = getframe(gcf);
I = frame2im(F);
[I,map] = rgb2ind(I,256);
if pit_num == 1
imwrite(I,map,f_n,'gif','Loopcount',Inf,'Delaytime',0.2);
else
imwrite(I,map,f_n,'gif','Writemode','append','Delaytime',0.2);
end
pit_num = pit_num +1;
end
% figure(2)
plot(E_locus(:,1),E_locus(:,2),'linewidth',2)
title('E点的运动轨迹')
% figure(3)
hold on
plot(D_locus(:,1),D_locus(:,2),'linewidth',2)
title('D点的运动轨迹')
drawlineinthefigure.m
function [] = drawlineinthefigure(P1,P2)
x(1) = P1(1);
x(2) = P2(1);
y(1) = P1(2);
y(2) = P2(2);
plot(x,y,'Linewidth',2)
% plot([-200,850],[-110,-110],'r--')
axis([-200 850 -500 200])
hold on
使用matlab绘制的gif图像