matlab练习一二问题详解.docx
- 文档编号:6988970
- 上传时间:2023-01-15
- 格式:DOCX
- 页数:8
- 大小:185.65KB
matlab练习一二问题详解.docx
《matlab练习一二问题详解.docx》由会员分享,可在线阅读,更多相关《matlab练习一二问题详解.docx(8页珍藏版)》请在冰豆网上搜索。
matlab练习一二问题详解
练习一答案
1、一个三位整数各位数字的立方和等于该数本身,则称该数为水仙花数。
试编写程序找到,并输出全部水仙花数。
forn=100:
999
a=floor(n/100);
b=floor(rem(n,100)/10);
c=rem(n,10);
ifn==a^3+b^3+c^3
s=n
end
end
2、若一个数等于它的各个真因子之和,则称该数为完数,如6=1+2+3,所以6是完数。
求
[1500]之间的全部完数。
fori=2:
500
s=1;
forj=2:
(i-1)
if(rem(i,j)==0)
s=s+j;
end
end
ifs==i
s
end
end
3、写一个函数pifun计算下列级数:
F(n)=4*(1-1/3+1/5-1/7+...)
并使用tic和toc指令来测量pifun(1000000)的计算时间。
n=input('请输入n值:
');
tic;
f=0;
fori=1:
n
f=f+4*((-1)^(i+1))/(2*i-1);
end
f
toc
4、输入一个百分制成绩,要求输出成绩等级为A~E,即90~100为A,80~89为B,70~79
为C,60~69为D,60以下为E。
m=input('请输入一个百分制成绩:
');
m=floor(m/10);
switchm
case{10,9}
disp('A');
case8
disp('B');
case7
disp('C');
case6
disp('D');
disp('E');
otherwise
disp('E');
end
5、输入x,y的值,并将它们的值互换后输出。
x=input('x=');
y=input('y=');
disp([x,y]);
t=x;
x=y;
y=t;
disp([x,y]);
6、某商场对顾客所购买的商品实行打折销售,标准如下(商品价格用price来表示):
price<200没有折扣;200≤price<5003%折扣;500≤price<10005%折扣;
1000≤price<25008%折扣;2500≤price<500010%折扣;5000≤price14%折扣
输入所售商品的价格,求其实际销售价格。
x=input('price:
')
ifx<200
f=x;
elseifx<500
f=x*0.97;
elseifx<1000
f=x*0.95;
elseifx<2500
f=x*0.92;
elseifx<5000
f=x*0.90;
else
f=x*0.86;
end
end
end
end
end
f
7由指令A=rand(3,5)生成二维数组A,试求该数组中所有大于0.5的元素的位置。
A=rand(3,5)
fori=1:
3
forj=1:
5
ifA(i,j)>0.5
Ai=i;
Aj=j;
Aij=[i,j]
end
end
end
8、写一程序求1!
+2!
+…+10!
sum=0;
form=1:
10
i=1;
forn=1:
m
i=i*n;
end
sum=sum+i;
end
sum
9、编写一个函数,找出矩阵A中最大元素和最小元素所在的行列号。
A=rand(3,5);
disp(A);
Max=max(max(A));
Min=min(min(A));
disp(Max);
[r1,c1]=find(A==Max)
disp(Min);
[r2,c2]=find(A==Min)
10、从键盘输入的10个整数中,找出第一个能被7整除的数。
若找到,打印此数后退出循环;若未找到,打印“notexist”。
a=zeros(1,10);
fori=1:
10
a(i)=input('pleaseenternumber');
end
fori=1:
10
if(rem(a(i),7)==0)
a(i)
break
end
end
if(a(i)==0&&rem(a(i),7)~=0)
disp('notexist')
end
练习二答案
1绘制下列曲线。
x=-2:
0.1:
2;
y=exp(-x.^2/2)/(2*pi);
plot(x,y)
1.2
t=0:
0.1:
2*pi;
plot(8*cos(t),4*sqrt
(2)*sin(t))
2分别用plot和fplot函数绘制函数
的曲线,分析两曲线的差别
(1)x=-2:
0.01:
2;
y=sin(1./x);
plot(x,y)
(2)fplot(‘sin(1/x)’,[-2,2])
3设y=1/1+e-t,-pi<=t<=pi,在同一窗口中采用子图的形式绘制不同图形:
条形图,阶梯图,杆图和对数坐标图
t=-pi:
0.1:
pi;
y=1./(1+exp(-t));
plot(t,y);
subplot(2,2,1);
stairs(t,y);
subplot(2,2,2);
bar(t,y);
subplot(2,2,3);
stem(t,y);
subplot(2,2,4);
loglog(t,y);
4绘制三维图
X=e-t/20cos(t)
Y=e-t/20sin(t)0 Z=t t=0: 0.1: 2.*pi; x=exp(-t./20).*cos(t); y=exp(-t./20).*sin(t); [x,y]=meshgrid(x,y); z=(log(x.^2+y.^2)).*(-10); mesh(x,y,z); 5在同一图形窗口中绘制函数y1=1-sin2(x)(蓝色圆圈),y2=2x+1(绿色点划线),加分格线,x的围都是[0,10],给图形加上标题“y1和y2”,在x轴上加注“x轴”,在y轴上加注“y轴”,在图右侧添加图例,并把“x=5”字符串放置到图形中鼠标所指定的位置上。 x=0: 0.1: 10; y1=1-(sin(x)).^2; y2=2*x+1; plot(x,y1,'bo'); holdon; plot(x,y2,'g-.'); xlabel('x') ylabel('y') grid legend('y1','y2',1) gtext('x=5') 6 x=0: 0.04: 2*pi; y=exp(x./2).*sin(x); y1=exp(x./2).*sin(x); plot(x,y,’bo’,x,y1,’r-‘) gridon; gtext('\leftarrowe-pi/20sin(pi)=0'); title('函数exp(x./2).*sin(x)'); xlabel('x轴0 ylabel('y轴’)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab 练习 一二 问题 详解
![提示](https://static.bdocx.com/images/bang_tan.gif)