Summation loop (sigma notation) help

3 ビュー (過去 30 日間)
S
S 2014 年 2 月 10 日
コメント済み: S 2014 年 2 月 10 日
Please help! I am slowly but surely understanding Matlab, but struggle a bit every now and then. I am trying to make a summation loop out of:
a=1:100; n=2; sum_a=0.5; for ii=1:length(a); sum_a=sum_a + a(ii).^n; end display(sum_a);
The way i read it is that (a)^n + (a+a)^n + (a+a+a)^n....
Is this correct?
Many thanks :D

回答 (3 件)

ES
ES 2014 年 2 月 10 日
編集済み: ES 2014 年 2 月 10 日
What yours does is sum of squares of natural numbers from 1 to 1000 +0.5. That is 0.5+1^2+2^2+3^2+...+1000^2;
Is this what you want?
  1 件のコメント
S
S 2014 年 2 月 10 日
No i want the 0.5 to be the first number and then it goes up in powers of 2 till infinite ;/

サインインしてコメントする。


Mischa Kim
Mischa Kim 2014 年 2 月 10 日
In your code
sum_a = 0.5;
for ii = 1:length(a)
sum_a = sum_a + a(ii).^n;
end
you initially set sum_a = 0.5. In the loop you then add a(ii).^n to the previously stored value of sum_a. In other words:
ii = 1: sum_a = 0.5 + 1^2 ... which equals 1.5
ii = 2: sum_a = 1.5 + 2^2 ... which equals 5.5
ii = 3: sum_a = 5.5 + 3^2 ... which equals 14.5
...
  1 件のコメント
S
S 2014 年 2 月 10 日
Thanks :D

サインインしてコメントする。


Iain
Iain 2014 年 2 月 10 日
If you want: (a)^n + (a+a)^n + (a+a+a)^n....
What you actually want is:
a = 0.5; % or whatever
sum = 0;
n = 2;
for i = 1:m % m being the limit
sum = sum + (i*a)^n
end
So, if m is 0, you get sum = 0, if m is 1, you get 0.25, if m is 2, sum = 1.25, m = 3, sum = 3.5, m = 4 sum = 7.5 ...
  1 件のコメント
S
S 2014 年 2 月 10 日
Thanks this helps alot :D

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by