用MATLAB求对1/0! 1/1! 1/2! 1/3! ``````````=e,确定这个级数的前多少项能精确到e的10位有效数字

来源:百度知道 编辑:UC知道 时间:2024/09/21 11:15:02

clc;clear
format long
k=0;temp=0;test=1;
while test>1e-10
temp=temp+1/factorial(k); %ex = 1+x+x^2/2!+x^3/3!+...+xn/n!+...
test=abs(temp-exp(1));
k=k+1;
end
result=[k test temp exp(1)]

k=0;temp=0;test=1;
while test>1e-10
temp=temp+(-1)^k/factorial(2*k+1);%sin x = x-x^3/3!+x^5/5!-...(-1)k-1*x^(2k-1)/(2k-1)!+... (-∞<x<∞)
test=abs(temp-sin(1));
k=k+1;
end
result=[k test temp sin(1)]

k=0;temp=0;test=1;
while test>1e-10
temp=temp+(-1)^k/factorial(2*k); %cos x = 1-x^2/2!+x^4/4!-...(-1)k*x^(2k)/(2k)!+... (-∞<x<∞)
test=abs(temp-cos(1));
k=k+1;
end
result=[k test temp cos(1)]

结果:
result =

14.00000000000000 0.00000000001229 2.71828182844676 2.71828182845905

result =

7.00000000000000 0.00000000000076 0.84147098480866 0.84147098480790

result =<