matlab总练习题完整_第1页
matlab总练习题完整_第2页
matlab总练习题完整_第3页
matlab总练习题完整_第4页
matlab总练习题完整_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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线性无关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运行>> 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:陈民权的文档2matlab作业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%运行“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在前一百万位出现的次>> 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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论