matlab总练习题(完整版).doc
>> 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 Columns 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=rand(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.4854 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 0alpha = 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 81 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 4782969 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>> Char 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727>> 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)/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(-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);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)*(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 = 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)end end% 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) 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/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);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(indexx)/n;end>> ttry(100)ans = 3.1200>> ttry(10000)ans = 3.1376function y=collatz(n)if n=1 y=1 return;else if 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);>> 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 = 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:1500000 if y=1000001 breakelseif b(x)='' & 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 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 n=6174 returnelseindex(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(index,t)m=index(1).*exp(index(2).*t)end>> index,r,j,c,m,e=nlinfit(t,m,tem,0 0)index = 78.7137 -0.1047%在这里省略中间的迭代过程和表格变量输入过程>> m=tem(index,0:0.1:25);>> plot(0:0.1:25,m)function son=sonfind(A)index=sum(A)/2;for k=1:5 b=nchoosek(1:10,k); sizee=size(b); kk=sizee(1); for x=1:kk c=b(x,:); A_son=; for y=c A_son=A_son A(y); end if sum(A_son)=index son=A_son; break return end end end end%集合元素有互异性,但不影响验证!>> sonfind(11:20)>> sonfind(10 5 5 10 60 10 10 10 10 10)ans = 5 5 60>> sonfind(10 10 10 10 90 10 10 10 10 10)ans =90%进一步验证(这里元素互异)>> sonfind(21 22 23 24 25 11 12 13 14 65)ans = 21 22 23 24 25>> sort(21 22 23 24 25 11 12 13 14 65)ans = 11 12 13 14 21 22 23 24 25 65>> sonfind(ans)ans =11 12 13 14 652016 12