1. 试计算以下循环语句操作的步数。
(1) for i=-1000:1000
length(-1000:1000)
ans = 2001次
(2) for j=1:2:20%j=[1,3,......,19]
length(1:2:20)
ans = 10
2. 用π/4≈1-1/3+1/5-1/7+…公式求π的近似值,直到最后一
项的绝对值小于 为止,试编写其 M文件并运行,写出
实际运行结果。
很明显收敛速度超慢的求pi公式!直接用脚本文件方便
fourthpi=0;tol=1;n=1;m=1;
while tol>1e-5
T=1/(2*n-1)*m;
fourthpi=fourthpi+T;
n=n+1;m=-m;
tol=abs(pi/4-fourthpi);
end
fourthpi
fourthpi =
0.785388163397451
3. 试用 switch 语句完成卷面成绩 score 的转换:
(1) score ≥90 分,优;
(2) 90>score ≥80 分,良;
(3) 80>score≥70 分,中;
(4) 70>score ≥60 分,及格;
(5) 60<score,不及格。
flag=fix(score/10);
switch flag
case {9,10}
disp('优');
case 8
disp('良');
case 7
disp('中');
case 6
disp('及格');
otherwise
disp('你,完了!')
end
4. 用for循环和while循环语句求 1!+2!+…+10!的值。
%while
he=0;n=1;
while n<11
he=he+factorial(n);
n=n+1;
end
he
he =
4037913
%for
e=0;
for n=1:10
he=he+factorial(n);
end
he
he =
4037913
设Z=x^2*e^(-x^2-y^2),求定义域 x=[-2,2],y=[-2,2]内的z值
(网格取 0.1)。请把 z 的值用网线图形象地表示出来。
[x,y]=meshgrid(-2:0.1:2);
z=x.^2.*exp(-x.^2-y.^2);
mesh(x,y,z)
温馨提示:答案为网友推荐,仅供参考