ismember(1,[1 2 3 4]):1是否是向量1 2 3 4中的一个数。
choices=eoption;
if choices ==1
explaine;
elseif choices ==2
limite;
elseif choices ==3
expfn;
elseif choicess==4
%
end
function choices = eoption()
printChoices;
choices = input('please choose an option:');
while ismember(choices,1:4)
%choices~=1 && choices~=2 && choices~=3 && choices~=4
disp('Error - please choose an option.');
printChoices;
choices = input('please choose an option:');
end
function printChoices()
fprintf('1) explaine:\n')
fprintf('2) limite:\n')
fprintf('3) expfn:\n')
fprintf('4) exit:\n')
end
end
function explaine()
fprintf('e就是2.7几的一个数!\n')
end
function limite()
n=input('please enter a n, and then will give you a value\n');
while n <=0
fprintf('you enter a number %2f\n',n)
n=input('please enter a n, and then will give you a value\n');
end
fprintf('当n=%f时,e的大概值为%2.9f\n',n,(1+1/n)^n)
end
function expfn
x = input('input value of x:');
fprintf('the exact value exp(x) = %.4f\n',exp(x));
fprintf('the approximate value exp(x) = %.4f\n',appExp(x,4));
function y=appExp(x,n)
y = sum(x.^(0:(n-1))./factorial(0:(n-1)));
end
end
persistent变量。
global 全局变量