1.2500000000000000000000000000000
Elapsed time is 4.029501 seconds.
〖说明〗
使用数值求和指令和“数组运算”的计算速度最快。
符号计算的精度最高,但速度慢。
以上程序运行时间仅供参考。具体时间与所用机器、那程序是否初次运行、在MATLAB
指令窗还是在M-book中运行等因素有关。
2 编写一个函数M文件,它的功能:没有输入量时,画出单位圆
(见图p6.2-1);输入量是大于2的自然数N时,绘制正N边形,图名应反映显示多边形的真实边数(见图p6.2-2);输入量是“非自然数”时,给出“出错提示”。此外,函数M文件应有H1行、帮助说明和程序编写人姓名。
〖解答〗
(1)函数M文件
function prob_solve602(n)
% prob_solve602(n) plot a circle or a polygon with n edges
% prob_solve602 plot a circle
% n 应为大于2的自然数
% By ZZY, 2006-2-15
if nargin==0
t=0:pi/100:2*pi;
x=exp(i*t);
str='Circle';
else
if (nargin~=0)&(n<=2)
error('输入量应是大于2的自然数')
end;
if n-round(n)~=0 %检查非自然数
error('输入量应是大于2的自然数')
end;
t=(0:n)/n*2*pi;
x=exp(i*t);
str=['Polygon with ', int2str(n),' edges']; % 合成字符串
end
plot(real(x),imag(x),'r','LineWidth',4)
title(str)
axis square image off
shg
(2)各典型运行情况
prob_solve602