基于matlab的科學計算試驗_第1頁
基于matlab的科學計算試驗_第2頁
基于matlab的科學計算試驗_第3頁
基于matlab的科學計算試驗_第4頁
基于matlab的科學計算試驗_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、科學計算與數(shù)據(jù)處理實驗報告學 號S姓 名張園實驗名稱更于MATLAB的科學計算實驗實驗目的1、掌握 MATLAB2、掌握 MATLAB3、掌握 MATLAB中數(shù)組的創(chuàng)建和操作方法 中常用的數(shù)值計算方法 中常用的符號計算方法實驗方案一、一維數(shù)組創(chuàng)建實驗:(1)直接輸入法: test=1 2 3 4 test=1;2;3;4(2)步長生成法: test=1:0.5:10(3)定數(shù)線性采樣法: test = linspace(1,12,5)(4)定數(shù)對數(shù)采樣法: logspace(2,6,4)二、高維數(shù)組創(chuàng)建實驗:(1)直接輸入法:匕一 A=1 2 3;4 5 6;7 8 9(2)使用卜標: cle

2、ar,A(2,3,2)=1(3)使用低維數(shù)組:clear,A=eye(3,4);A(:,:,2)=eye(3,4)*2;A(:,:,3)=eye(3,4)*3;A(:,:,4)=eye(3,4)*4(4)使用創(chuàng)建函數(shù)(cat、repmat、reshape)創(chuàng)建高維數(shù)組: cat(3,1,2,3;4,5,6,eye(2,3)*2,ones(2,3) repmat(1,2;3,4,1,2,3) reshape(1:20,2,5,2)三、標準數(shù)組創(chuàng)建實驗:(1)全 0 矩陣: zeros(3)(2)全 1 矩陣: ones(5)(3)單位矩陣: eye(4)(4) magic 矩陣: magic(4

3、)(5)隨機矩陣: randn(4)四、矩陣變換實驗:令 Data=1,2,3,4;5,6,7,8;9,10,11,12,分別使用 diag、fliplr、flipud、rot90、tril、triu函數(shù)計算 Data的對角、轉(zhuǎn)置、翻轉(zhuǎn)、旋轉(zhuǎn)、 三角矩陣,具體命令如下: Data=1,2,3,4;5,6,7,8;9,10,11,12 diag(Data)(Data) fliplr(Data) flipud(Data) rot90(Data) tril(Data) triu(Data)五、字符串數(shù)組創(chuàng)建與操作實驗:(1)創(chuàng)建字符串數(shù)組: arr=str2mat(I,am,a,student)(2

4、)去掉字符串末尾的空格deblank::建立字符串,用abs函數(shù)驗證空格的存在;用 deblank去掉空格,用abs已經(jīng)去掉空格 x=a n ;y=abs(x) z=deblank(x);w=abs(z)(3)刪除字符串開頭和結(jié)尾的空格strtrim str1= I am a student ; str2=I am a student; x=strtrim(str1) y=strtrim(str2)(4)執(zhí)行簡單的字符串替代strrep、 str1=I am a student.; str2=student; str3=teacher; str=strrep(str1,str2,str3)(5

5、)規(guī)范格式strread; strread(0.231,%5.3f)(6)函數(shù)strtok找出由特定字符指定的字符串內(nèi)的標記; ar=I am a student strtok(ar,s)六、架構(gòu)數(shù)組的創(chuàng)建與操作實驗:直接創(chuàng)建法: clear x; x.real = 1 2 3 4 5; x.imag = ones(4)命令(struct)創(chuàng)建法 s = struct(name,x,y,id,3,4,w,3,4)Fieldnames 函數(shù): fieldnames(s)Getfield 函數(shù): str(1,1).name = x; str(1,1).ID = 5; str(2,1).name =

6、 y; str(2,1).ID = 3; result = getfield(str, 2,1, name)Setfield 函數(shù): str(1,1).name = x; str(1,1).ID = 5; str(2,1).name = y; str(2,1).ID = 3; str= setfield(str,2,1,name,a); str(2,1).name七、基本運算符號實驗:(1)矩陣加: a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a+b(2)矩陣減: a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a-b

7、(3)矩陣乘 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a*b(4)數(shù)組乘 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a.*b(5)矩陣乘方 a=1,2,3;4,5,6;7,8,9; aA2(6)數(shù)組乘方 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a.Ab(7)矩陣左除 a=1,2,3;4,5,6;7,8,9; b=2;4;6;ab(8)矩陣右除 a=ones(3); b=1,1,1; a/b(9)數(shù)組左除 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,

8、2,3;2,4,6; a.b(10)數(shù)組右除 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a./b(11)克羅內(nèi)克張量積 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;kron(a,b)(12)邏輯與 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;a&b(13)邏輯或 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;a|b(14)邏輯非 a=1,0,1;1,1,1;1,0,1; -a(15)邏輯異或 a=1,0,1;1,1,1;1,0,1; b=0,

9、0,1;1,0,1;0,0,1;xor(a,b)八、矩陣分析實驗:(1)范數(shù)(norm): norm(a,1) cond(a)(3)行列式(det): det(a)(4)秩(rank): rank(a)(5)特征值(eig): eig(a) a=pascal(3)(1)導數(shù)(diff): a=xA3+4*xA2-x+20 a=1,2,3;4,5,6;7,8,9; norm(a,2)(2)條件數(shù)(cond): V,D=eig(a)(6)化零矩陣(null) Z=null(a)Cholesky 分解( a=pascal(3);chol(a)LU 分解(lu) a=1,2,3;4,5,6;7,8,9

10、; L1,U1=lu(a)(9)正交分解(qr) a=1,2,3;4,5,6;7,8,9; U,S=qr(a)(10)奇異值分解(svd): a=1,2,3;4,5,6;7,8,9; U,S,V=svd(a)九、數(shù)值計算實驗: diff(a)(2)梯度(gradient) a=1,2,3;4,5,6;7,8,9; fx,fy=gradient(a)(3)多項式求根(roots)、 p=1,3,2,5;px=poly2str(p,x);r=roots(p)/ p是多項式的MATLAB描述方法,我們可用poly2str(p,x)函數(shù),來顯示多項式的形式,px =xA3 +3 xA2 +2 x +5

11、 (4)零點(fzero、fsolve): a=(x)xA2+3*x+2; x=fzero(a,0) x=fsolve(xA2+3*x+2,0)(5)極值(fminbnd、fminsearch、fminunc)、 1; f=(x) xA2-4*x+5; fminbnd(f,0,1) 2; fun=inline(x(1)A2-3*x(1)*x(2)+2*x(2)A2);x0=1,1; fminsearch(fun,x0)3; fun=inline(x(1)A2-3*x(1)*x(2)+2*x(2)A2);x0=1,1; fminunc(fun,x0)(6)積分(quadl)用內(nèi)聯(lián)函數(shù)定義被積函數(shù)

12、: fun=inline(-x.*x,x); y=quadl(fun,0,1)十、符號計算實驗:(1)將cosx + J-sin2 x化簡:先用syms定義符號變量,再用 simplify函數(shù)進行化簡,具體命令如下: simplify(cos(x)+sqrt(-sin(x)A2)(2)求(x+2)x=2的解:用solve函數(shù)求解,命令如下:x=solve(x+2)Ax=2,x)實驗記錄 test=1 2 3 4 test =1234 test=1;2;3;4test =1234(2),. test=1:0.5:10test =Columns 1 through 41.00001.50002.0

13、0002.5000Columns 5 through 83.00003.50004.00004.5000Columns 9 through 125.00005.50006.00006.5000Columns 13 through 167.00007.50008.00008.5000Columns 17 through 199.00009.500010.0000(3) test = linspace(1,12,5)test =Columns 1 through 41.00003.75006.50009.2500Column 512.0000(4) logspace(2,6,4)ans =1.0e

14、+006 *0.00010.00220.04641.0000二、 (1)直接輸入法 A=1 2 3;4 5 6;7 8 9 A =123456789(2)使用卜標 clear,A(2,3,2)=1 A(:,:,1)=000000A(:,:,2)=000001(3)使用低維數(shù)組 :.-clear,A=eye(3,4);A(:,:,2)=eye(3,4)*2;A(:,:,3)=eye(3,4)*3;A(:,:,4)=eye(3,4)*4 A(:,:,1)= 100001000010A(:,:,2)= 200002000020A(:,:,3)= 300003000030A(:,:,4)= 40000

15、4000040(4)使用創(chuàng)建函數(shù)(cat、repmat、reshape)創(chuàng)建高維數(shù)組。1, cat(3,1,2,3;4,5,6,eye(2,3)*2,ones(2,3) ans(:,:,1)=123456ans(:,:,2)=200020ans(:,:,3)=1111112, repmat(1,2;3,4,1,2,3) ans(:,:,1)=12123434ans(:,:,2)=12123434ans(:,:,3)=121234343, reshape(1:20,2,5,2)ans(:,:,1)ns(:,:,2)=11131517191214161820三、zeros

16、(3)ans =000000000(2)ones(5)ans =1111111111111111111111111eye(4)ans =1000010000100001(4)magic(4)ans =16231351110897612414151 randn(4)ans =1.06680.2944-0.6918-1.44100.0593-1.33620.85800.5711-0.09560.71431.2540-0.3999-0.83231.6236-1.59370.6900四、 Data=1,2,3,4;5,6,7,8;9,10,11,12Data =12345678910 diag(Dat

17、a) ans =1611(2)(Data)ans =111215926103711-J48 fliplr(Data) ans =12432187651211(4) flipud(Data) ans =1099101112567812 rot90(Data) ans =3448123711261015(6) tril(Data)9ans =10005600910110 triu(Data)ans =12340678001112五、 arr=str2mat(I,am,a,student) arr =Iam astudent x=a n ;y=abs(x)y =973211032 z=deblank

18、(x);w=abs(z) w =9732110 str1=I am a student ; str2=I am a student ; x=strtrim(str1)x =I am a student y=strtrim(str2) y =I am a student str1=I am a student.; str2=student; str3=teacher; str=strrep(str1,str2,str3) str =I am a teacher. strread(0.231,%5.3f) ans =0.2310 ar=I am a studentar =I am a studen

19、t strtok(ar,s)ans =I am a六;(1)直接法 clear x; x.real = 1 2 3 4 5; x.imag = ones(4) x =real: 1 2 3 4 5imag: 4x4 double(2)struct 函數(shù) s = struct(name,x,y,id,3,4,w,3,4) s =1x2 struct array with fields:nameidfieldnames 功能演示 fieldnames(s) ans =nameidgetfield功能演示 str(1,1).name = x; str(1,1).ID = 5; str(2,1).na

20、me = y; str(2,1).ID = 3; result = getfield(str, 2,1, name) result =ysetfield功能演示 str(1,1).name = x; str(1,1).ID = 5; str(2,1).name = y; str(2,1).ID = 3; str= setfield(str,2,1,name,a); str(2,1).nameans =a七:(1)矩陣加 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a+bans = TOC o 1-5 h z 812791215(2)矩陣減 a=1,2,3

21、;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a-bans = TOC o 1-5 h z -2-4-6333543(3)矩陣乘 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a*bans = TOC o 1-5 h z 1122332958874794141(4)數(shù)組乘 a=1,2,3;4,5,6;7,8,9;L-./ jr y| 產(chǎn) b=3,6,9;1,2,3;2,4,6; a.*bans =12271018143254(5)矩陣乘方 a=1,2,3;4,5,6;7,8,9; aA2ans = TOC o 1-5 h z 303

226)數(shù)組乘方 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a.Abans =16419683425216494096531441(7)矩陣左除 a=1,2,3;4,5,6;7,8,9; b=2;4;6;abWarning: Matrix is close to singular or badly scaled.Results may be inaccurate. RCOND = 2.203039e-018.ans =-0.46670.93350.1999(8)矩陣右除 a=ones(3); b=1,1,1; a/ba

23、ns =111(9)數(shù)組左除 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a.bans =3.00003.00003.0000.0.25000.40000.50001 10.28570.50000.6667- 1-I-(10)數(shù)組右除 a=1,2,3;4,5,6;7,8,9; b=3,6,9;1,2,3;2,4,6; a./bans =0.33330.33330.33334.00002.50002.00003.50002.00001.5000(11)克羅內(nèi)克張量積 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;kron

24、(a,b) ans =001000001101000101001000001001001001101101101001001001001000001101000101001000001(12)邏輯與 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;a&b ans =0010100(13)邏輯或 a=1,0,1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;a|b ans = TOC o 1-5 h z 101111101(14)邏輯非 a=1,0,1;1,1,1;1,0,1; -a ans =010000010(15)邏輯異或 a=1,0,

25、1;1,1,1;1,0,1; b=0,0,1;1,0,1;0,0,1;xor(a,b)ans = TOC o 1-5 h z 00,丈11r / y . i010I ./ J _/ . jf f /00八、(1)范數(shù)(norm)用來度量矩陣或者向量在某種意義下的長度 a=1,2,3;4,5,6;7,8,9; norm(a,1) ans =18 norm(a,2)ans =16.8481(2)條件數(shù)(cond)可以描述矩陣為良性矩陣還是病態(tài)矩陣的一個參數(shù) cond(a)ans =5.0524e+016(3)行歹U式(det) det(a)ans =0(4)秩(rank) rank(a) ans

26、=2(5)特征值(eig) eig(a) ans =16.1168-1.1168-0.0000 V,D=eig(a) V = -0.2320-0.78580.4082-0.5253-0.0868-0.8165-0.81870.61230.4082D =16.1168000-1.1168000-0.0000(6)化零矩陣(null) Z=null(a) Z = -0.4082 0.8165 -0.4082(7) Cholesky分解(chol)把對稱正定矩陣表示成上三角矩陣的轉(zhuǎn)置與其本身的乘積a=pascal(3)/pascal 矩陣a =111123136a=pascal(3);chol(a)

27、ans =111012001(8) LU分解(lu)a=1,2,3;4,5,6;7,8,9;L1,U1=lu(a)L1=10.14291.0000010.57140.50001.00001.000000U1=7.00008.00009.000000.85711.714300-0.0000(9)正交分解(qr) a=1,2,3;4,5,6;7,8,9; U,S=qr(a)U =-0.12310.90450.4082-0.49240.3015-0.8165-0.8616-0.30150.4082S =-8.1240-9.6011 -11.078200.90451.809100-0.0000(10)

28、奇異值分解(svd) a=1,2,3;4,5,6;7,8,9; U,S,V=svd(a)U =-0.21480.88720.4082-0.52060.2496-0.8165-0.8263-0.38790.4082S =16.84810001.06840000.0000V =0.4797-0.7767-0.40820.5724-0.07570.81650.66510.6253-0.4082九(1)導數(shù)/ J/ : 5 /.r r V- J . J r 產(chǎn). J 卡I a=xA3+4*xA2-x+20 a =xA3+4*xA2-x+20 diff(a) ans =3*xA2+8*x-1 diff(

29、a,2) ans =6*x+8(2)梯度 a=1,2,3;4,5,6;7,8,9; fx,fy=gradient(a) fx = TOC o 1-5 h z 111111111fy =333333333(3)多項式求根 p=1,3,2,5;px=poly2str(p,x);r=roots(p)/ p是多項式的MATLAB描述方法,我們可用 poly2str(p,x)函數(shù),來顯示 多項式的形式,px =xA3 +3 xA2 +2 x +5r =-2303/793-205/4278+ 851/649i-205/4278- 851/649i a=(x)xA2+3*x+2; x=fzero(a,0)-

30、1 x=fsolve(xA2+3*x+2,0)Optimization terminated: first-order optimality is less than options.TolFun.-1(5)極值1; f=(x) xA2-4*x+5; fminbnd(f,0,1)ans =0.99992;fun=inline(x(1)A2-3*x(1)*x(2)+2*x(2)A2);x0=1,1;fminsearch(fun,x0)Exiting: Maximum number of function evaluations has been exceededans =1.0e+042 *1.89461.40903; fun=

溫馨提示

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

評論

0/150

提交評論