matlab上机习题详解-试题答案

上传人:xgs****56 文档编号:10369861 上传时间:2020-04-11 格式:DOC 页数:37 大小:1.05MB
返回 下载 相关 举报
matlab上机习题详解-试题答案_第1页
第1页 / 共37页
matlab上机习题详解-试题答案_第2页
第2页 / 共37页
matlab上机习题详解-试题答案_第3页
第3页 / 共37页
点击查看更多>>
资源描述
P第一次实验答案 1 设要求以0.01秒为间隔,求出y的151个点,并求出其导数的值和曲线。clcclearx=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自定数据)clccleara=10;b=pi/2;n=5;theta=0:pi/100:2*pi;rho=a*cos(b+n*theta);polar(theta,rho)3. 列出求下列空间曲面交线的程序clcclearx=-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)hold onsurf(X,Y,z2)k=find(abs(z1-z2)2 error(输入量太多。);else S=N*R*R*sin(2*pi/N)/2; L=2*N*R*sin(pi/N); plot(x,y,str) endaxis equal squarebox on3、编写一个学生成绩管理的程序,完成下列功能:(1) 构造一个学生结构,包括学生的姓名,课程名称(限M门),考试成绩和平均值等域; (2) 从键盘上,连续输入N个学生的情况,分别将这些学生的情况填入到相应的域,若域中没有值即为空或0,并分别计算其平均值写入到其平均值域。(3) 根据平均值排序(由高到低),并输出学生的姓名和平均值。clcclearn=input(please input students number:);for x=1:n number(x)=struct(name,Curriculum1,Curriculum2,Curriculum3,Average,);endfor i=1:n number(i).name=input(name:,s); number(i).Curriculum1=input(please input the scoresnCurriculum1:); number(i).Curriculum2=input(Curriculum2:); number(i).Curriculum3=input(Curriculum3:); number(i).Average=(number(i).Curriculum1+number(i).Curriculum2+number(i).Curriculum3)/3; disp(the average is:) disp(num2str(number(i).Average)end NameCell=cell(1,n);Array=1,n;for i=1:n NameCell(1,i)=number(i).name; Array(i)=number(i).Average;endfor j=1:(n-1) iptr = j; for i=(j+1):n if Array(i)Array(iptr) %比较相邻前后大小 iptr=i; end end if j=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; endenddisp(成绩排序如下:)for i=1:n disp(strcat(名次: ,num2str(i), 名字 ,NameCell(1,i), 平均成绩: ,num2str(Array(i)end 4、使用句柄图像对象绘制曲线:y=2e-0.5xcos(x),同时对曲线进行标注和修饰。x=-12:0.02:12y=2*exp(-0.5)*x).*cos(pi*x)h_f=figure(Position,200 300 300 300,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)*pi 0 (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.1 0.5 .8 0.4,FaceAlpha,.7,FaceColor,red)第三次试验答案1、做一个带按钮的界面,当按动“播放”按钮时调入声音文件并播放,显示声音波形,并建立一个用于关闭界面的按钮对象。(提示,找一个.wav文件,简单起见可以在windows目录下找一个文件,将其放在当前工作目录下或搜索路径上。具体用法请参照:y,f,b=wavread(*.wav); % 读入声音文件sound(y,f,b) % 由声卡播放声音plot(y) % 画出波形2、创建一个用于绘图参数选择的菜单对象,其中包含三个选项LineStyle、Marker和Color,每个选项下面又包含若干的子项分别可以进行选择图线的类型、标记点的类型和颜色 (每个子项不少于3个),当按下“绘图”按钮时,根据选项绘制正弦曲线(缺省时为蓝色无标记实线)。(注意使用全球变量)function varargout = shiyan32(varargin)% SHIYAN32 MATLAB code for shiyan32.fig% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, shiyan32_OpeningFcn, . gui_OutputFcn, shiyan32_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);end if nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT % - Executes just before shiyan32 is made visible.function shiyan32_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to shiyan32 (see VARARGIN) % Choose default command line output for shiyan32handles.output = hObject; % Update handles structureguidata(hObject, handles); % UIWAIT makes shiyan32 wait for user response (see UIRESUME)% uiwait(handles.figure1);global cc=bglobal ll=-global mm= % - Outputs from this function are returned to the command line.function varargout = shiyan32_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structurevarargout1 = handles.output; % -function Untitled_1_Callback(hObject, eventdata, handles)% hObject handle to Untitled_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global l % -function Untitled_8_Callback(hObject, eventdata, handles)% hObject handle to Untitled_8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global m % -function color_Callback(hObject, eventdata, handles)% hObject handle to color (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global c % -function Untitled_14_Callback(hObject, eventdata, handles)% hObject handle to Untitled_14 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global cc=r % -function Untitled_15_Callback(hObject, eventdata, handles)% hObject handle to Untitled_15 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global cc=b % -function Untitled_16_Callback(hObject, eventdata, handles)% hObject handle to Untitled_16 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global cc=g % -function Untitled_17_Callback(hObject, eventdata, handles)% hObject handle to Untitled_17 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global cc=y% -function Untitled_10_Callback(hObject, eventdata, handles)% hObject handle to Untitled_10 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global mm=o % -function Untitled_11_Callback(hObject, eventdata, handles)% hObject handle to Untitled_11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global mm=* % -function Untitled_13_Callback(hObject, eventdata, handles)% hObject handle to Untitled_13 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) global mm=d % -function Untitled_4_Callback(hObject, eventdata, handles)% hObject handle to Untitled_4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global ll=- % -function Untitled_5_Callback(hObject, eventdata, handles)% hObject handle to Untitled_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global ll=- % -function Untitled_7_Callback(hObject, eventdata, handles)% hObject handle to Untitled_7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global ll=-. % - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global s global l global m global cx=0:0.1:2*pi;y=sin(x);s=strcat(l,m,c);plot(x,y,s)2. 设和是两个10行10列的矩阵(数组),试说明命令A*B, A/B, AB, A.*(B.A), A./B, A.B和sin(A).*exp(B)的涵义。A,B的矩阵乘法,矩阵A乘以B的逆,矩阵B乘以A的逆,B的每个元素的对应A次幂乘以A对应的每个元素A的每个元素除以B对应的每个元素,B的每个元素除以A对应的每个元素,A的每个元素的正弦与B对应的每个元素的指数求积,3. 设A为数组,B为一个行数大于3的数组,请给出(1)删除A的第4、8、12三列的命令; (2)删除B的倒数第3行的命令;(3)求符号极限的命令集; (4)求的3阶导数的命令集;(5)计算定积分的命令,并给出计算结果(!)A(:,4 8 12)=(2)B(end-2)=(3)syms m n xlimit(tan(m*x)/(n*x)(4)syms x yy=atan(x+3)/(x-2)-log(1+exp(-2)*x)diff(y,3)(5)syms xint(abs(x)+sin(x)*x2,-1,1)得分 三基本编程题(每小题10分,共30分)1. Write a program to compute the following expressions;Here, we suppose the variable x has existed in the workspace.for i=1:10 for j=1:10xbar(i)=xbar(i)+x(i,j); end xbar(i)=xbar(i)/10;endfor i=1:10 for j=1:10 t1=0;t2=0;t3=0; for k=1:3 t1=t1+(x(i,k)-xbar(i)*(x(j,k)-xbar(j); t2=t2+(x(i,k)-xbar(i)2; t3=t3+(x(j,k)-xbar(j)2; end r2(i,j)=t1/sqrt(t2*t3); endendr2 2. (1) Using plot() function to plot the curves of and in the range of , let their color are red and green respectively, and add the grid to the figure. (2) Using fplot() function and ezplot() function to plot above-mentioned (上述的) curves respectively.(1)x=-2*pi:pi/100:2*pi;y=(sin(2*x)+cos(3*x).*exp(-2*x);z=sin(x)/x;plot(x,y,r,x,z,g)(2)fplot(sin(2*x)+cos(3*x).*exp(-2*x), sin(x)/x,-2*pi 2*pi) 3. Plot the 3D mesh figure and 3D surface figure of the functionin the range of and , respectively.x=-4:1/100:4;y=-4:1/100:4;z=9(1-x)2*exp(-x2/2-(y+1)2)mesh(x,y,z);surf(x,y,z);得分 四综合编程题(每小题11分,共22分) 1. Write a function program to compute the following functionand call this function program to compute the value of in main program or command window, there, shows the factorial (阶乘) of . 提示:对x取整的函数有fix(x), floor(x), round(x) 和ceil(x) .fuction y=f1(x) %f1.mif x-fix(x)= =0y=prod(1:abs(2*x);elsey=4*sin(2*x)+x1.8;y=f1(-4)+f1(3)-f1(14.37)2.Write a program to compute the sample mean,and the sample skewness (偏度) .for the following three case:(1) n and exist in Workspace of Matlab. It is no any limit;(2) n and are unknown variables. Please input x by keyboard one by one, and give a mark of end the data input. Examinees can use loop statement and length() function and input() function, but cannot use mean() function, std() function, and var() function directly (可以使用循环语句和length()函数、input()函数等进行编程,但不能直接使用mean()、std()和var()等函数). (1)s=0;for i=1:ns=s+x(i);endxbar=s/nm=0;for i=1:nm=m+(x(i)-xbar)3;endske=m/n(2)x(1)=input(请输入x );i=1;s=0;while(x(i)=a) s=s+x(i); i=i+1; x(i)=input(请输入x ,输a结束);endxbar=s/(i-1)m=0;for j=1:i-1m=m+(x(i)-xbar)3;endske=m/(i-1)一、填空题1、MATLAB常用操作界面包括 命令窗口 、工作空间窗口、 命令历史窗口 、当前目录窗口 、内存数组编辑器、M文件编辑/调试器、帮助导航/浏览器、图形窗口等。2、MATLAB中Inf或inf表示 无穷大 、NaN或nan表示 不是一个数 、nargout表示 函数输出变量数目 。3、工作空间浏览器主要用于内存变量的 查阅 、 保持 和 编辑 。4、MATLAB实现将全下标转换为单下标的指令为 sub2ind 、据单下标换算出全下标的指令为 ind2sub 。5、MATLAB中clf用于 清除图形窗口 、clc用于 清除指令窗口中显示内容 、clear用于 清除MATLAB工作空间中保存的变量 。二、简答题(每题5分,共20分) 1、简述MATLAB历史指令窗的主要作用。答:历史指令窗记录着用户在MATLAB指令窗中所输入过的所有指令。历史记录包括:每次开启MATLAB的时间,每次开启MATLAB后在指令窗中运行过的所有指令。应用功能有单行或多行指令的复制和运行、生成M文件等。2、简述MATLAB函数的基本结构。答:典型M函数文件的结构:函数申明行、H1行、在线帮助文本区、编写和修改记录、函数体。3、简述绘制二维图形的一般步骤。绘制二维图形的一般步骤为:曲线数据准备、选定图形窗及子图位置、调用二维曲线绘图指令、设置轴的范围、坐标分格线、图形注释、图形的精细操作。三、阅读程序并回答问题(每题4分,共28分)1、写出下列指令运行结果。A=zeros(2,4); A(:)=1:8; s=2 3 5;A(s)Sa=10 20 30A(s)=Saans = 2 3 5Sa = 10 20 30A = 1 20 30 7 10 4 6 8 2、写出下列指令运行结果。A=reshape(1:16,2,8) reshape(A,4,4) s=1 3 6 8 9 11 14 16;A(s)=0A = 1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16ans = 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16A = 0 0 5 7 0 0 13 15 2 4 0 0 10 12 0 03、写出下列指令运行结果。A=1,2;3,4; B=-1,-2;2,1;S=3;A.*Bans = -1 -4 6 4A*Bans = 3 0 5 -2S.*Aans = 3 6 9 12S*Bans =-3 -6 6 34、下面的函数主要完成什么功能? function f=factor(n)if n=a&ch=z); ch(k)=ch(k)-(a-A); char(ch) subch =ABc12revch =9gF65e4d321cBAans =ABC123D4E56FG9ans = 46、写出下列指令运行结果。A(1,1)=this is cell;A1,2=1 2 3;4 5 6;A2,1=1+2*i;A2,2=A1,21+(A1,21(1,1)+A1,21(2,2);celldisp(A)A1,1 = this is cellA2,1 = 1.0000 + 2.0000iA1,21 = 1 2 3 4 5 6A2,2 = 7 8 9 10 11 127、下面的程序完成功能是什么?t=0:pi/50:4*pi;y0=exp(-t/3);y=exp(-t/3).*sin(3*t);plot(t,y,-r,t,y0,:b,t,-y0,:b)xlabel(bfit t);ylabel(bfit y);grid on;绘制图形如下:四、编程题(32分)1、用命令来创建GUI并绘制方程y=ax2 +bx+c图形,需要显示绘图结果的坐标系窗口,还能输入a、b、c的值和x的取值范围,同时用命令设置所以对象的属性。2、在同一图上分别用红色实线和绿色虚线绘制y1=sin(x)和y2=cos(x)在区间0,4*pi的曲线,并用星号*标出两条曲线的交点以及建立图例。(5分)x=0:0.01:4*pi;y1=sin(x);y2=cos(x);z=find(abs(y1-y2)0.007);plot(x,y1,r.-,Linewidth,2,Markersize,1);axis(-1.5,15,-1.5,1.5);hold on plot (x,y2,g.:,Linewidth,2,Markersize,1);n=size(z);plot(x(z),y1(z),k*,Markersize,15);3、某商场对顾客所购买的商品实行打折销售,标准如下(商品价格用price来表示): price200 没有折扣 200price500 3%折扣 500price1000 5%折扣 1000price2500 8%折扣 2500price5000 10%折扣5000price 14%折扣输入所售商品的价格,求其实际销售价格。(5分)price=input(please input your price:);a=0;if price200 a=1;elseif price500 a=2;elseif price1000 a=3;elseif price2500 a=4;elseif price2,error(wrong);endif nargout1,error(wrong);endss=1;for t=1:n,ss=ss+(xt)./(2*t-1);s=ss;end zh(35/60,35)ans = 1.7677 2. 已知系统的框图如下图所示,请推导出从输入信号r(t)到输出信号y(t)的总系统模型。2.syms G1 G2 G3 G4 G5 G6 G7 H1 H2 H3 H4c1=feedback(G4,H4);c2=feedback(G3*G2,H2);c3=feedback(G5*c1,H3);G=feedback(G6+G7)*c3*c2*G1,H1)3. 已知单位负反馈控制系统的被控对象及控制器的传递函数分别为 , 试判断系统的稳定性,并用时域响应检验得出的结论。 s=tf(s) Transfer function:s Gs=16*(1+s/2.5)/s*(1+s/0.5)*(1+s/20) Transfer function: 6.4 s + 16-0.1 s3 + 2.05 s2 + s Gc=20*(s+1)*(s+2)/(s+1.5)*(s+23) Transfer function:20 s2 + 60 s + 40-s2 + 24.5 s + 34.5 GG=feedback(Gs*Gc,1) Transfer function: 128 s3 + 704 s2 + 1216 s + 640-0.1 s5 + 4.5 s4 + 182.7 s3 + 799.2 s2 + 1251 s + 640 eig(GG)ans = -20.1421 +34.9785i -20.1421 -34.9785i -1.8264 + 0.5997i -1.8264 - 0.5997i -1.0630 根都有负实部 则系统稳定 step(GG,1)4. 考虑简单的线性微分方程,且方程的初值为y(0)=1,y(0)=y”(0)=1/2,y(3)(0)=0.6,,请用Simulink搭建起系统的仿真模型,并绘制出仿真结果曲线。2.6 求的共轭转置。 x=4+8i 3+5i 2-7i 1+4i 7-5i;3+2i 7-6i 9+4i 3-9i 4+4i; xans = 4.0000 - 8.0000i 3.0000 - 2.0000i 3.0000 - 5.0000i 7.0000 + 6.0000i 2.0000 + 7.0000i 9.0000 - 4.0000i 1.0000 - 4.0000i 3.0000 + 9.0000i 7.0000 + 5.0000i 4.0000 - 4.0000i2.7 计算与的数组乘积。 a=6 9 3;2 7 5; b=2 4 1;4 6 8; a.*bans = 12 36 3 8 42 402.9 对于,如果,求解X。 A=4 9 2;7 6 4;3 5 7; B=37 26 28; X=ABX = -0.5118 4.0427 1.33182.10 已知:,分别计算a的数组平方和矩阵平方,并观察其结果。 a=1
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!