matlab上機(jī)習(xí)題詳解 試題答案_第1頁
matlab上機(jī)習(xí)題詳解 試題答案_第2頁
matlab上機(jī)習(xí)題詳解 試題答案_第3頁
matlab上機(jī)習(xí)題詳解 試題答案_第4頁
matlab上機(jī)習(xí)題詳解 試題答案_第5頁
已閱讀5頁,還剩32頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第一次實(shí)驗(yàn)答案 1 設(shè)要求以0.01秒為間隔,求出y的151個(gè)點(diǎn),并求出其導(dǎo)數(shù)的值和曲線。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繪制極坐標(biāo)系下曲線(a,b,n自定數(shù)據(jù))clccleara=10;b=pi/2;n=5;theta=0:pi/100:2*pi;rho=a*cos(b+n*theta);polar(theta,rho)3. 列出求下列空間曲面交線的程序clcclea

2、rx=-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、編寫一個(gè)學(xué)生成績管理的程序,完成下列功能:(1) 構(gòu)造一個(gè)學(xué)生結(jié)構(gòu),包括學(xué)生的姓名,課程名稱(限m門),考試成績和平均值等域; (2

3、) 從鍵盤上,連續(xù)輸入n個(gè)學(xué)生的情況,分別將這些學(xué)生的情況填入到相應(yīng)的域,若域中沒有值即為空或0,并分別計(jì)算其平均值寫入到其平均值域。(3) 根據(jù)平均值排序(由高到低),并輸出學(xué)生的姓名和平均值。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(pleas

4、e 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

5、=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=

6、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),同時(shí)對曲線進(jìn)行標(biāo)注和修飾。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,函數(shù)=2

7、*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,.

8、7,facecolor,red)第三次試驗(yàn)答案1、做一個(gè)帶按鈕的界面,當(dāng)按動(dòng)“播放”按鈕時(shí)調(diào)入聲音文件并播放,顯示聲音波形,并建立一個(gè)用于關(guān)閉界面的按鈕對象。(提示,找一個(gè).wav文件,簡單起見可以在windows目錄下找一個(gè)文件,將其放在當(dāng)前工作目錄下或搜索路徑上。具體用法請參照:y,f,b=wavread(*.wav); % 讀入聲音文件sound(y,f,b) % 由聲卡播放聲音plot(y) % 畫出波形2、創(chuàng)建一個(gè)用于繪圖參數(shù)選擇的菜單對象,其中包含三個(gè)選項(xiàng)linestyle、marker和color,每個(gè)選項(xiàng)下面又包含若干的子項(xiàng)分別可以進(jìn)行選擇圖線的類型、標(biāo)記點(diǎn)的類型和顏色 (每個(gè)

9、子項(xiàng)不少于3個(gè)),當(dāng)按下“繪圖”按鈕時(shí),根據(jù)選項(xiàng)繪制正弦曲線(缺省時(shí)為藍(lán)色無標(biāo)記實(shí)線)。(注意使用全球變量)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

10、, . 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 ed

11、it % - 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 u

12、ser 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);gl

13、obal 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 fut

14、ure 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 def

15、ined 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 handl

