matlab上机习题详解试题答案.docx
《matlab上机习题详解试题答案.docx》由会员分享,可在线阅读,更多相关《matlab上机习题详解试题答案.docx(62页珍藏版)》请在冰豆网上搜索。
![matlab上机习题详解试题答案.docx](https://file1.bdocx.com/fileroot1/2023-6/28/1a0952ef-aa59-480a-b613-a907735c996e/1a0952ef-aa59-480a-b613-a907735c996e1.gif)
matlab上机习题详解试题答案
matlab上机习题详解_试题答案
clc
clearP第一次实验答案
x=-2*pi:
0.1:
2*pi;3,,,y=cos(x).*(0.5+sin(x)*3./(1+x.^2));,4tsin43y,,et,,,1(设plot(x,y,'b*-');23,,title('绘图');
xlabel('x坐标');要求以0.01秒为间隔,求出y的151个点,ylabel('y坐标');并求出其导数的值和曲线。
legend('原函数')clcgtext('y=cos(x)(0.5+3*sin(x)/(1+x^2))')clearx=0:
0.01:
1.5;
y=sqrt(3)/2*exp(-4*x).*sin(4*sqrt(3)*x+pi/35、求下列联立方程的解)3x,4y,7z,12w,4y1=diff(y)
subplot(2,1,1)5x,7y,4z,2w,,3plot(x,y)x,8z,5w,9subplot(2,1,2)
plot(x(1:
150),y1),6x,5y,2z,10w,,8
clc
clear2绘制极坐标系下曲线(a,b,n自定数据)
a=[3,4,-7,-12;5,-7,4,2;1,0,8,-5;-6,5,-2,10];,,,,acosb,n,b=[4,-3,9,-8];clcc=b/a;clearx=c(1,1)a=10;y=c(1,2)b=pi/2;z=c(1,3)n=5;w=c(1,4)theta=0:
pi/100:
2*pi;rho=a*cos(b+n*theta);6.假设一曲线数据点为polar(theta,rho)
x=0:
2:
4*pi;
y=sin(x).*exp(-x/5);3.列出求下列空间曲面交线的程序
试将x的间距调成0.1,采用不同插值方法进行22z,2x,3yz,x,2y2插值,并通过子图的形式将不同插值结果和原始数1
据点绘制在同一图形窗口。
clc
clearclc
x=[-5:
0.5:
5];clear
[X,Y]=meshgrid(x);x=0:
2:
4*pi;z1=X.^2-2*Y.^2;y=sin(x).*exp(-x./5);z2=X.*2-Y.*3;x1=0:
0.1:
4*pi;xlabel('x')y01=interp1(x,y,x1,'spline');ylabel('y')y02=interp1(x,y,x1,'cubic');zlabel('z')y03=interp1(x,y,x1,'nearest');surf(X,Y,z1)y04=interp1(x,y,x1);holdonsubplot(3,2,1);surf(X,Y,z2)plot(x,y,'o-');k=find(abs(z1-z2)<0.5);title('Ô,º?
Êý');x1=X(k)subplot(3,2,2);y1=Y(k)plot(x1,y01,'gx-');z3=x1.^2-2*y1.^2title('spline');holdonsubplot(3,2,3);plot3(x1,y1,z3,'*')plot(x1,y02,'m+-');
title('cubic');
subplot(3,2,4);,,x3sinplot(x1,y03,'r*-');yx,cos0.5,4、设,,2(1,x)title('nearest');,,subplot(3,2,5);把x=0~2π间分为101点,画出以x为横坐标,yplot(x1,y04,'kd-');
title('line');为纵坐标的曲线,要求有图形标注。
str='-b';第二次试验答案case3
;1、编制m文件,等待键盘输入,输入密码otherwise
error('输入量太多。
');20120520,密码正确,显示输入密码正确,程end;
t=0:
2*pi/N:
2*pi;序结束;否则提示,重新输入。
x=R*sin(t);y=R*cos(t);
ifnargout==0
plot(x,y,str);clearelseifnargout>2a=input('输入密码:
')
error('输入量太多。
');whilea~=20120520
elsedisp('密码错误,请重新输入:
')
S=N*R*R*sin(2*pi/N)/2;clear
L=2*N*R*sin(pi/N);a=input('输入密码:
')
plot(x,y,str)endendifa==20120520axisequalsquaredisp('密码输入正确!
')
boxon
end
3、编写一个学生成绩管理的程序,完成下列2、编写一个函数实现以下功能:
功能:
a.若没有实参输入变量,则用蓝色实线画出
(1)构造一个学生结构,包括学生的姓名,课半径为1的圆;程名称(限M门),考试成绩和平均值等域;若有两个实参输入变量N,R,则用蓝色实b.
(2)从键盘上,连续输入N个学生的情况,分线画出外接半径为R的正N多边形;别将这些学生的情况填入到相应的域,若域中d.若有两个实参输出变量,则分别输出正多没有值即为空或0,并分别计算其平均值写入边形的面积S和周长L。
到其平均值域。
g.如果输入变量多于三个或输出变量多于两(3)根据平均值排序(由高到低),并输出学个则给出错误提示信息。
生的姓名和平均值。
(提示:
外接半径为R的正N多边形参数
clc
式:
t=0:
2*pi/N:
2*pi;x=R*sin(t);y=R*cos(t)当Nclear
n=input('pleaseinputstudentsnumber:
');
forx=1:
n?
100时,可看作是半径为R的圆)
number(x)=struct('name','','Curriculum1','','Curriculum2','','Curriculum3','','Averagefunction[S,L]=shiyan22(N,R,str)','');
end
fori=1:
nswitchnarginnumber(i).name=input('name:
','s');case0number(i).Curriculum1=input('pleaseN=100;R=1;str='-b';inputthescores\nCurriculum1:
');case1
R=1;str='-b';number(i).Curriculum2=input('Curriculum2:
')
;case2
set(h_1,'linewidth',2)
number(i).Curriculum3=input('Curriculum3:
')set(get(h_t,'parent'),'color','y');h_anm1=annotation(gcf,'rectangle',[0.1
0.5.8
number(i).Average=(number(i).Curriculum1+nu0.4],'FaceAlpha',.7,'FaceColor','red')mber(i).Curriculum2+number(i).Curriculum3)/
3;
disp('theaverageis:
')
disp(num2str(number(i).Average))第三次试验答案end
NameCell=cell(1,n);1、做一个带按钮的界面,当按动“播放”按Array=[1,n];
fori=1:
n钮时调入声音文件并播放,显示声音波形,并NameCell(1,i)={number(i).name};
Array(i)=number(i).Average;
建立一个用于关闭界面的按钮对象。
(提示,end
forj=1:
(n-1)
iptr=j;找一个.wav文件,简单起见可以在windows
fori=(j+1):
n
ifArray(i)>Array(iptr)%比较相邻前后目录下找一个文件,将其放在当前工作目录下大小?
iptr=i;或搜索路径上。
具体用法请参照:
end
end[y,f,b]=wavread(*.wav');%读入声音文ifj~=iptr%若后面比前面大,
互换件Name=NameCell(1,i);
NameCell(1,i)=NameCell(1,j);sound(y,f,b)%由声卡播放NameCell(1,j)=Name;
average=Array(i);声音Array(i)=Array(j);
Array(j)=average;
plot(y)%画出波形j=1;
iptr=j;
endend
disp('成绩排序如下:
')fori=1:
n
disp(strcat('名次:
',num2str(i),'名字2、创建一个用于绘图参数选择的菜单对象,',NameCell(1,i),'平均成绩:
',num2str(Array(i))))其中包含三个选项LineStyle、Marker和Color,end
每个选项下面又包含若干的子项分别可以进
-0.5x4、使用句柄图像对象绘制曲线:
y=2ecos(π行选择图线的类型、标记点的类型和颜色(每
x),同时对曲线进行标注和修饰。
个子项不少于3个),当按下“绘图”按钮时,
根据选项绘制正弦曲线(缺省时为蓝色无标记x=-12:
0.02:
12
y=2*exp((-0.5)*x).*cos(pi*x)实线)。
(注意使用全球变量)h_f=figure('Position',[200300300300],'menubar','none')functionvarargout=shiyan32(varargin)h_a1=axes('position',[0.1,0.1,.8,.8])%SHIYAN32MATLABcodeforshiyan32.figh_t=title(h_a1,'函数=2*exp((-0.5)*x).*cos(pi*x)')%Begininitializationcode-DONOTEDITh_1=line(x,y)gui_Singleton=1;set(gca,'xtick',[(-6)*pi(-4)*pi(-2)*pi0gui_State=struct('gui_Name',
(2)*pi(4)*pi(6)*pi])mfilename,...set(gca,'xticklabel',{'(-6)*pi','(-4)*pi','gui_Singleton','(-2)*pi','0','
(2)*pi','(4)*pi','(6)pi'})gui_Singleton,...set(gca,'xgrid','on','ygrid','on')'gui_OpeningFcn',
@shiyan32_OpeningFcn,...%Getdefaultcommandlineoutputfromhandles
'gui_OutputFcn',structure
@shiyan32_OutputFcn,...varargout{1}=handles.output;
'gui_LayoutFcn',[],...'gui_Callback',[]);ifnargin&&ischar(varargin{1})%gui_State.gui_Callback=-------------------------------------------str2func(varargin{1});-------------------------endfunctionUntitled_1_Callback(hObject,eventdata,handles)ifnargout%hObjecthandletoUntitled_1(seeGCBO)[varargout{1:
nargout}]=%eventdatareserved-tobedefinedinagui_mainfcn(gui_State,varargin{:
});futureversionofMATLABelse%handlesstructurewithhandlesandusergui_mainfcn(gui_State,varargin{:
});data(seeGUIDATA)endgloball%Endinitializationcode-DONOTEDIT%%---Executesjustbeforeshiyan32ismade-------------------------------------------visible.-------------------------functionshiyan32_OpeningFcn(hObject,functionUntitled_8_Callback(hObject,eventdata,handles,varargin)eventdata,handles)%Thisfunctionhasnooutputargs,see%hObjecthandletoUntitled_8(seeGCBO)OutputFcn.%eventdatareserved-tobedefinedina%hObjecthandletofigurefutureversionofMATLAB%eventdatareserved-tobedefinedina%handlesstructurewithhandlesanduserfutureversionofMATLABdata(seeGUIDATA)%handlesstructurewithhandlesanduser
data(seeGUIDATA)globalm%varargincommandlineargumentsto%shiyan32(seeVARARGIN)--------------------------------------------------------------------%Choosedefaultcommandlineoutputforfunctioncolor_Callback(hObject,eventdata,shiyan32handles)handles.output=hObject;%hObjecthandletocolor(seeGCBO)
%eventdatareserved-tobedefinedina%UpdatehandlesstructurefutureversionofMATLAB
guidata(hObject,handles);%handlesstructurewithhandlesanduser
data(seeGUIDATA)
%UIWAITmakesshiyan32waitforuserresponseglobalc(seeUIRESUME)%%uiwait(handles.figure1);-------------------------------------------
-------------------------globalc
functionUntitled_14_Callback(hObject,c='b'eventdata,handles)
%hObjecthandletoUntitled_14(seeGCBO)globall%eventdatareserved-tobedefinedinal='-'futureversionofMATLAB
%handlesstructurewithhandlesanduserglobalmdata(seeGUIDATA)
m=''globalc%---Outputsfromthisfunctionarereturnedc='r'tothecommandline.
functionvarargout=%
shiyan32_OutputFcn(hObject,eventdata,-------------------------------------------handles)-------------------------
%varargoutcellarrayforreturningoutputfunctionUntitled_15_Callback(hObject,args(seeVARARGOUT);eventdata,handles)
%hObjecthandletofigure%hObjecthandletoUntitled_15(seeGCBO)
%eventdatareserved-tobedefinedina%eventdatareserved-tobedefinedinafutureversionofMATLABfutureversionofMATLAB
%handlesstructurewithhandlesanduser%handlesstructurewithhandlesanduserdata(seeGUIDATA)data(seeGUIDATA)
%handlesstructurewithhandlesanduserglobalcdata(seeGUIDATA)
c='b'
%globalm-------------------------------------------
-------------------------m='d'
functionUntitled_16_Callback(hObject,%eventdata,handles)-------------------------------------------%hObjecthandletoUntitled_16(seeGCBO)-------------------------%eventdatareserved-tobedefinedinafunctionUntitled_4_Callback(hObject,futureversionofMATLABeventdata,handles)%handlesstructurewithhandlesanduser%hObjecthandletoUntitled_4(seeGCBO)data(seeGUIDATA)%eventdatareserved-tobedefinedina
futureversionofMATLABglobalc%handlesstructurewithhandlesanduserc='g'data(seeGUIDATA)
%globall-------------------------------------------
-------------------------l='-'
functionUntitled_17_Callback(hObject,%eventdata,handles)-------------------------------------------%hObjecthandletoUntitled_17(seeGCBO)-------------------------%eventdatareserved-tobedefinedinafunctionUntitled_5_Callback(hObject,futureversionofMATLABeventdata,handles)%handlesstructurewithhandlesanduser%hObjecthandletoUntitled_5(seeGCBO)data(seeGUIDATA)%eventdatareserved-tobedefinedina
futureversionofMATLABglobalc%handlesstructurewithhandlesanduserc='y'data(seeGUIDATA)
%globall-------------------------------------------
-------------------------l='--'
functionUntitled_10_Callback(hObject,%eventdata,handles)-------------------------------------------%hObjecthandletoUntitled_10(seeGCBO)-------------------------%eventdatareserved-tobedefinedinafunctionUntitled_7_Callback(hObject,futureversionofMATLABeventdata,handles)%handlesstructurewithhandlesanduser%hObjecthandletoUntitled_7(seeGCBO)data(seeGUIDATA)%eventdatareserved-tobedefinedina
futureversionofMATLABglobalm%handlesstructurewithhandlesanduserm='o'data(seeGUIDATA)
%globall-------------------------------------------
-------------------------l='-.'
functionUntitled_11_Callback