我需要这样的输出:
如果n = 3;
x = function_name(n)
我需要 x = 11。
$ b
如果n = 5;
x = function_name(n)
我需要 x = 45
。
我相信我需要一个for循环来迭代;但是我发现很难迭代递增值本身。
解决方案
inc = 2;
sum = 1;
next = 1;
n = input('what is n?\\\
');
for i = 2:n
next = next + inc;
sum = sum + next;
inc = inc + 2;
end
disp('sum is');
disp(sum);
I need to compute the sum of this series
I need the output this way:
If n = 3;
x = function_name(n)
I need to get x = 11.
If n = 5;
x = function_name(n)
I need to get x = 45
.
I believe I need a for-loop to iterate; but am finding it difficult to iterate the increment value itself.
解决方案
inc=2;
sum=1;
next=1;
n=input('what is n?\n');
for i=2:n
next=next+inc;
sum=sum+next;
inc=inc+2;
end
disp('sum is ');
disp(sum);
这篇关于计算系列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!