我是Matlab的新手。我只是尝试实现系列1 + x + x ^ 2/2!+ x ^ 3/3的总和!.....但我无法找到如何做到这一点。到目前为止,我只做了一些数字。请帮忙。
for ii = 1:length(a)
sum_a = sum_a + a(ii)
sum_a
end
答案 0 :(得分:1)
n = 0 : 10; % elements of the series
x = 2; % value of x
s = sum(x .^ n ./ factorial(n)); % sum
答案 1 :(得分:0)
你的答案的第二部分是:
n = 0:input('variable?')
答案 2 :(得分:0)
Cheery's approach完全有效。对于较大的值,更快的方法如下。这样更有效,因为它避免了重复乘法:
m = 10;
x = 2;
result = 1+sum(cumprod(x./[1:m]));
m = 1000; x = 1;
tic
for k = 1:1e4
result = 1+sum(cumprod(x./[1:m]));
end
toc
tic
for k = 1:1e4
result = sum(x.^(0:m)./factorial(0:m));
end
toc
给出
Elapsed time is 1.572464 seconds.
Elapsed time is 2.999566 seconds.