当前位置:文档之家› matlab仿真大作业

matlab仿真大作业

《MATLAB与系统仿真》大作业系别:电子信息工程系班级;09机械1班姓名:潘家辉学号:200900606106指导教师:姚金邮箱:334044940@2012年7月4日第一题建立M文件,内容如下。

for m=100:999m1=fix(m/100);m2=rem(fix(m/10),10);m3=rem(m,10);if m==m1^3+m2^3+m3^3disp(m)endend运行后结果如下图所示第二题新建Model文件命名pan,建立仿真模型如下仿真模型使用模块的提取与参数设置如下图Integrator设置Saturation设置Transfer Fcn设置Transport Delay设置运行结果如下建立pan2文件模型如图Transfer Fcn1设置如下其他元件设置和前面一样建立pan3文件如下Subsystem如下图其他设置与前面一样建立pan4文件如下Subsystem设置如下其他设置与前面一样第三题建立两个M文件内容如下Panjiahui.m文件function y=panjiahui(x)y=2*pi/4*(x(2)^2*x(3)^2-x(5)^2)*x(1)+pi/4*(100*x(2)^2*x(3)^2-x(6)^2)... *x(1)-pi/4*((x(2)*x(3)-10*x(3))^2-1.6*x(6))*0.8*x(1)-0.4*x(1)*pi/4*(x(2 )...*x(3)-10*x(3)-1.6*x(6))+pi/4*x(6)^2*(x(4)+x(8))+pi/4*(x(6)^2-x(7)^2)... *(x(4)+x(8));Panjiahuib.m文件function [c1,c2]=panjiahui(x)g(1)=17-x(2);g(2)=x(1)/x(3)-25;g(3)=8-x(1)/x(3);g(4)=2-x(3);g(5)=x(1)-x(4)+0.25*x(6)+20;g(6)=20-x(5);g(7)=x(5)-35;g(8)=150-x(6);g(9)=x(6)-200;g(10)=443168.7749/x(2)/x(3)/sqrt(x(1))-690;g(11)=3579919.2/x(1)/x(2)/x(3)^2-427.4;g(12)=3135363.4/x(1)/x(2)/x(3)^2-427.4;g(13)=sqrt((305*1000*x(4)/(2*x(2)*x(3)*cos(20)))^2/4+(0.58*305*1000)^2) .../(0.1*x(5)^3)-70;g(14)=sqrt((305*1000*x(4)/(2*x(2)*x(3)*cos(20)))^2/4+(0.58*10*305*1000) ...^2)/(0.1*x(6)^3-x(7)^3)-70;g(15)=sqrt((305*1000*x(4)/(2*x(2)*x(3)*cos(20)))^2/4+(0.58*305*1000)^2) .../(0.1*x(6)^3)-70;g(16)=3.233624*0.0000001*30000/48*x(8)^2*(x(4)+x(8))/(x(6)^4-x(7)^4)-0. 005;g(17)=1-x(4)/x(7);g(18)=50-x(2)*x(3);g(19)=x(7)/x(6)-0.6;g(20)=1-x(8)/650;c1=[g(2);g(3);g(10);g(11);g(12);g(13);g(14);g(15);g(16);g(17);g(18);g(1 9)];c2=0;输入一下内容x0=[48 17 2 105 20 150 105 650];A=[0 -1 0 0 0 0 0 0;0 0 -1 0 0 0 0 0;1 0 0 -4 0 0.25 0 0;...0 0 0 0 -1 0 0 0;0 0 0 0 1 0 0 0;0 0 0 0 0 -1 0 0;0 0 0 0 0 1 0 0;...0 0 0 0 0 0 0 -1/650];b=[-17 -2 -20 -20 35 -150 200 -1];[x,f]=fmincon(@panjiahui,x0,A,b,[],[],[],[],@panjiahuib)运行结果如下第四题模型的建立建立的模型由一个机架模块、一个旋转铰模块、一个刚体模块、一个环境模块、一个铰传感器模块、一个示波器模块、一个混路器模块以及一个XY相图模块构成。

