matlab總練習(xí)題完整_第1頁
matlab總練習(xí)題完整_第2頁
matlab總練習(xí)題完整_第3頁
matlab總練習(xí)題完整_第4頁
matlab總練習(xí)題完整_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、>> vpa('pi',20)ans =3.1415926535897932385>> vpa('exp(1)',20)ans =2.7182818284590452354>> x=linspace(-pi,pi,21);>> y=sin(x)y = Columns 1 through 6 -0.0000 -0.3090 -0.5878 -0.8090 -0.9511 -1.0000 Columns 7 through 12 -0.9511 -0.8090 -0.5878 -0.3090 0 0.3090 Colu

2、mns 13 through 18 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 19 through 210.5878 0.3090 0.0000å ->> k=1:1000;>> kk=1./k;>> kkk=kk./k;>> res=sum(kkk)-(pi2)/6res =-9.9950e-04%承接上題>> sum(kk)-log(1000)ans =0.5777>> power(1+eps,1/eps)ans =2.7183>> a=r

3、and(2,3)a = 0.8147 0.1270 0.63240.9058 0.9134 0.0975>> x=a(1,:)x =0.8147 0.1270 0.6324>> y=a(2,:)y =0.9058 0.9134 0.0975>> norm(x)ans = 1.0391>> norm(y)ans =1.2900>> acos(dot(x,y)/norm(x)/norm(y)ans = 0.8189>> rand(3,3)ans = 0.2785 0.9649 0.9572 0.5469 0.1576 0.48

4、54 0.9575 0.9706 0.8003>> det(ans)ans =0.2937線性無關(guān)a = 0.3922 0.7060 0.6555 0.0318 0.1712 0.2769>> x=a(1,:)x = 0.3922 0.7060>> y=a(2,:)y = 0.6555 0.0318>> z=a(3,:)z = 0.1712 0.2769>> alpha=x-zalpha = 0.2210 0.4291>> beta=y-zbeta =0.4843 -0.2451>> alpha=alpha 0

5、alpha = 0.2210 0.4291 0>> beta=beta 0beta = 0.4843 -0.2451 0>> cross(alpha,beta)ans = 0 0 -0.2620面積0.2620>> a=11:19;>> b=a;>> for k=1:8b=b;a+10*k;end>> rank(b)ans = 2>> a=vander(1:9);>> b=fliplr(a)b = Columns 1 through 5 1 1 1 1 1 1 2 4 8 16 1 3 9 27 8

6、1 1 4 16 64 256 1 5 25 125 625 1 6 36 216 1296 1 7 49 343 2401 1 8 64 512 4096 1 9 81 729 6561 Columns 6 through 9 1 1 1 1 32 64 128 256 243 729 2187 6561 1024 4096 16384 65536 3125 15625 78125 390625 7776 46656 279936 1679616 16807 117649 823543 5764801 32768 262144 2097152 16777216 59049 531441 47

7、82969 43046721>> det(b)ans = 5.0566e+15方式一>> f=(x,y) exp(x+y)+sin(x2)+(y2)f = (x,y)exp(x+y)+sin(x2)+(y2)>> f(1,2)ans = 19.1266方式二function f=myfunfun(x,y)f=exp(x+y)+sin(x2)+(y2);>> myfunfun(1,2)ans = 19.1266>> Char1.4142135623730950488016887242096980785696718753769480731

8、766797379907324784621070388503875343276415727>> a=ans;>> sqrt2char(3-2)=a(3)sqrt2char =4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727>> for x=1:100b(x)=str2num(sqrt2char(x)end>> sum(b)/100ans = 4.8100f=(x) (x3)*sin(x)+(x2)

9、/3+x*cos(x)f = (x)(x3)*sin(x)+(x2)/3+x*cos(x)>> ezplot(f,-2,1)>> x0=fzero(f,-1)x0 = -0.7889另一根為0,是顯然的function y=difun(x)if x<-pi y=-x-pi;elseif x>-pi & x<pi y=sin(x);else y=(x-pi)/2;endend>> y=y = >> for x=-6:0.05:6y=y difun(x);end>> plot(x,y)>> plot(

10、-6:0.05:6,y)>> pi/4ans = 0.7854%pi/4的理想值矩形公式:function y=rectangle(n)x=0:1/n:1;a=1./(1+x.*x);y=sum(a)*(1/n);end>> rectangle(1000)ans = 0.7861>> rectangle(10000)ans = 0.7855>> rectangle(100000)ans =0.7854梯形公式:function y=trapezoid(n)x=0:1/n:1;a=1./(1+x.*x);begin=a(1);endd=a(n+1)

11、;a(1)=0;a(n)=0;y=sum(a)*(1/n)+begin*(1/n)*0.5+endd*(1/n)*0.5;endtrapezoid(1000)ans = 0.7854>> trapezoid(100)ans = 0.7853Simpson 公式function y=simpson(n)x=0:1/n:1;a=thefun(x);begin=a(1);endd=a(n+1);medium=;for x=1:n medium=medium (a(x)+a(x+1)*0.5;enda(1)=0;a(n)=0;y=begin*(1/n)*(1/6)+endd*(1/n)*(

12、1/6)+sum(a)*(1/n)*(1/3)+sum(medium)*(1/n)*(1/6)*4;endfunction e=thefun(r)e=1./(1+r.*r);end>> simpson(100)ans = 0.7854>> simpson(10)ans =0.7832>> A=6 2 1 -1;2 4 1 0;1 1 4 -1;-1 0 -1 3;>> b=6 1 5 -5'>> x=Abx = 0.7906 -0.3613 0.8639 -1.1152>> diag(1:4) eye(4)ans

13、= 1 0 0 0 1 0 0 0 0 2 0 0 0 1 0 0 0 0 3 0 0 0 1 0 0 0 0 4 0 0 0 1function yh=yhsj(n)yh=1; disp(1);for k=2:n yh=yh,0+0,yh; disp(yh)endend% n=11運(yùn)行>> x=sym('x');>> f=sqrt(1+(4/9)*x(1/2)2)f =(16*x)/81 + 1)(1/2)>> a=sym('a');>> b=sym('b');>> int(f,a,b

14、)ans =(16*b + 81)(3/2)/216 - (16*a + 81)(3/2)/216>> t=sym('t');>> a=sym('a');>> x=a*(t-sin(t)x =a*(t - sin(t)>> y=a*(1-cos(t)y =-a*(cos(t) - 1)>> dx=diff(x)dx =-a*(cos(t) - 1)>> dy=diff(y)dy =a*sin(t)>> int(sqrt(dx2+dy2),0,2*pi)ans =8*(a2)(1/

15、2)>> p=polyfit(-pi -pi/2 0 pi/2 pi,0 -1 0 1 0,5)p = Columns 1 through 5 -0.0349 0.0000 0.3440 -0.0000 0 Column 6 -0.0000>> plot(-pi:pi/100:pi,polyval(p,-pi:pi/100:pi);>> hold on>> plot(-pi:pi/100:pi,sin(-pi:pi/100:pi);>>function a=num2p(n)strr=num2str(n);index=size(strr

16、);indexx=index(2);a=;for k=indexx-1:-1:0 a=a rem(fix(n/(10k),10);endend>> num2p(95489298494)ans = Columns 1 through 8 9 5 4 8 9 2 9 8 Columns 9 through 11 4 9 4function ppi=ttry(n)pointx=-1+2*rand(1,n);pointy=-1+2*rand(1,n);index=pointx.*pointx+pointy.*pointy;indexx=index<=1;ppi=4*sum(index

17、x)/n;end>> ttry(100)ans = 3.1200>> ttry(10000)ans = 3.1376function y=collatz(n)if n=1 y=1return;elseif rem(n,2)=0 n=n/2;else n=3*n+1;end n y=collatz(n);end>> collatz(12)n = 6n = 3n = 10n = 5n = 16n = 8n = 4n = 2n = 1y = 1>> f=(x) 1./(1+x.2)f = (x)1./(1+x.2)>> y=f(x);>

18、;> x=0:0.1:1;>> y=f(x);>> p=polyfit(x,y,5)p = Columns 1 through 5 -0.2372 0.3529 0.5071 -1.1343 0.0115 Column 6 0.9999>> intp=polyint(p)intp = Columns 1 through 5 -0.0395 0.0706 0.1268 -0.3781 0.0058 Columns 6 through 7 0.9999 0>> res1=polyval(intp,1)-polyval(intp,0)res1 =

19、 0.7854%這是插值擬合解>> atan(1)ans =0.7854%actan(1)公式解fid=fopen('C:陳民權(quán)的文檔2matlab作業(yè)pi_1m.txt','r')fid = 3>> b=fscanf(fid,'%s');>> fclose(fid)ans = 0>> b(1:55)=;編輯“givemerun.m”文件:y=1;n=zeros(1,10)flag=0;for x=1:1500000if y=1000001breakelseif b(x)='' &a

20、mp; flag=0 & isempty(str2num(b(x)=1 c=b(x); d=str2double(c); n(d+1)=n(d+1)+1; y=y+1;elseif b(x)='' flag=1;elseif b(x)='' flag=0;endendendn>> givemerun%運(yùn)行“givemerun”n = 0 0 0 0 0 0 0 0 0 0n =n = Columns 1 through 3 99959 99758 100026 Columns 4 through 6 100229 100230 100359

21、Columns 7 through 9 99548 99800 99985 Column 10 100106 %分別為0,1,2,8,9在前一百萬位出現(xiàn)的次>> sum(n)ans = 1000000>> cc=num2str(n)cc =99959 99758 100026 100229 100230 100359 99548 99800 99985 100106>> fopen('res.txt','w')ans = 6>> fprintf(6,cc)ans =78function f=veryfun(n)if

22、 n=6174returnelseindex(1)=(n-rem(n,1000)/1000;index(2)=fix(n/100)-index(1)*10;index(3)=rem(fix(n/10),10);index(4)=rem(n,10);index2=sort(index,'ascend');index3=sort(index,'descend');a=1000 100 10 1;max=index3*(a');min=index2*(a');max-minveryfun(max-min);endend>> veryfun(9864)ans = 5175ans = 5994ans = 5355ans = 1998ans = 8082ans = 8532ans = 6174function m=tem(ind

溫馨提示

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

評(píng)論

0/150

提交評(píng)論