用MATLAB编程计算.docx
1、 对n=1,2,10,求xn= 的值.编程:function x=yi(n)for n=1:10 x(n)=sin(n*pi/10);endx运行:x = 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 0.5878 0.3090 0.0000ans =0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 0.5878 0.3090 0.00002、 设银行年利率为11.25%.将10000元钱存入银行,问多长时间会连本带利翻一番?编程:function money=er(years)money=10000years=0while money<20000 years=years+1 money=money*(1+11.25/100)end运行:money =10000 years =0 years =1 money =11125years =2 money =1.2377e+004 years =3 money =1.3769e+004 years =4 money =1.5318e+004 years =5 money =1.7041e+004 years =6 money =1.8958e+004 years =7 money =2.1091e+004ans =2.1091e+0043、编程:function f=san(x)if x>1 f=x2+1endif x<=1 f=2*xend运行:>>san(2) f =5 ans =5>>san(-1) f = -2 ans =-24、编程1:function f=si(x)if x>1 f=x2+1else if x<=0 f=x3 else f=2*x endend运行1:>> si(2)f =5 ans =5>> si(0.5)f =1 ans =1>> si(-1)f =-1 ans =-1编程2:function f=sisi(x)f=(x2+1)*(x>1)+2*x*(x>0&x<=1)+x3*(x<=0)运行2:>> sisi(2)f =5 ans =5>> sisi(0.5)f =1 ans =1>> sisi(-1)f =-1 ans =-15、计算向量x=1,2,3,4;3,4,5,6'的与矩阵x=1,2,3,4;3,4,5,6的期望值与方差编程:function mean,stdev=wu(x)m,n=size(x);if m=1 m=n;endmean=sum(x)/m;stdev=sqrt(sum(x.2)/m-mean.2);运行:>> x=1,2,3,4;3,4,5,6'>> mean,stdev=wu(x)mean =2.5000 4.5000stdev =1.1180 1.1180>> x=1,2,3,4;3,4,5,6; >> mean,stdev=wu(x)mean =2 3 4 5stdev =1 1 1 16、函数的递归调用: 阶乘计算11! 21!编程:function fac=liu(n)if n=1|n=0; fac=1; returnendfac=n*liu(n-1);运行:>> liu(11)ans = 39916800>> liu(21)ans = 5.1091e+0197、有一天小猴摘下了若干个桃子,当即吃掉了一半,又多吃了一个.第二天接着吃了剩下的一半,又多吃了一个以后每天都是吃掉尚存的桃子的一半零一个.到第十天早上,小猴准备吃桃子时,看到只剩下 1 个桃子了.问小猴第一天共摘下了多少个桃子? 编程:function p=qi(k)k=10;p(k)=1;while k>=2 k=k-1; p(k)=2*(p(k+1)+1);endp(1)运行:ans =1534ans = 1534 766 382 190 94 46 22 10 4 18、求100以内的素数及其个数function m=ba(n) n=0; for m=1:100 flag=1;j=m-1; i=2; while i<=j & flag if rem(m,i)=0 flag=0; end i=i+1; end if flag n=n+1; ba(n)=m; end end N=n-1 ba(ba>1)运行:N=25ans =2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97ans =1009、已知多项式为,求其根。编程:function h=jiu(x)h=roots(1 -10 31 -10 -116 200 -96) 运行:h =-2.0000 4.0000 3.0000 2.0000 + 0.0000i 2.0000 - 0.0000i 1.0000 ans =-2.0000 4.0000 3.0000 2.0000 + 0.0000i 2.0000 - 0.0000i 1.0000 10、已知多项式的根h =-2.0000 4.0000 3.0000 2.0000 + 0.0000i 2.0000 - 0.0000i 1.0000 求多项式 编程:function c=shi(h)h=-2.0000 4.0000 3.0000 2.0000+0.0000i 2.0000-0.0000i 1.0000;c=poly(h)运行:c = 1 -10 31 -10 -116 200 -96ans = 1 -10 31 -10 -116 200 -96 统计1001班苏姚0121014440119