16、es 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(ho

17、bject, 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 (

18、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 v

19、ersion 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 d

20、ata (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_callba

21、ck(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

22、_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 futu

23、re 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

24、 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

25、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=

26、strcat(l,m,c);plot(x,y,s)2. 設(shè)和是兩個(gè)10行10列的矩陣(數(shù)組),試說明命令a*b, a/b, ab, a.*(b.a), a./b, a.b和sin(a).*exp(b)的涵義。a,b的矩陣乘法,矩陣a乘以b的逆,矩陣b乘以a的逆,b的每個(gè)元素的對應(yīng)a次冪乘以a對應(yīng)的每個(gè)元素a的每個(gè)元素除以b對應(yīng)的每個(gè)元素,b的每個(gè)元素除以a對應(yīng)的每個(gè)元素,a的每個(gè)元素的正弦與b對應(yīng)的每個(gè)元素的指數(shù)求積,3. 設(shè)a為數(shù)組,b為一個(gè)行數(shù)大于3的數(shù)組,請給出(1)刪除a的第4、8、12三列的命令; (2)刪除b的倒數(shù)第3行的命令;(3)求符號極限的命令集; (4)求的3階導(dǎo)數(shù)的命令集

27、;(5)計(jì)算定積分的命令,并給出計(jì)算結(jié)果(!)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 wo

28、rkspace.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 o

29、f 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

30、*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 fu

31、nctionand call this function program to compute the value of in main program or command window, there, shows the factorial (階乘) of . 提示:對x取整的函數(shù)有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 t

32、o 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()

33、 function and input() function, but cannot use mean() function, std() function, and var() function directly (可以使用循環(huán)語句和length()函數(shù)、input()函數(shù)等進(jìn)行編程,但不能直接使用mean()、std()和var()等函數(shù)). (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(

34、i); i=i+1; x(i)=input(請輸入x ,輸a結(jié)束);endxbar=s/(i-1)m=0;for j=1:i-1m=m+(x(i)-xbar)3;endske=m/(i-1)一、填空題1、matlab常用操作界面包括 命令窗口 、工作空間窗口、 命令歷史窗口 、當(dāng)前目錄窗口 、內(nèi)存數(shù)組編輯器、m文件編輯/調(diào)試器、幫助導(dǎo)航/瀏覽器、圖形窗口等。2、matlab中inf或inf表示 無窮大 、nan或nan表示 不是一個(gè)數(shù) 、nargout表示 函數(shù)輸出變量數(shù)目 。3、工作空間瀏覽器主要用于內(nèi)存變量的 查閱 、 保持 和 編輯 。4、matlab實(shí)現(xiàn)將全下標(biāo)轉(zhuǎn)換為單下標(biāo)的指令為 s

35、ub2ind 、據(jù)單下標(biāo)換算出全下標(biāo)的指令為 ind2sub 。5、matlab中clf用于 清除圖形窗口 、clc用于 清除指令窗口中顯示內(nèi)容 、clear用于 清除matlab工作空間中保存的變量 。二、簡答題(每題5分,共20分) 1、簡述matlab歷史指令窗的主要作用。答:歷史指令窗記錄著用戶在matlab指令窗中所輸入過的所有指令。歷史記錄包括:每次開啟matlab的時(shí)間,每次開啟matlab后在指令窗中運(yùn)行過的所有指令。應(yīng)用功能有單行或多行指令的復(fù)制和運(yùn)行、生成m文件等。2、簡述matlab函數(shù)的基本結(jié)構(gòu)。答:典型m函數(shù)文件的結(jié)構(gòu):函數(shù)申明行、h1行、在線幫助文本區(qū)、編寫和修改記

36、錄、函數(shù)體。3、簡述繪制二維圖形的一般步驟。繪制二維圖形的一般步驟為:曲線數(shù)據(jù)準(zhǔn)備、選定圖形窗及子圖位置、調(diào)用二維曲線繪圖指令、設(shè)置軸的范圍、坐標(biāo)分格線、圖形注釋、圖形的精細(xì)操作。三、閱讀程序并回答問題(每題4分,共28分)1、寫出下列指令運(yùn)行結(jié)果。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、寫出下列指令運(yùn)行結(jié)果。a=reshape(1:16,2,8) reshape(a,4,4) s=1 3 6 8 9 11 14 16;a(s)=

37、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、寫出下列指令運(yùn)行結(jié)果。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、下面的函數(shù)主要完成什么功能? function f=factor(n)if n=a&ch=z); ch(k)=ch(k)-

38、(a-a); char(ch) subch =abc12revch =9gf65e4d321cbaans =abc123d4e56fg9ans = 46、寫出下列指令運(yùn)行結(jié)果。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(

39、-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、用命令來創(chuàng)建gui并繪制方程y=ax2 +bx+c圖形,需要顯示繪圖結(jié)果的坐標(biāo)系窗口,還能輸入a、b、c的值和x的取值范圍,同時(shí)用命令設(shè)置所以對象的屬性。2、在同一圖上分別用紅色實(shí)線和綠色虛線繪制y1=sin(x)和y2=cos(x)在區(qū)間0,4*pi的曲線,并用星號*標(biāo)出兩條曲線的交點(diǎn)以及建立圖例。(5分)x=0:0.01:4*pi;y1=sin(x);y2=cos(

40、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、某商場對顧客所購買的商品實(shí)行打折銷售,標(biāo)準(zhǔn)如下(商品價(jià)格用price來表示): price200 沒有折扣 200price500 3%折扣 500price1000 5%折扣 1000price2500 8%折扣 2500price5000 10%折扣5000price 14%折扣輸入所售商品的價(jià)格,求其實(shí)際銷售價(jià)格。(5分)price=input(please input your price:);a=0;if price200 a=1;elseif price500 a=2;elseif price1000

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論