资源描述:
《用MATLAB编程计算》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、1、对n=1,2,…,10,求xn=的值.编程:functionx=yi(n)forn=1:10x(n)=sin(n*pi/10);endx运行:x=0.30900.58780.80900.95111.00000.95110.80900.58780.30900.0000ans=0.30900.58780.80900.95111.00000.95110.80900.58780.30900.00002、设银行年利率为11.25%.将10000元钱存入银行,问多长时间会连本带利翻一番?编程:functionmoney=er(years)mone
2、y=10000years=0whilemoney<20000years=years+1money=money*(1+11.25/100)end运行:money=10000years=0years=1money=11125years=2money=1.2377e+004years=3money=1.3769e+004years=4money=1.5318e+004years=5money=1.7041e+004years=6money=1.8958e+004years=7money=2.1091e+004ans=2.1091e+0043、编
3、程:functionf=san(x)ifx>1f=x^2+1endifx<=1f=2*xend运行:>>san(2)f=5ans=5>>san(-1)f=-2ans=-24、编程1:functionf=si(x)ifx>1f=x^2+1elseifx<=0f=x^3elsef=2*xendend运行1:>>si(2)f=5ans=5>>si(0.5)f=1ans=1>>si(-1)f=-1ans=-1编程2:functionf=sisi(x)f=(x^2+1)*(x>1)+2*x*(x>0&x<=1)+x^3*(x<=0)运行2:>>si
4、si(2)f=5ans=5>>sisi(0.5)f=1ans=1>>sisi(-1)f=-1ans=-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);ifm==1m=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.50004.500
5、0stdev=1.11801.1180>>x=[1,2,3,4;3,4,5,6];>>[mean,stdev]=wu(x)mean=2345stdev=11116、函数的递归调用:阶乘计算11!21!编程:functionfac=liu(n)ifn==1
6、n==0;fac=1;returnendfac=n*liu(n-1);运行:>>liu(11)ans=39916800>>liu(21)ans=5.1091e+0197、有一天小猴摘下了若干个桃子,当即吃掉了一半,又多吃了一个.第二天接着吃了剩下的一半,又多吃了一个以后每天都是吃掉尚存的
7、桃子的一半零一个.到第十天早上,小猴准备吃桃子时,看到只剩下1个桃子了.问小猴第一天共摘下了多少个桃子?编程:functionp=qi(k)k=10;p(k)=1;whilek>=2k=k-1;p(k)=2*(p(k+1)+1);endp(1)运行:ans=1534ans=153476638219094462210418、求100以内的素数及其个数functionm=ba(n)n=0;form=1:100flag=1;j=m-1;i=2;whilei<=j&flagifrem(m,i)==0flag=0;endi=i+1;endiffla
8、gn=n+1;ba(n)=m;endendN=n-1ba(ba>1)运行:N=25ans=2357111317192329313741434753596167717379838997ans=1009、已知多项式为,求其根。编程:functionh=jiu(x)h=roots([1-1031-10-116200-96])运行:h=-2.00004.00003.00002.0000+0.0000i2.0000-0.0000i1.0000ans=-2.00004.00003.00002.0000+0.0000i2.0000-0.0000i1.0
9、00010、已知多项式的根h=-2.00004.00003.00002.0000+0.0000i2.0000-0.0000i1.0000求多项式编程:functionc=shi(h)h=[-2