matlab上机习题详细讲解试题答案解析.docx
《matlab上机习题详细讲解试题答案解析.docx》由会员分享,可在线阅读,更多相关《matlab上机习题详细讲解试题答案解析.docx(126页珍藏版)》请在冰豆网上搜索。
matlab上机习题详细讲解试题答案解析
P第一次实验答案
1.设
要求以0.01秒为间隔,求出y的151个点,并求出其导数的值和曲线。
clc
clear
x=0:
0.01:
1.5;
y=sqrt(3)/2*exp(-4*x).*sin(4*sqrt(3)*x+pi/3)
y1=diff(y)
subplot(2,1,1)
plot(x,y)
subplot(2,1,2)
plot(x(1:
150),y1)
2绘制极坐标系下曲线(a,b,n自定数据)
clc
clear
a=10;
b=pi/2;
n=5;
theta=0:
pi/100:
2*pi;
rho=a*cos(b+n*theta);
polar(theta,rho)
3.列出求下列空间曲面交线的程序
clc
clear
x=[-5:
0.5:
5];
[X,Y]=meshgrid(x);
z1=X.^2-2*Y.^2;
z2=X.*2-Y.*3;
xlabel('x')
ylabel('y')
zlabel('z')
surf(X,Y,z1)
holdon
surf(X,Y,z2)
k=find(abs(z1-z2)<0.5);
x1=X(k)
y1=Y(k)
z3=x1.^2-2*y1.^2
holdon
plot3(x1,y1,z3,'*')
4、设
把x=0~2π间分为101点,画出以x为横坐标,y为纵坐标的曲线,要求有图形标注。
clc
clear
x=-2*pi:
0.1:
2*pi;
y=cos(x).*(0.5+sin(x)*3./(1+x.^2));
plot(x,y,'b*-');
title('绘图');
xlabel('x坐标');
ylabel('y坐标');
legend('原函数')
gtext('y=cos(x)(0.5+3*sin(x)/(1+x^2))')
5、求下列联立方程的解
clc
clear
a=[3,4,-7,-12;5,-7,4,2;1,0,8,-5;-6,5,-2,10];
b=[4,-3,9,-8];
c=b/a;
x=c(1,1)
y=c(1,2)
z=c(1,3)
w=c(1,4)
6.假设一曲线数据点为
x=0:
2:
4*pi;
y=sin(x).*exp(-x/5);
试将x的间距调成0.1,采用不同插值方法进行插值,并通过子图的形式将不同插值结果和原始数据点绘制在同一图形窗口。
clc
clear
x=0:
2:
4*pi;
y=sin(x).*exp(-x./5);
x1=0:
0.1:
4*pi;
y01=interp1(x,y,x1,'spline');
y02=interp1(x,y,x1,'cubic');
y03=interp1(x,y,x1,'nearest');
y04=interp1(x,y,x1);
subplot(3,2,1);
plot(x,y,'o-');
title('Ôº¯Êý');
subplot(3,2,2);
plot(x1,y01,'gx-');
title('spline');
subplot(3,2,3);
plot(x1,y02,'m+-');
title('cubic');
subplot(3,2,4);
plot(x1,y03,'r*-');
title('nearest');
subplot(3,2,5);
plot(x1,y04,'kd-');
title('line');
第二次试验答案
1、编制m文件,等待键盘输入,输入密码20120520,密码正确,显示输入密码正确,程序结束;否则提示,重新输入。
clear
a=input('输入密码:
')
whilea~=20120520
disp('密码错误,请重新输入:
')
clear
a=input('输入密码:
')
end
ifa==20120520
disp('密码输入正确!
')
end
2、编写一个函数实现以下功能:
a.若没有实参输入变量,则用蓝色实线画出半径为1的圆;
b.若有两个实参输入变量N,R,则用蓝色实线画出外接半径为R的正N多边形;
d.若有两个实参输出变量,则分别输出正多边形的面积S和周长L。
g.如果输入变量多于三个或输出变量多于两个则给出错误提示信息。
(提示:
外接半径为R的正N多边形参数式:
t=0:
2*pi/N:
2*pi;x=R*sin(t);y=R*cos(t)当N≥100时,可看作是半径为R的圆)
function[S,L]=shiyan22(N,R,str)
switchnargin
case0
N=100;R=1;str='-b';
case1
R=1;str='-b';
case2
str='-b';
case3
;
otherwise
error('输入量太多。
');
end;
t=0:
2*pi/N:
2*pi;
x=R*sin(t);y=R*cos(t);
ifnargout==0
plot(x,y,str);
elseifnargout>2
error('输入量太多。
');
else
S=N*R*R*sin(2*pi/N)/2;
L=2*N*R*sin(pi/N);
plot(x,y,str)
end
axisequalsquare
boxon
3、编写一个学生成绩管理的程序,完成下列功能:
(1)构造一个学生结构,包括学生的姓名,课程名称(限M门),考试成绩和平均值等域;
(2)从键盘上,连续输入N个学生的情况,分别将这些学生的情况填入到相应的域,若域中没有值即为空或0,并分别计算其平均值写入到其平均值域。
(3)根据平均值排序(由高到低),并输出学生的姓名和平均值。
clc
clear
n=input('pleaseinputstudentsnumber:
');
forx=1:
n
number(x)=struct('name','','Curriculum1','','Curriculum2','','Curriculum3','','Average','');
end
fori=1:
n
number(i).name=input('name:
','s');
number(i).Curriculum1=input('pleaseinputthescores\nCurriculum1:
');
number(i).Curriculum2=input('Curriculum2:
');
number(i).Curriculum3=input('Curriculum3:
');
number(i).Average=(number(i).Curriculum1+number(i).Curriculum2+number(i).Curriculum3)/3;
disp('theaverageis:
')
disp(num2str(number(i).Average))
end
NameCell=cell(1,n);
Array=[1,n];
fori=1:
n
NameCell(1,i)={number(i).name};
Array(i)=number(i).Average;
end
forj=1:
(n-1)
iptr=j;
fori=(j+1):
n
ifArray(i)>Array(iptr)%比较相邻前后大小¡
iptr=i;
end
end
ifj~=iptr%若后面比前面大,互换
Name=NameCell(1,i);
NameCell(1,i)=NameCell(1,j);
NameCell(1,j)=Name;
average=Array(i);
Array(i)=Array(j);
Array(j)=average;
j=1;
iptr=j;
end
end
disp('成绩排序如下:
')
fori=1:
n
disp(strcat('名次:
',num2str(i),'名字',NameCell(1,i),'平均成绩:
',num2str(Array(i))))
end
4、使用句柄图像对象绘制曲线:
y=2e-0.5xcos(πx),同时对曲线进行标注和修饰。
x=-12:
0.02:
12
y=2*exp((-0.5)*x).*cos(pi*x)
h_f=figure('Position',[200300300300],'menubar','none')
h_a1=axes('position',[0.1,0.1,.8,.8])
h_t=title(h_a1,'函数=2*exp((-0.5)*x).*cos(pi*x)')
h_1=line(x,y)
set(gca,'xtick',[(-6)*pi(-4)*pi(-2)*pi0
(2)*pi(4)*pi(6)*pi])
set(gca,'xticklabel',{'(-6)*pi','(-4)*pi','(-2)*pi','0','
(2)*pi','(4)*pi','(6)pi'})
set(gca,'xgrid','on','ygrid','on')
set(h_1,'linewidth',2)
set(get(h_t,'parent'),'color','y')
h_anm1=annotation(gcf,'rectangle',[0.10.5.80.4],'FaceAlpha',.7,'FaceColor','red')
第三次试验答案
1、做一个带按钮的界面,当按动“播放”按钮时调入声音文件并播放,显示声音波形,并建立一个用于关闭界面的按钮对象。
(提示,找一个.wav文件,简单起见可以在windows目录下找一个文件,将其放在当前工作目录下或搜索路径上。
具体用法请参照:
[y,f,b]=wavread(*.wav');%读入声音文件
sound(y,f,b)%由声卡播放声音
plot(y)%画出波形
2、创建一个用于绘图参数选择的菜单对象,其中包含三个选项LineStyle、Marker和Color,每个选项下面又包含若干的子项分别可以进行选择图线的类型、标记点的类型和颜色(每个子项不少于3个),当按下“绘图”按钮时,根据选项绘制正弦曲线(缺省时为蓝色无标记实线)。
(注意使用全球变量)
functionvarargout=shiyan32(varargin)
%SHIYAN32MATLABcodeforshiyan32.fig
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@shiyan32_OpeningFcn,...
'gui_OutputFcn',@shiyan32_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforeshiyan32ismadevisible.
functionshiyan32_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstoshiyan32(seeVARARGIN)
%Choosedefaultcommandlineoutputforshiyan32
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakesshiyan32waitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
globalc
c='b'
globall
l='-'
globalm
m=''
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=shiyan32_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%--------------------------------------------------------------------
functionUntitled_1_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globall
%--------------------------------------------------------------------
functionUntitled_8_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_8(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalm
%--------------------------------------------------------------------
functioncolor_Callback(hObject,eventdata,handles)
%hObjecthandletocolor(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalc
%--------------------------------------------------------------------
functionUntitled_14_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_14(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalc
c='r'
%--------------------------------------------------------------------
functionUntitled_15_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_15(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalc
c='b'
%--------------------------------------------------------------------
functionUntitled_16_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_16(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalc
c='g'
%--------------------------------------------------------------------
functionUntitled_17_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_17(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalc
c='y'
%--------------------------------------------------------------------
functionUntitled_10_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_10(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalm
m='o'
%--------------------------------------------------------------------
functionUntitled_11_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_11(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalm
m='*'
%--------------------------------------------------------------------
functionUntitled_13_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_13(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globalm
m='d'
%--------------------------------------------------------------------
functionUntitled_4_Callback(hObject,eventdata,handles)
%hObjecthandletoUntitled_4(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
globall
l='-'
%--------------------------------------------------------------------
functionUntitled_5_Callback(hObject,even