set(handles.edit6,'visible','on')
set(handles.edit6,'string','杆长符合条件')
A=l4-l1*cos(th1);
B=-l1*sin(th1);
C=(A.*A+B.*B+l3.^2-l2.^2)/(2*l3);
th3=2*atan((B+sqrt(A.*A+B.*B-C.*C))./(A-C));%角度分析,杆3转过的角度
th2=atan(B+l2*sin(th3)./(A+cos(th3)));%杆2转过的角度
w3=w1*l1*sin(th1-th2)./(l3*sin(th3-th2));%速度分析,杆3角速度
w2=-w1*l1*sin(th1-th3)./(l2*sin(th2-th3));%杆2角速度
a3=(l2*w2.^2+l1*w1.^2.*cos(th1-th2)-l3*w3.^2.*cos(th3-th2))./(l3*sin(th3-th2));%加速度分析,杆3角加速度
a2=(l3*w3.^2-l1*w1.^2.*cos(th1-th3)-l2*w2.^2.*cos(th2-th3))./(l2*sin(th2-th3));%杆2角加速度
plot(handles.axes2,th1,th2);
title(handles.axes2,'杆2角位移变化曲线')
xlabel(handles.axes2,'th1');
ylabel(handles.axes2,'角位移rad');
axes(handles.axes2);
gridon;
plot(handles.axes4,th1,th3,'b')
title(handles.axes4,'杆3角位移变化曲线')
xlabel(handles.axes4,'th1');
ylabel(handles.axes4,'角位移rad');
axes(handles.axes4);
gridon;
plot(handles.axes6,th1,w2,'m')
title(handles.axes6,'杆3角速度变化曲线')
xlabel(handles.axes6,'th1');
ylabel(handles.axes6,'角速度rad/s');
axes(handles.axes6);
gridon;
plot(handles.axes7,th1,w3,'k')
title(handles.axes7,'杆2角速度变化曲线')
xlabel(handles.axes7,'th1');
ylabel(handles.axes7,'角速度rad/s');
axes(handles.axes7);
gridon;
plot(handles.axes8,th1,a2,'b')
title(handles.axes8,'杆2角加速度变化曲线')
xlabel(handles.axes8,'th1');
ylabel(handles.axes8,'角加速度rad/s^2');
axes(handles.axes8);
gridon;
plot(handles.axes9,th1,a3,'b')
title(handles.axes9,'杆3角加速度变化曲线')
xlabel(handles.axes9,'th1');
ylabel(handles.axes9,'角加速度rad/s^2');
axes(handles.axes9);
gridon;
else
set(handles.edit6,'visible','on')
set(handles.edit6,'string','不符合杆长条件或整转副条件,请重新输入')
message='不符合杆长条件或整转副条件,请重新输入';
icon='error';
h=msgbox(message,'出错',icon)
y=wavread('ding.wav');
sound(y)
end
在这里面最容易出错的就是在数如表达式的时候,需要在不同的计算函数之间加点如“:
th3=2*atan((B+sqrt(A.*A+B.*B-C.*C))./(A-C));%角度分析,杆3转过的角度,
不仅在相乘之间加,而且在相除时也不要加入,不然会出错。
符合杆长条件,效果如下:
不符合杆长条件时(20+40>30+25):
2.倒杆机构
这些都是一些简单的程序。
程序段:
l1=str2num(get(handles.edit1,'string'));
l2=str2num(get(handles.edit2,'string'));
w1=str2num(get(handles.edit5,'string'));
th1=0:
0.1:
2*pi;
th2=asin(-l1*sin(th1)/l2);
%位置分析
th2=asin(-l1*sin(th1)./l2);%杆2的角位移
xc=l1*cos(th1)+l2*cos(th2);%滑块的位置变化
%速度分析
vc=-l1*w1*sin(th1-th2)./cos(th2);%滑块c的速度
w2=-l1*w1*cos(th1)./(l2*cos(th2));%杆2的角速度
%加速度分析
ac=-(l1*w1.*w1.*cos(th1-th2)+l2*w2.*w2)./(l2*cos(th2));%滑块c的加速度
a2=(l1*w1.*w1.*sin(th1)+l2*w2.*w2.*sin(th2))./(l2*cos(th2));%杆2的角加速度
plot(handles.axes2,th1,th2);
title(handles.axes2,'杆2角位移变化曲线')
xlabel(handles.axes2,'th1');
ylabel(handles.axes2,'角位移rad');
axes(handles.axes2);
gridon;
plot(handles.axes4,th1,xc);
title(handles.axes4,'滑块的位置变化')
xlabel(handles.axes4,'th1');
ylabel(handles.axes4,'位移');
axes(handles.axes4);
gridon;
plot(handles.axes6,th1,vc);
title(handles.axes6,'滑块c的速度曲线')
xlabel(handles.axes6,'th1');
ylabel(handles.axes6,'速度');
axes(handles.axes6);
gridon;
plot(handles.axes7,th1,w2);
title(handles.axes7,'杆2的角速度曲线')
xlabel(handles.axes7,'th1');
ylabel(handles.axes7,'杆2角速度');
axes(handles.axes7);
gridon;
plot(handles.axes8,th1,ac);
title(handles.axes8,'滑块c的加速度')
xlabel(handles.axes8,'th1');
ylabel(handles.axes8,'加速度');
axes(handles.axes8);
gridon;
plot(handles.axes9,th1,a2);
title(handles.axes9,'杆2的角加速度')
xlabel(handles.axes9,'th1');
ylabel(handles.axes9,'加速度');
axes(handles.axes9);
gridon;
elseifz1==2
l1=str2num(get(handles.edit1,'string'));
l4=str2num(get(handles.edit4,'string'));
w1=str2num(get(handles.edit5,'string'));
th1=0:
0.1:
2*pi;
%位置分析
th3=atan((l1*sin(th1)+l4)./(l1*cos(th1)));%杆3的位置变化
s=l1*cos(th1)./cos(th3);%滑块2在杆3上的位移变化
%速度分析
vb2b3=-l1*w1*sin(th1-th3);%滑块2相对于杆3的速度
w3=l1*w1*cos(th1-th3)./s;%杆3的角速度
%加速度分析
ab2b3=s.*w3.^2-l1*cos(th1-th3)*w1.^2;%滑块2相对于杆3的加速度
a3=-(2*vb2b3.*w3+l1*w1.^2.*sin(th1-th3))./s;%杆3的角加速度
plot(handles.axes2,th1,th3);
title(handles.axes2,'杆3角位移变化曲线')
xlabel(handles.axes2,'th1');
ylabel(handles.axes2,'角位移rad');
axes(handles.axes2);
gridon;
plot(handles.axes4,th1,s);
title(handles.axes4,'滑块2在杆3上的位移变化')
xlabel(handles.axes4,'th1');
ylabel(handles.axes4,'位移');
axes(handles.axes4);
gridon;
plot(handles.axes6,th1,vb2b3);
title(handles.axes6,'滑块2相对于杆3的速度')
xlabel(handles.axes6,'th1');
ylabel(handles.axes6,'速度');
axes(handles.axes6);
gridon;
plot(handles.axes7,th1,w3);
title(handles.axes7,'杆3的角速度曲线')
xlabel(handles.axes7,'th1');
ylabel(handles.axes7,'杆3角速度');
axes(handles.axes7);
gridon;
plot(handles.axes8,th1,ab2b3);
title(handles.axes8,'滑块2相对于杆3的加速度')
xlabel(handles.axes8,'th1');
ylabel(handles.axes8,'加速度');
axes(handles.axes8);
gridon;
plot(handles.axes9,th1,a3);
title(handles.axes9,'杆2的角加速度')
xlabel(handles.axes9,'th1');
ylabel(handles.axes9,'加速度');
axes(handles.axes9);
gridon;
效果如图:
3.曲柄滑块机构
程序:
l1=str2num(get(handles.edit1,'string'));
l4=str2num(get(handles.edit4,'string'));
w1=str2num(get(handles.edit5,'string'));
th1=0:
0.1:
2*pi;
%位置分析
th3=atan((l1*sin(th1)+l4)./(l1*cos(th1)));%杆3的位置变化
s=l1*cos(th1)./cos(th3);%滑块2在杆3上的位移变化
%速度分析
vb2b3=-l1*w1*sin(th1-th3);%滑块2相对于杆3的速度
w3=l1*w1*cos(th1-th3)./s;%杆3的角速度
%加速度分析
ab2b3=s.*w3.^2-l1*cos(th1-th3)*w1.^2;%滑块2相对于杆3的加速度
a3=-(2*vb2b3.*w3+l1*w1.^2.*sin(th1-th3))./s;%杆3的角加速度
plot(handles.axes2,th1,th3);
title(handles.axes2,'杆3角位移变化曲线')
xlabel(handles.axes2,'th1');
ylabel(handles.axes2,'角位移rad');
axes(handles.axes2);
gridon;
plot(handles.axes4,th1,s);
title(handles.axes4,'滑块2在杆3上的位移变化')
xlabel(handles.axes4,'th1');
ylabel(handles.axes4,'位移');
axes(handles.axes4);
gridon;
plot(handles.axes6,th1,vb2b3);
title(handles.axes6,'滑块2相对于杆3的速度')
xlabel(handles.axes6,'th1');
ylabel(handles.axes6,'速度');
axes(handles.axes6);
gridon;
plot(handles.axes7,th1,w3);
title(handles.axes7,'杆3的角速度曲线')
xlabel(handles.axes7,'th1');
ylabel(handles.axes7,'杆3角速度');
axes(handles.axes7);
gridon;
plot(handles.axes8,th1,ab2b3);
title(handles.axes8,'滑块2相对于杆3的加速度')
xlabel(handles.axes8,'th1');
ylabel(handles.axes8,'加速度');
axes(handles.axes8);
gridon;
plot(handles.axes9,th1,a3);
title(handles.axes9,'杆2的角加速度')
xlabel(handles.axes9,'th1');
ylabel(handles.axes9,'加速度');
axes(handles.axes9);
gridon;
效果如下:
4.popupmenu设计
当选中不同的杆件时,见面发生对应变化,这里面界面button比较多,不同的杆件需要不同edittext的数值,所以容易混乱。
用if语句编制如下:
z1=get(handles.popupmenu1,'value');
ifz1==1
tu1=imread('jiaolian.png');
axes('position',[0.01,0.4,0.2,0.3]);%显示图像
image(tu1);
axisoff
set(handles.edit7,'string','铰链四杆机构运动分析')
set(handles.gan3,'visible','on')
set(handles.gan2,'visible','on')
set(handles.gan4,'visible','on')
set(handles.edit3,'visible','on')
set(handles.edit2,'visible','on')
set(handles.edit4,'visible','on')
set(handles.edit6,'visible','on')
elseifz1==2
tu3=imread('qubingyaogan.jpg');
axes('position',[0.01,0.4,0.2,0.3]);
image(tu3);
axisoff
set(handles.gan3,'visible','off')
set(handles.gan4,'visible','on')
set(handles.gan2,'visible','off')
set(handles.edit4,'visible','on')
set(handles.edit3,'visible','off')
set(handles.edit2,'visible','off')
set(handles.edit6,'visible','off')
set(handles.edit7,'string','倒杆机构运动分析')
else
z1==3
tu2=imread('qubinghuakaui.jpg');
axes('position',[0.01,0.4,0.2,0.3]);
image(tu2);
axisoff
set(handles.gan3,'visible','off')
set(handles.gan4,'visible','off')
set(handles.gan2,'visible','on')
set(handles.edit2,'visible','on')
set(handles.edit3,'visible','off')
set(handles.edit4,'visible','off')
set(handles.edit6,'visible','off')
set(handles.edit7,'string','曲柄滑块机构运动分析')
end
5.保存
生成了图像,对其保存:
[FileName,PathName,filterindex]=uiputfile({'*.jpg','JPEG(*.jpg)';...
'*.bmp','Bitmap(*.bmp)';...
'*.gif','GIF(*.gif)';...
'*.*','AllFiles(*.*)'},...
'SavePicture','Untitled');
iffilterindex
h=getframe(handles.axes9);
imwrite(h.cdata,[PathName,FileName]);
end
三.总结
做了这个大作业之后,感觉matlab这个软件功能十分强大,自己不过只用了它的冰山一角,而且大部分都是用的课本上简单的基础命令。
在调试程序的过程中,虽然改正一次次的错误很辛苦,但自己从这些错误中学到了这些命令的用法,也看到了matlab的灵活性、实用性。
其次,这也是对以前专业基础课的一个升华,学到了知识要综合利用,这样才能使知识得到交融,自己对它们也会有新的认识,从而”温故而知新“,获得意想不到的效果。
今后的学习生活中,遇到一些实际问题完全可以用matlab编一些程序来解决,尤其是涉及数值计算和图像处理。