matlab考试答案题选.docx
- 文档编号:29587810
- 上传时间:2023-07-24
- 格式:DOCX
- 页数:20
- 大小:78.98KB
matlab考试答案题选.docx
《matlab考试答案题选.docx》由会员分享,可在线阅读,更多相关《matlab考试答案题选.docx(20页珍藏版)》请在冰豆网上搜索。
matlab考试答案题选
编程1,亲密数对
clear;
clc;
j=0;
s=0;
fori=2:
100
p=i*(i+1)-1;
ifisprime(p)
j=j+1;
s=s+p;
end
end
fprintf('亲密数对有%d对,亲密素数的和为%d\n',j,s);
编程2,学生成绩
(1)
clear;
clc;
n=input('学生人数');
t=49+50*rand(n,5);
P=fix(t);%data
%[x,l]=max(P)%x-max,l-index
fori=1:
5
x(i)=P(1,i);
l(i)=1;
forj=1:
n
ifP(j,i)>x(i)
x(i)=P(j,i);
l(i)=j;
end
end
end
%[y,k]=min(P)%y-min,k-index
fori=1:
5
y(i)=P(1,i);
k(i)=1;
forj=1:
n
ifP(j,i) y(i)=P(j,i); k(i)=j; end end end %mu=mean(P)%mean fori=1: 5 mu(i)=0; forj=1: n mu(i)=mu(i)+P(j,i); end mu(i)=mu(i)/n; end %sig=std(P)%std forj=1: 5 sig(j)=0; fori=1: n sig(j)=sig(j)+(P(i,j)-mu(j))^2; end sig(j)=sqrt(sig(j)/(n-1)); end %s=sum(P,2) %[X,m]=max(s) %[Y,n]=min(s) fori=1: n s(i)=0; forj=1: 5 s(i)=s(i)+P(i,j); end end xx=s (1); m=1; fori=1: n ifs(i)>xx xx=s(i); m=i; end end yy=s (1); n=1; fori=1: n ifs(i) yy=s(i); n=i; end end fprintf('5门课总分最高分%f,对应学号%d,最低分%f,对应学号%d\n',xx,m,yy,n); %[zcj,xh]=sort(s,'descend') xh=1: n; fori=1: 5 forj=n: -1: i+1 ifs(j)>s(j-1) t=s(j); s(j)=s(j-1); s(j-1)=t; tt=xh(j); xh(j)=xh(j-1); xh(j-1)=tt; end end end zjc=s; 编程2,学生成绩 (2) clear; clc; t=46+50*rand(100,5); P=fix(t);%data [x,l]=max(P)%x-max,l-index [y,k]=min(P)%y-min,k-index mu=mean(P,1)%mean sig=std(P,1)%std s=sum(P,2) [X,m]=max(s) [Y,n]=min(s) [zcj,xh]=sort(s,'descend') 编程3,散点回归拟合 clearall; clc; %散点图 x1=[2,3,4,5,7,8,10,11,14,15,15,18,19]; y=[106.42,109.20,109.58,109.50,110.00,109.93,110.49,110.59,110.60,111.90,110.76,111.00,111.20]; scatter(x1,y,'*'); %线性拟合y=b0+b1*x x=[ones(13,1),x1']; [bbintrrintstats]=regress(y',x); y2=b (1)+b (2)*x1; holdon plot(x1,y2,'g') %二次曲线拟合y=b0+b1*x+b2*x^2 x=[ones(13,1),x1',x1'.^2]; [bbintrrintstats]=regress(y',x); y3=b (1)+b (2)*x1+b(3)*x1.^2 holdon plot(x1,y3,'m') %双曲线拟合y=b0+b1/x x=[ones(13,1),1./x1']; [bbintrrintstats]=regress(y',x); y4=b (1)+b (2)./x1; holdon plot(x1,y4,'r') %根据拟合效果stats,选择双曲线模型y4 %预测 x5=[1,6,9,12,13,16,17]; y5=b (1)+b (2)./x5; [x5',y5'] 编程4160女子数据画图(A直方图) clear; closeall; loaddata1; xmax=max(samp); xmin=min(samp); R=xmax-xmin;%极距 g_int=round(R/12*10)/10;%组段 g_bins=[0.5: g_int: 1.7]';%边界 xcount=zeros(size(g_bins)); fork=1: length(g_bins)-1 xcount(k)=sum(samp>=g_bins(k)&samp end xcount(end)=sum(samp>=g_bins(end)); Total=sum(xcount); f=xcount./Total; xccum=cumsum(xcount); fcum=cumsum(f); disp('160名正常女子的血清甘油三酯(mmol/L)频数分布表') disp('---------------------------------------------------------') disp('组段频数累计频数频率%累积频率%') disp('---------------------------------------------------------') disp([repmat('',size(g_bins)),strcat(num2str(g_bins,'%.1f'),'-'),... repmat('',size(g_bins)),num2str(xcount,'%2d'),... repmat('',size(g_bins)),num2str(xccum,'%3d'),... repmat('',size(g_bins)),num2str(f*100,'%.1f'),... repmat('',size(g_bins)),num2str(fcum*100,'%.1f')]); disp('---------------------------------------------------------'); disp(['合计',num2str(sum(xcount)),'',num2str(sum(f)*100)]); disp('---------------------------------------------------------'); figure bar(g_bins,xcount,1); title('直方图'); xlabel('组段'); ylabel('频数'); 编程4160女子数据画图(BCD平均、离散、斜度) clear; closeall; loaddata1; xmean=mean(samp); xgmean=geomean(samp); xmed=median(samp); disp('平均水平指标: '); disp(strcat('算术均值=',num2str(xmean,'%.2f'),',几何均值=',... num2str(xgmean,'%.2f'),',中位数=',num2str(xmed,'%.2f'))); xrange=range(samp); xiqr=iqr(samp); xvar=var(samp); xstd=std(samp); disp('离散程度指标: '); disp(strcat('极差=',num2str(xrange,'%.2f'),',四分位数间距=',... num2str(xiqr,'%.2f'),',方差=',num2str(xvar,'%.2f'),... ',标准差=',num2str(xstd,'%.2f'))); xskew=skewness(samp); xkurt=kurtosis(samp); disp('分布形状指标: '); disp(strcat('斜度=',num2str(xskew,'%.2f'),',峭度=',... num2str(xkurt,'%.2f'))); 编程5猜数游戏 clear; clc; x=fix(100*rand (1)); i=1; whilei<=7 y=input('yourguessnumber: '); ify==x disp('Youwon! ') break; elseifx>y disp('low') else disp('high') end i=i+1; end 编程6筛选法求素数 functiony=sushu(x) y=2: x; n=2: x; forj=2: x m=j*n; y=setdiff(y,m); end end 模拟7小车喷射 模拟8线性连续时间 模拟8线性连续时间(2不用仿真的方法) [t,x]=ode45('odefun_shixun2',[0300],[00]); plot(t,x(: 1),'r-','LineWidth',2); xlabel('t-秒'); ylabel('x-米'); title('位置-时间') a=num2str(x(300,1)); b=strcat('x(300,1)=',a,'米'); text(150,0.38,b,'FontSize',18); 模拟9用户注册(gui1) 模拟9(gui1Callback数据) %---Executesonbuttonpressinpushbutton1. functionpushbutton1_Callback(hObject,eventdata,handles) %hObjecthandletopushbutton1(seeGCBO) %eventdatareserved-tobedefinedinafutureversionofMATLAB %handlesstructurewithhandlesanduserdata(seeGUIDATA) globalabcdefx; a=get(handles.edit1,'String'); ifget(handles.radiobutton1,'value') b=get(handles.radiobutton1,'String'); elseifget(handles.radiobutton2,'value') b=get(handles.radiobutton2,'String'); end c=get(handles.edit2,'String'); val=get(handles.popupmenu1,'Value'); str=get(handles.popupmenu1,'String'); d=str{val}; val1=get(handles.listbox1,'Value'); str1=get(handles.listbox1,'String'); e=str1{val1}; f=get(handles.edit3,'String'); ifget(handles.checkbox8,'Value') g=get(handles.checkbox8,'String'); else g=''; end ifget(handles.checkbox9,'Value') h=get(handles.checkbox9,'String'); else h=''; end ifget(handles.checkbox10,'Value') i=get(handles.checkbox10,'String'); else i=''; end ifget(handles.checkbox11,'Value') j=get(handles.checkbox11,'String'); else j=''; end ifget(handles.checkbox12,'Value') k=get(handles.checkbox12,'String'); else k=''; end gh=strcat(g,h); ij=strcat(i,j); ghij=strcat(gh,ij); x=strcat(ghij,k); gui1('Visible','off'); gui2('Visible','on'); 模拟9(gui1重置程序) %guidata(hObject,handles); %---Executesonbuttonpressinpushbutton2. functionpushbutton2_Callback(hObject,eventdata,handles) %hObjecthandletopushbutton2(seeGCBO) %eventdatareserved-tobedefinedinafutureversionofMATLAB %handlesstructurewithhandlesanduserdata(seeGUIDATA) set(handles.edit1,'String',''); set(handles.edit2,'String',''); set(handles.edit3,'String',''); set(handles.checkbox8,'Value',0); set(handles.checkbox9,'Value',0); set(handles.checkbox10,'Value',0); set(handles.checkbox11,'Value',0); set(handles.checkbox12,'Value',0); guidata(hObject,handles); 模拟9用户注册(gui2) 模拟9用户注册(gui2可见性) %---Executesonbuttonpressinpushbutton1. functionpushbutton1_Callback(hObject,eventdata,handles) %hObjecthandletopushbutton1(seeGCBO) %eventdatareserved-tobedefinedinafutureversionofMATLAB %handlesstructurewithhandlesanduserdata(seeGUIDATA) gui2('Visible','off'); gui1('Visible','on'); 模拟9用户注册(gui2输入值) %---Executesjustbeforegui2ismadevisible. functiongui2_OpeningFcn(hObject,eventdata,handles,varargin) %Thisfunctionhasnooutputargs,seeOutputFcn. %hObjecthandletofigure %eventdatareserved-tobedefinedinafutureversionofMATLAB %handlesstructurewithhandlesanduserdata(seeGUIDATA) %varargincommandlineargumentstogui2(seeVARARGIN) %Choosedefaultcommandlineoutputforgui2 handles.output=hObject; globalabcdefx; set(handles.edit2,'String',a); set(handles.edit3,'String',b); set(handles.edit4,'String',c); set(handles.edit5,'String',d); set(handles.edit6,'String',e); set(handles.edit7,'String',f); set(handles.edit8,'String',x); %Updatehandlesstructure guidata(hObject,handles); %UIWAITmakesgui2waitforuserresponse(seeUIRESUME) %uiwait(handles.figure1); 模拟10画图 %---Executesonbuttonpressinpushbutton1. functionpushbutton1_Callback(hObject,eventdata,handles) %hObjecthandletopushbutton1(seeGCBO) %eventdatareserved-tobedefinedinafutureversionofMATLAB %handlesstructurewithhandlesanduserdata(seeGUIDATA) val=get(handles.listbox1,'Value'); str=get(handles.listbox1,'String'); v1=get(handles.popupmenu1,'Value'); s1=get(handles.popupmenu1,'String'); v2=get(handles.popupmenu2,'Value'); s2=get(handles.popupmenu2,'String'); v3=get(handles.popupmenu3,'Value'); s3=get(handles.popupmenu3,'String'); stt=strcat(s1{v1},s2{v2},s3{v3}); switchstr{val} case'plot' x=-pi: 0.1: pi; y=sin(x); plot(x,y,stt); case'semilogx' semilogx([1: 100],stt); case'semilogy' semilogy([1: 100],stt); case'loglog' x=logspace(-1,2); y=exp(x); loglog(x,y,stt); case'plot3' x=0: pi/50: 10*pi; plot3(sin(x),cos(x),x,stt); case'mesh' [x,y]=meshgrid(-2: 0.2: 2); z=peaks(x,y); mesh(x,y,z); case'surf' [x,y]=meshgrid(-2: 0.2: 2); z=peaks(x,y); surf(x,y,z); end guidata(hObject,handles);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab 考试 答案