1、matlab总练习题完整版K显示20位有效数字鸡卫的近似值; vpa(pi,20)ans =3.85 vpa(exp(1),20)ans =2.542.将区间-码卸20等分,并以行向量形式给岀正弦函数在这些节点上的函数值; x=li nspace(_pi,pi,21); y=si n(x)Columns 1 through 6-0.0000 -0.3090 -0.5878 -0.8090 -0.9511 -1.0000Columns 7 through 120.3090-0.9511 -0.8090 -0.5878 -0.3090Colu mns 13 through 180.5878 0.8
2、090 0.9511 1.0000 0.9511 0.8090Colu mns 19 through 210.5878 0.3090 0.0000 k=1:1000; kk=1./k; kkk=kk./k; res=sum(kkk)-(piA2)/ 6 res =-9.9950e-04iwu 14”计算7-ln(l(KK的近似值(无带度要求;%承接上题 sum(kk)-log(1000) ans =0.57775.计算极限+ power(1+eps,1/eps) ans =2.7183&计算; (MuPad命令7.生成两亍用中的随机向量并计养它们的内积,向境长:度,问量之间的夹角; a=ra
3、nd(2,3)0.81470.12700.63240.90580.91340.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&生成三个疋中的随机向屋仏拭5 判斷它们是否线性无关; ran d(3,3)ans =0.27850.96490.95720.54690.15760.48540.95750.97060.8003 det(a ns) ans =0
4、.2937线性无关g.生成三个f中的随机三个点匸,计算它们构成的三角形的面积;a =0.3922 0.70600.65550.0318 x=a(1,:)0.39220.7060 y=a(2,:)0.65550.0318 z=a(3,:)0.17120.2769 alpha=x-z alpha =0.22100.4291 beta=y-z beta =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.2620面积 0.26
5、20%)213IF2122232910.建立矩阵貝二31323339a B v ,并计臬该矩阵的秩;929399丿 a=11:19; b=a; for k=1:8 b=b;a+10*k; end ran k(b) ans =IL it立 Vandermonde 柜阵片二1 11 1 2 Z22*1 3 323总: - . J 9少d,并计算该矩阵形城的行 a=va nder(1:9); b=fliplr(a)Columns 1 through 5111111248161392781141664256152512562516362161296174934324011864512409619817
6、296561Columns 6 through 911113264128256243729218765611024409616384655363125156257812539062577764665627993616796161680711764982354357648013276826214420971521677721659049531441478296943046721 det(b) ans =5.0566e+1512-用两种不同的方式定义二元函数/乩“二严刃+血井在相应览 义形式下计算AU);方式一 f=(x,y) exp(x+y)+sin(x2)+(yA2)(x,y)exp(x+y)
7、+si n( (x2)+(yA2) f(1,2) ans =19.1266方式二fun cti on f=myfu nfun( x,y)f=exp(x+y)+si n( (xA2)+(yA2); myfu nfun (1,2) ans =13-血的辺0位小数近似值为:1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727;Char 1.5727 a=a ns; sqrt2char(3-2)=a(3)sqrt2char =5727 for x=1:
8、100b(x)=str2 nu m(sqrt2char(x)end sum(b)/100ans =14-出函数几0心血何+,/3 +开血(盂在区间卜即上的图象.并计算岀该区间上的两个零点;f=(x) (xA3)*sin(x)+(x2)/ 3+x*cos(x) (x)(x3)*sin(x)+(xA2)/ 3+x*cos(x) x0=fzero(f,-1)x0 = ezplot(f,-2,1)Rh EditVnv Imvct TddIDnktep* J dk x r? 5 1 到 n另一根为0,是显然的-X - ZT. X -JT15. Pi出分段函数/h鈕(鼻),-圧3 nfun cti on
9、y=difu n(x)if xv-piy=-x-pi;elseif x-pi & x y= for x=-6:0.05:6y=y difu n( x);end plot(x,y) plot(-6:0.05:6,y)16.分别用矩形公式片梯形公式Simpson公式计算积分(右点的近似值,并以这些近似値计算岀汙的近似值; pi/4 ans =0.7854 %pi/4的理想值矩形公式:fun cti on y=recta ngle (n)x=0:1/ n:1;a=1./(1+x.*x);y=sum(a)*(1/ n);end recta ngle(1000)ans =0.7861 rectangle
10、(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
11、);endd=a(n+1);medium=;for x=1:nmedium=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 simpso n(100)ans =0.7854 simps on (10) ans =0.783217.求解线性方程组心=虬其中6224110A :=211445103. A=6 2 1 -1
12、;2 4 1 0;1 1 4 -1;-1 0 -1 3; b=6 1 5 -5; x=Ab0.7906-0.36130.8639-1.1152定义一呻对角矩阵以及一个单泣矩阵,o030QA004Jr diag(1:4) eye(4) ans =1000100002000100003000100004000119.输出如下所示的杨釋三角形(也称为Pascal 形):1kl1,2.11,3,3, 11,4, 6,4,1l,5t 10,10.5,11.6.15,20,15, 6,1L7P 21,35,35,21,7f 11, & 2& 56, 70, 56. 2& & 11.Z 36,84, 126
13、, 12& 84. 36, 9, 11, 10, 45. 120f 210, 252, 210t 120, 45, 10, 1fun cti on yh=yhsj (n)yh=1;disp(1);for k=2: nyh=yh,0+0,yh;disp(yh) endend% n=11运行13 114e411ioIQ&11162016甘11丁212B3521才11e705$011a襄B4,LZ&L2&54911ICi46120210*2622T1CI1 20451020.计算曲线y =扌门工册的弧长;(弧长公式 Jl+rW-rfr) x=sym(x); f=sqrt(1+(4/9)*xA(1 /
14、2)人2)(16*x)/81 + 1)A(1/2) a=sym(a); b=sym(b);ans =(16*b + 81)人(3/2)/216 - (16*a + 81)人(3/2)/21621-讣算摆线囂爲贈严5的十形的弧圮 t=sym(t); a=sym(a); x=a*(t-si n(t) a*(t - sin (t) y=a*(1-cos(t)-a*(cos(t) - 1) dx=diff(x)dx =-a*(cos(t) - 1) dy=diff(y) dy = a*si n(t) in t(sqrt(dxA2+dyA2),0,2*pi) ans =8*(aA2)A(1 /2)22.
15、 (TrQh(-拓/2厂0心)拓/2),(眄0)是函数A p=polyfit(-pi -pi/ 2 0 pi/2 pi,O -1 0 1 0,5)P =Columns 1 through 5Colu mn 6-0.0000 plot(-pi:pi/100:pi,polyval(p,-pi:pi/100:pi); hold on plot(-pi:pi/100:pi,s in (-pi:pi/100:pi);药.试编牙 洽程序它以个正整数为参数返回它的备位数字构成 的行向屋例如二fun23 (12345J返回2山,4*5fun cti on a=n um2p( n)strr= num2str(
16、n);in dex=size(strr);in dexx=in dex(2);a=;for k=i ndexx-1:-1:0a=a rem(fix( n/(10Ak),10);endend nu m2p() ans =Colu mns 1 through 895489298Columns 9 through 114 9 424 . i殳计个榄拟蒙特卡罗随机方建计漳Pi近似值的程序;比如 产生M个-1,1 *-1,1的随机点,统计出位于单位圏内的点的个数鬥则P/N约为Pi“fun cti on ppi=ttry (n)poi ntx=-1+2*ra nd(1, n);poi nty=-1+2*r
17、a nd(1, n);in dex=poi ntx.*poi ntx+poi nty.*poi nty;in dexx=in dex ttry(100) ans =3.1200 ttry(10000) ans =3.137625. Collate猜想是至今没有解决的数学问题.将任意牛口然数珂故如下 迭代过程,若是偶数将它除以N否则将它乘以3再加如此迭代下去-Cdlltz 猜想一定会得到1:请你编写一个程序,对该猜想做检验*fun cti on y=collatz( n)if n=1y=1return ;elseif rem(n, 2)=0n=n /2;elsen=3*n+1;endny=col
18、latz(n);end collatz(12)n =6n =3n =16126-对歯数在0,1区间上进行丄0等分并作等距节点上的务项氏插值,对计算所得的号项式在0,1!分.比鞭得到的积分值马原来函垃的积分值的误差。 f=(x) 1丿(1+X42) f =(x)1./(1+x.A2) y=f(x); x=O:O.1:1; y=f(x); p=polyfit(x,y,5)Columns 1 through 5-0.2372 0.3529 0.5071 -1.13430.0115Column 60.9999 intp=polyint(p)intp =Columns 1 through 5-0.039
19、5 0.0706 0.1268 -0.37810.0058Columns 6 through 70.9999 0 res1=polyval(intp,1)-polyval(intp,0)res1 = ata n(1) ans =0.7854 %acta n公式解27,通过文件操作功能,从丸件pi_lm.txt中读取Pi小数点后的100W 统 计出0门出现的次数,井将绪果写出到外部文件氓*文件 pi_lm. tx匕可从课程ftp下載-的文档 2matlab 作业 pi_1m.txt,r)fid =3 b=fsca nf(fid,%s); fclose(fid) ans = b(1:55)=;编辑
20、“ givemerun.m ”文件:y=1;n=zeros(1,10)flag=0;for x=1:1500000if y=1000001breakelseif 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 ”0 0 0 0 0 0Columns 1 through 399959 99758 100026Columns 4
21、 through 6100229 100230 100359Columns 7 through 999548 99800 99985Column 10100106%分别为0, 1, 2,,8, 9在前一百万位出现的次 sum(n)ans =1000000 cc=nu m2str( n)cc =99959 99758 100026 100229 100230 100359 99548 99800 99985100106 fope n( res.txt,w)ans = fprin tf(6,cc)ans =78文w99758 100026 100223 100230 10035G 99o48 购8
22、D099985 100106VZ$任勢个四个数字不全相等的四位数将它的4个数字组成最大4位数,最 小4位数.将所得婀数相减,得到一个新的4位数(不足4位则补0),依次执行 这个过程,最终总可M得到6175请编程加M验证.fun cti on f=veryfu n(n)if n=6174returnelsein dex(1)=( n-rem( n,1000)/1000;in dex(2)=fix( n/100)-i ndex*10;in dex(3)=rem(fix( n/10),10);in dex(4)=rem( n,10);in dex2=sort(i ndex, asce nd );in
23、 dex3=sort(i ndex, desce nd );a=1000 100 10 1;max=in dex3*(a);min=in dex2*(a);max-minveryfu n(max-mi n);endend veryfu n(9864)ans =5175ans =5994ans =5355ans =1998ans =8082ans =8532ans =617429在骄究化学反应速度时得到下列数攜。找出实验”曲隨的时间f与反应物 的H朋之间的关系井画出拟台曲线。X36 1I !l7 i12 -2124m57.64i 5 31222.915.412 18 9I(提示与r的关*为開二口
24、匕协fun cti on m=tem(i ndex,t)m=i ndex.*exp( in dex(2).*t) end index,r,j,c,m,e=nlinfit(t,m,tem,0 0) in dex =78.7137 -0.1047%在这里省略中间的迭代过程和表格变量输入过程 m=tem(i ndex,0:0.1:25); plot(0:0.1:25,m)丸”任给一个由10个两位数(10-39)组成的樂合.编写程序输出这个集合的两个不相交的的子集,使得这两个子樂的元素和相等,fun cti on son=sonfind(A)in dex=sum(A)/2;for k=1:5b=n choosek(1:10,k); sizee=size(b);kk=sizee(1);for x=1:kkc=b(x,:);A_so n=;for y=cA_so n=A_son A(y);endif sum(A_s on)=in dexson=A_son;breakendendend 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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1