matlab程序設(shè)計(第二版)課后作業(yè)答案_第1頁
matlab程序設(shè)計(第二版)課后作業(yè)答案_第2頁
matlab程序設(shè)計(第二版)課后作業(yè)答案_第3頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、Matlab 程序設(shè)計實驗報告Matlab 程序設(shè)計實驗報告 PAGE PAGE 9一、實驗結(jié)果:1、 利用函數(shù)函數(shù)分別產(chǎn)生首先編寫腳本函數(shù)如下: a1=eye(3) a2=eye(15,8) b1=ones(3) b2=ones(15,8) c1=zeros(3) c2=zeros(15,8) d1=2*rand(3,3)-1 d2=2*randn(3,3)+1 r1=2*rand(15,8)-1 r2=2*randn(15,8)+12、使用函數(shù)diag 和 reshape 函數(shù):編寫腳本:a1=8 -7 2;a2=diag(a1); a3=fliplr(a2); a4=5 3;a5=dia

2、g(a4,-1); a6=fliplr(a5); a=a3+a6 b1=4 57;b2=diag(b1); b4=2 08;b5=diag(b4); b=b3+b5 b6=reshape(b,1,9)3、直接在工作空間里輸入:a=5-round(100*rand(50,2)/10 4、腳本文件:y1=sin(a)y2=cos(a)5.、編寫腳本: A=rand(10) b=A0.6i,j,v=find(b) c=sum(v)6、編寫腳本:A=randn(10)b=-0.5A0.5i,j,v=find(b); c=sum(v)7、編寫實現(xiàn)表中功能的語句:if and(a1,b=0.5)語句1el

3、seif and(a0.5)語句2elseif and(a=1,b=1,b0.5)語句4 elsedisp(error)end8、編寫腳本如下: A=2*rand(3) b=A1c=A.*b; i,j,v=find(c); v9、編寫腳本函數(shù):A=101*rand(100,3) b=A100;c=A.*b; i,j,d=find(c); d10、為節(jié)約空間,用 5*5 的矩陣來模擬100*100 的矩陣:編寫腳本函數(shù)如下:A=0 2 0 3 0;0 0 0 0 0;0 8 0 5 0;04 0 5 0;0 4 0 20b=any(A); A(:,j)=; B=A;c=any(B); i,j,v

4、=find(1-c);B(:,j)=; A=B一、實驗結(jié)果:1 、 輸 入 腳 本 函 數(shù) : x=0:pi/50:2*pi; k=1 26 51 76101; x(k)=;figure(1) subplot(2,2,1) plot(x,sin(x),grid on subplot(2,2,2) plot(x,cos(x),grid on subplot(2,2,3) plot(x,tan(x),grid on subplot(2,2,4) plot(x,cot(x),grid on2、分別利用 title 函數(shù)給圖形加上標(biāo)題,用text 字符串放在圖形的指定位置,用 xlabel、ylabe

5、l 函數(shù)添加 、y legend x=0:0.1:2;y1=exp(x); figure(1); plot(x,y1);title(itx);xlabel(x);ylabel(ity=exp(x); text(0.6,-0.28,exp(x)rightarro w);legend(ity=exp(x); y2=log10(x); figure(2);plot(x,y2);title(itx);xlabel(x); ylabel(ity=log10(x); text(0.6,-0.28,log10(x)rightar row);legend(ity=log10(x);將腳本函數(shù)保存為xiti1.

6、m,運行后產(chǎn)生圖形如下:3、編寫腳本函數(shù)如下: x=0:pi/40:4*pi; figure(1); subplot(2,1,1);plot(x,sec(x),grid on;title(正割函數(shù)圖形); ylabel(y),xlabel(x); subplot(2,1,2);plot(x,csc(x),grid on;title(余割函數(shù)圖形); ylabel(y),xlabel(x); 4、編寫腳本函數(shù)如下: x=(1:.1:10);y=exp(x+5)+x.3; figure(1); semilogx(x,y,k-);title(y=e(x+5)+x.3);%此處加表示是寫在指數(shù)部分的字

7、符ylabel(y),xlabel(x);5、繪制多峰函數(shù)x,y=meshgrid(-3:.125:3); z=peaks(x,y); plot3(x,y,z),grid on;繪制多條三角函數(shù)曲線:腳本函數(shù)如下:x=0:pi/50:2*pi; figure(1)plot(x,y1,k-,x,y2,k-),g rid ontext(pi,0.05,leftarrow sin(alpha)text(pi/4-0.05,0.05,cos(al pha)rightarrow)title(sin(alpha) and cos(alpha)xlabel(alpha),ylabel(sin (alpha)

8、?cos(alpha)6使用axis調(diào)整坐標(biāo)軸的刻度編寫的腳本如下:x=-3:.1:3;y1=2*x+5; y2=x.2-3*x+1;subplot(2,1,1),plot(x,y1),grid on;axis(-3 3 -5 10)title(y1=2x+5) ylabel(y),xlabel(x) subplot(2,1,2),plot(x,y2),grid on;axis(-3 3 -510)title(x.2-3x+1)ylabel(y),xlabel(x)axis square二、實驗結(jié)果:1cool板的繪制都是相似的,編寫的腳本文件如 下:cmap=colormap; L=leng

9、th(cmap); x=1:L;y=x*ones(size(x); figure(1) bar(x(1:2),y(1:2,:);title(cool) colormap(cool)每次都改動colormap()中的依次為autumn,bone,cool,hot,hsv,gray, flag,line即可2,編寫腳本:x=190 33 45 4245;explode=0 1 0 00;labels=生活費,資料費,電話費,購買衣服,其他費用; figure(1)colormap gray pie(x,explode,labels)title(某研究生每月消費比例)3,編寫成一個腳本函數(shù)如下:x,

10、y=meshgrid(-3:.125:3)z=(x-2).2+(y-1.2).2figure(1) plot3(x,y,z),grid on figure(2) mesh(x,y,z),grid on 4、編寫成腳本文件:x,y=meshgrid(-3:.125:3);z=x.2+y.2+sin(x*y); c=ones(size(x); figure(1) surfc(x,y,z,c),grid on view(-15,25)figure(2) contour3(x,y,z,30),grid on view(-15,25)5、畫出球形:spherex=0.5*x y=0.5*y z=0.5*

11、zhold on surf(x,y,z) surf(x+1,y+1,z+1) 柱形:cylinderaxis square h=findobj(Type,surface); set(h,CData,rand(size(get(h,C Data)二、實驗結(jié)果:1、先編寫函數(shù)文件: function f=isprimnum(x) n=fix(sqrt(x);for i=2:nif rem(x,i)=0f=false;else;endend再編寫腳本文件:a=19b=isprimnum(a)2、編寫函數(shù)文件如下:function num,str=shanzifu(s) n=length(s);num

12、=0; k=0; z=;for i=1:nif s(i)= num=num+1; endif s(i)= k=k+1; z(k)=i;endends(z)=;str=s;編寫腳本運行:w=I am astudent! a b=shanzifu(w); ab運行解雇如下: w =I am a student! a =12b =Iamastudent!3function c=qiuling(A) c=0;for j=1:2006 s=num2str(A(j); n=length(s);for i=1:nif s(i)=0c=c+1;end end end寫腳本文件運行:A=1:2006;B=qiu

13、ling(A) 運行的結(jié)果是: B =5044、先編寫函數(shù)文件:function x1,x2=fangcheng(a,b,c) if a=0 x2=-c/b;x1=-c/b; returnendif b2-4*a*c0 x1=(-b+sqrt(b2-4*a*c)/2*a; x2=(-b-sqrt(b2-4*a*c)/2*a;disp(); endend編 寫 腳 本 函 數(shù) 運 行 : x1,x2=fangcheng(0,3,4) x3,x4=fangcheng(1,-2,1) x5,x6=fangcheng(2,3,1) x7,x8=fangcheng(2,1,1) 顯示的結(jié)果如下:只有一個

14、根!x1 =-1.3333x2 =-1.3333方程有兩個相等的根!x3 =1x4 =1方程有兩個不等的根!x5 =-2x6 =-4無實根!x7 =不存在實根x8 =不存在實根5、先編寫兩個函數(shù)文件function y=fenduan(x) if x =-3 & x=-1 & x= 1 & x =-3 & x(i)=-1 & x(i)=1 & x(i) chengji=pingjun(x)906789769877chengji =81.72413、編寫?yīng)劷鸬暮瘮?shù)文件function y=jiangjin(x) if x10&x20&x40y=(x-40)*0.01+20*0.02+10*0.0

15、5+10*0.1;end4、先編寫求前n 項和的函數(shù)文件: function y=qiuhe15(n) n=input(please input n:n); i=1;a(1)=1;a(2)=2;y(1)=0;for i=1:na(i+2)=a(i)+a(i+1);-2.9150-1.3152end-2.9298-2.9412fori=1:n2、計算加權(quán)平均成績的函數(shù)文件:function y=mean(x)h(i)=a(i+1)/a(i);for i=1:15 y=y+h(i); end5、這個問題屬于約瑟夫環(huán)的問題:編 寫 函 數(shù) 文 件 如 下 : function a=yuesefu(n

16、,m) b=1:n;k=0; m1=1; i=1;while m1 fzero at 293The function values at the interval endpoints must differ in sign.Error in = xiti18 at 3 z=fzero(hanshu1,x)出錯信息表示此區(qū)間內(nèi)沒有零點。19dblquad編寫函數(shù)文件如下:function out= shuang( x,y ) out=exp(-x*y-2*x*y);end腳 本 文 件 : d=dblquad(shuang,0,1,-1,1) 20,求微分方程組:編寫函數(shù)文件function d

17、y= diff2( t,y )dy=0.5-y(1);y(1)-4*y(2)end編寫腳本文件:X0=1;-0.5;tspan=0,25; T,X=ode45(diff2,tspan,X0); figure(1); subplot(3,1,1),plot(T,X(:,1),r),title(x_1),grid on;subplot(3,1,2),plot(T,X(:,2),k),title(x_2),grid on; subplot(3,1,3) plot(X(:,1),X(:,2),title( ),grid on; xlabel(x_1),ylabel(x_2)三、實驗結(jié)果:1, 腳 本 文 件 如 下 : a=rand(10,10,5);%隨機產(chǎn)生房間內(nèi)的各個點內(nèi)的溫度for i=1:1:5; disp(a(:,:,i);means=sum(a(:,:,i);%求一個平面內(nèi)陣列的一列的和mean(i)=sum(means)/100;%求一個平面內(nèi)所有溫度的平均值disp(mean(i);%將結(jié)果顯示出來end將腳本文件保存為fang.m保存后運行 2, 腳 本 語 言 輸 入

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論