从它们各自的库中添加至panjiahui4.mbl的模型窗口中Ground模块Revolute设置Body模块设置Joint Sensor模块设置机械环境模块:打开机械环境模块参数对话框,保持默认值如图示:打开XY相图模块参数设置对话框,设定参数如图。

运行结果左图为系统相图,右图为单摆的角位移和角速度时程曲线第五题建立如下的模型文件UA,UB,UC设置如下Ua 设置phase为0 Ub设置phase为-120 Uc 设置phase为120 Universal Bridge设置Diode设置Selector设置电感L设置如下Saturation设置如下Transfer Fcn设置如下运行后结果如下第六题建立GUI文件建立17个静态文本框,4个动态文本框,一个滚动条和两个按钮M文件修改如下function pushbutton2_Callback(hObject, eventdata, handles)Close;function pushbutton1_Callback(hObject, eventdata, handles)A=get(findobj(gcf,'Tag','Edit_a'),'string');la=str2num(A);B=get(findobj(gcf,'Tag','Edit_b'),'string');lb=str2num(B);C=get(findobj(gcf,'Tag','Edit_c'),'string');lc=str2num(C);D=get(findobj(gcf,'Tag','Edit_d'),'string');ld=str2num(D);lmax=max([la,lb,lc,ld]);lmin=min([la,lb,lc,ld]);lo=2*(lmax+lmin)-(la+lb+lc+ld);ll=2*lmax-(la+lb+lc+ld);w0=get(findobj(gcf,'Tag','Text_w'),'string');w=str2num(w0);if (lo>0)&(ll<0)set(findobj(gcf,'Tag','Type'),'String',{'˫ҡ¸Ë»ú¹¹!²»ÔÚÄ£ÄⷶΧ!'},'Fo regroundColor',[1,0,0]);elseif(lo<=0)&(ll<0)switch lmincase ldset(findobj(gcf,'Tag','Type'),'String',{'Ë«Çú±ú»ú¹¹!²»ÔÚÄ£ÄⷶΧ!'},'ForegroundColor',[1,0,0]);crankrocker.mcase laset(findobj(gcf,'Tag','Type'),'String',{'Çú±úÒ¡¸Ë»ú¹¹!'});crankrocker(la,lb,lc,ld,w);case lcset(findobj(gcf,'Tag','Type'),'String',{'Ë«Çú±ú»ú¹¹!µ«Ö÷¶¯¼þΪҡ¸Ë£¬²»ÔÚÄ£ÄⷶΧ!'},'ForegroundColor',[1,0,0],'Position',[0.75,0.26,0.25,0.1]) ;otherwise lbset(findobj(gcf,'Tag','Type'),'String',{'˫ҡ¸Ë»ú¹¹!²»ÔÚÄ£ÄⷶΧ!'},'Fo regroundColor',[1,0,0]);endelseif ll>=0set(findobj(gcf,'Tag','Type'),'String',{'²»Âú×ã¸Ë³¤Ìõ¼þ!ÎÞ·¨½øÐÐÄ£Äâ!!' },'ForegroundColor',[1,0,0]);end;编写crankrocker.m文件内容如下function crankrocker(la,lb,lc,ld,w)L=[la,lb,lc,ld];sum=la+lb+lc+ld;la=la/sum*200;lb=lb/sum*200;lc=lc/sum*200;ld=ld/sum*200;L=[la,lb,lc,ld];a2=line([0,-3],[0,-6],'color','k','linestyle','-','linewidth',2);a3=line([0,3],[0,-6],'color','k','linestyle','-','linewidth',2);a4=line([-6,+6],[-6,-6],'color','k','linestyle','-','linewidth',2);a41=line([-1,-4],[-6,-9],'color','k','linestyle','-','linewidth',1);a42=line([-4,-7],[-6,-9],'color','k','linestyle','-','linewidth',1);a43=line([2,-1],[-6,-9],'color','k','linestyle','-','linewidth',1);a44=line([5,2],[-6,-9],'color','k','linestyle','-','linewidth',1);d2=line([ld,-3+ld],[0,-6],'color','k','linestyle','-','linewidth',2);d3=line([ld,3+ld],[0,-6],'color','k','linestyle','-','linewidth',2);d4=line([-6+ld,6+ld],[-6,-6],'color','k','linestyle','-','linewidth',2) ;d41=line([-1+ld,-4+ld],[-6,-9],'color','k','linestyle','-','linewidth', 1);d42=line([-4+ld,-7+ld],[-6,-9],'color','k','linestyle','-','linewidth', 1);d43=line([2+ld,-1+ld],[-6,-9],'color','k','linestyle','-','linewidth',1 );d44=line([5+ld,2+ld],[-6,-9],'color','k','linestyle','-','linewidth',1) ;theta_1=acos(((la+lb)*(la+lb)+ld*ld-lc*lc)/(2*(la+lb)*ld));theta_2=acos(((-la+lb)*(-la+lb)+ld*ld-lc*lc)/(2*(-la+lb)*ld));theta_0=theta_2-theta_1;theta0=theta_0/pi*180;set(findobj(gcf,'Tag','theta_0'),'String',num2str(theta0));K=(pi+theta_0)/(pi-theta_0);set(findobj(gcf,'Tag','K'),'String',num2str(K));gama_1=acos((lb*lb+lc*lc-(ld-la)*(ld-la))/(2*lb*lc));gama_2=acos((lb*lb+lc*lc-(ld+la)*(ld+la))/(2*lb*lc));if gama_2>=pi/2gama_2=pi-gama_2;endgama_0=min(gama_1,gama_2);gama0=gama_0/pi*180;set(findobj(gcf,'Tag','gama_0'),'String',num2str(gama0));ad=line([0,ld],[0,0],'color','k','marker','o','markersize',5,'linewidth ',1,'linestyle','-.');ax0=0;ay0=0;Va=complex(ax0,ay0);dx0=ld;dy0=0;Vd=complex(dx0,dy0);mm=90;for i=1:1:mmtheta(i)=2*i*pi/mm;bx0=la*cos(theta(i));by0=la*sin(theta(i));Vb(i)=complex(bx0,by0);theta_a(i)=angle(Vb(i));Vbd(i)=Vb(i)-Vd;theta_bda(i)=pi-angle(Vbd(i));lbd(i)=abs(Vbd(i));theta_cdb(i)=acos((lc*lc+lbd(i)*lbd(i)-lb*lb)/(2*lc*lbd(i)));theta_d(i)=theta_bda(i)+theta_cdb(i);cx0=ld-lc*cos(theta_d(i));cy0=lc*sin(theta_d(i));Vc(i)=complex(cx0,cy0);Vbc(i)=Vc(i)-Vb(i);theta_2(i)=angle(Vb(i));theta_3(i)=angle(Vbc(i));theta_4(i)=pi-theta_d(i);w2=w;R1=[-lb*sin(theta_3(i)) lc*sin(theta_4(i));lb*cos(theta_3(i))-lc*sin(theta_4(i))];R2=[w2*la*sin(theta_2(i)); w2*la*sin(theta_2(i))];Result_w=inv(R1)*R2;w3(i)=Result_w(1,1);w4(i)=Result_w(2,1);Ra=[-lb*sin(theta_3(i)) lc*sin(theta_4(i));-lb*cos(theta_3(i))-lc*sin(theta_4(i))];Ras=[w2*w2*la*cos(theta_2(i))+w3(i)*w3(i)*lb*cos(theta_3(i))-w4(i)*w4(i )*lc*cos(theta_4(i));w2*w2*la*sin(theta_2(i))+w3(i)*w3(i)*lb*sin(theta_ 3(i))-w4(i)*w4(i)*lc*sin(theta_4(i))];Result_a=inv(Ra)*Ras;a3(i)=Result_a(1,1);a4(i)=Result_a(2,1);endab=line([real(Va),real(Vb(i))],[imag(Va),imag(Vb(i))],'color','k','mark er','o','markersize',5,'linewidth',2,'linestyle','-');cd=line([real(Vd),real(Vc(i))],[imag(Vd),imag(Vc(i))],'color','k','mark er','o','markersize',5,'linewidth',2,'linestyle','-');bc=line([real(Vc(i)),real(Vb(i))],[imag(Vc(i)),imag(Vb(i))],'color','k' ,'marker','o','markersize',5,'linewidth',2,'linestyle','-');j=1;m=0;while m<100set(ab,'xdata',[real(Va),real(Vb(j))],'ydata',[imag(Va),imag(Vb(j))]); set(cd,'xdata',[real(Vd),real(Vc(j))],'ydata',[imag(Vd),imag(Vc(j))]); set(bc,'xdata',[real(Vc(j)),real(Vb(j))],'ydata',[imag(Vc(j)),imag(Vb(j ))]);set(findobj(gcf,'Tag','Text_Wc'),'String',num2str(w4(j)));set(findobj(gcf,'Tag','Text_Ac'),'String',num2str(a4(j)));drawnowj=j+1;if j>mmj=1;m=m+1;endend把3个文件放在同一个文件夹下,指派路径要改成所放文件的文件夹路径,更改如下,例如我所放文件在桌面\6下必须改成下面的路径点击开始模拟按钮结果如下第七题建立UGI文件如下建立23个按钮,1个静态文本框,和1动态文本框M内容文件如下function varargout = panjiahui7(varargin)global gloval1;global gloval2;global locaval;handles.flagnum=0;format longgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...'gui_OutputFcn', @jisuanqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});endfunction jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) handles.output = hObject;guidata(hObject, handles);function varargout = jisuanqi_OutputFcn(hObject, eventdata, handles) varargout{1} = handles.output;function edit1_Callback(hObject, eventdata, handles)function edit1_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;endfunction pushbutton1_Callback(hObject, eventdata, handles)textString = get(handles.edit1,'String');if(strcmp(textString,'0.')==1)set(handles.edit1,'String','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);if((l-1)==0)set(handles.edit1,'String','0.')elseset(handles.edit1,'String',textString)endendglobal gloval1;global gloval2;global locaval;locaval= textString; %••__•¨¨backspace____••_••____________¡§•gloval2=locaval; %guidata(hObject,handles)function pushbutton2_Callback(hObject, eventdata, handles)global locavallocaval=' ';set(handles.edit1,'String','0.');guidata(hObject,handles)function pushbutton3_Callback(hObject, eventdata, handles)global locavalglobal gloval1global gloval2locaval=' ';gloval1=' ';gloval2=' ';set(handles.edit1,'String','0.');guidata(hObject,handles)function pushbutton4_Callback(hObject, eventdata, handles)global locaval;global gloval1;a = get(handles.pushbutton4,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton5_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton5,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton6_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton6,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton7_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton7,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton8_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton8,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton9_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton9,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton10_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton10,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton11_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton11,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton12_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton12,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton13_Callback(hObject, eventdata, handles) global locaval;global gloval1;a = get(handles.pushbutton13,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton14_Callback(hObject, eventdata, handles) global locaval;textString = get(handles.edit1,'String');textString=str2num(textString);textString=0-textString;textString=num2str(textString);set(handles.edit1,'String',textString);global gloval2gloval2=textString;guidata(hObject, handles);function pushbutton15_Callback(hObject, eventdata, handles) global locavala = get(handles.pushbutton15,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=a;guidata(hObject, handles);function pushbutton15_ButtonDownFcn(hObject, eventdata, handles) global locavala = get(handles.pushbutton15,'String');locaval=strcat(locaval,a);set(handles.edit1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);function pushbutton16_Callback(hObject, eventdata, handles)a = get(handles.pushbutton16,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);% global flagnumglobal gloval1global locavallocaval=' ';% flagnum=1handles.flagnum=1;gloval1=b;guidata(hObject, handles);function pushbutton17_Callback(hObject, eventdata, handles)a = get(handles.pushbutton17,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global gloval1global locavallocaval=' ';handles.flagnum=2;gloval1=b;guidata(hObject, handles);function pushbutton18_Callback(hObject, eventdata, handles)a = get(handles.pushbutton18,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global gloval1global locavalhandles.flagnum=3;gloval1=b;guidata(hObject, handles);function pushbutton19_Callback(hObject, eventdata, handles)a = get(handles.pushbutton19,'String');b = get(handles.edit1,'String');set(handles.edit1,'String',a);global gloval1global locavallocaval=' ';handles.flagnum=4;gloval1=b;guidata(hObject, handles);function pushbutton20_Callback(hObject, eventdata, handles) global locavaltextString = get(handles.edit1,'String');textString=str2num(textString);textString=sqrt(textString);textString=num2str(textString);set(handles.edit1,'String',textString);locaval=' ';handles.flagnum=0;guidata(hObject,handles)function pushbutton21_Callback(hObject, eventdata, handles) global locavaltextString = get(handles.edit1,'String');textString=str2num(textString);textString=textString*textString;textString=num2str(textString);set(handles.edit1,'String',textString);locaval=' ';handles.flagnum=0;guidata(hObject,handles)function pushbutton22_Callback(hObject, eventdata, handles) global locavaltextString = get(handles.edit1,'String');textString=str2num(textString);textString=1/textString;textString=num2str(textString);set(handles.edit1,'String',textString);locaval=' ';handles.flagnum=0;guidata(hObject,handles)function pushbutton23_Callback(hObject, eventdata, handles) global gloval1global gloval2global locavallocaval=' ';%flagnumif(strcmp(gloval1,' ')==1)return;elsegloval1=str2num(gloval1);gloval2=str2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=gloval1-gloval2;case4=gloval1+gloval2;temp1=case1;%••__•¨¨___________•¨¬temp2=case2;temp3=case3;temp4=case4;case1=num2str(case1);case2=num2str(case2);case3=num2str(case3);case4=num2str(case4);switch (handles.flagnum);case 1set(handles.edit1,'String',case1);gloval1=num2str(temp1);gloval2=num2str(gloval2);case 2set(handles.edit1,'String',case2);gloval1=num2str(temp2);gloval2=num2str(gloval2);case 3set(handles.edit1,'String',case3);gloval1=num2str(temp3);gloval2=num2str(gloval2);case 4set(handles.edit1,'String',case4);gloval1=num2str(temp4);gloval2=num2str(gloval2);endendguidata(hObject,handles)function figure1_KeyPressFcn(hObject, eventdata, handles) if get(gcf,'CurrentCharacter')==13 %enterpushbutton23_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==48 %numpad0pushbutton13_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==49 %numpad1pushbutton10_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==50 %numpad2pushbutton11_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==51 %numpad3pushbutton12_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==52 %numpad4pushbutton7_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==53 %numpad5pushbutton8_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==54 %numpad6pushbutton9_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==55 %numpad7pushbutton4_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==56 %numpad8pushbutton5_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==57 %numpad9pushbutton6_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==46 %.pushbutton15_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==47 %/pushbutton16_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==42 %*pushbutton17_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==45 %-pushbutton18_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==43 %+pushbutton19_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==8 %backspacepushbutton1_Callback(hObject, eventdata, handles);% guidata(hObject,handles);elseif get(gcf,'CurrentCharacter')==27 %deletepushbutton3_Callback(hObject, eventdata, handles); % guidata(hObject,handles);end%=============================================运行后结果图下。

相关主题