I have the following polynomial
F(x) = 1^5 + (1^5 + 2^5)+ (1^5 + 2^5 + 3^5) + ... + (1^5 + 2^5 + 3^5 + 4^5 + ... + x^5)
How can I write a loop iteration for this polynomials?

1 件のコメント

Stephen23
Stephen23 2020 年 11 月 20 日
Why do you need a loop?
1^5 + (1^5 + 2^5) + (1^5 + 2^5 + 3^5) + (1^5 + 2^5 + 3^5 + 4^5)
ans = 1610
sum(cumsum((1:4).^5))
ans = 1610

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

 採用された回答

KSSV
KSSV 2020 年 11 月 20 日
編集済み: KSSV 2020 年 11 月 20 日

0 投票

x = 10 ;
thesum = 0 ;
for i = 1:x
thesum = thesum+sum((1:i).^5) ;
end

5 件のコメント

Zeynep Toprak
Zeynep Toprak 2020 年 11 月 20 日
thanks a lot. Well, can we also do this without loop iteration, that's, vectorization?
KSSV
KSSV 2020 年 11 月 20 日
Yes very much it can be done: See Rik answer.
Zeynep Toprak
Zeynep Toprak 2020 年 11 月 20 日
many thanks
Zeynep Toprak
Zeynep Toprak 2020 年 11 月 20 日
btw, your code gives wrong result:(
KSSV
KSSV 2020 年 11 月 20 日
Hey..yes there is a typo error in the code. I have edited it now.

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

その他の回答 (1 件)

Rik
Rik 2020 年 11 月 20 日

0 投票

I would suggest not using a loop:
F=@(x) sum( ( (1:x).^5 ).*(x:-1:1) );

2 件のコメント

Zeynep Toprak
Zeynep Toprak 2020 年 11 月 20 日
this is with vectorization method?
Rik
Rik 2020 年 11 月 20 日
Yes, and the code posted by Stephen in a comment is also vectorized.
There are probably some exceptions I'm not thinking of, but if you don't see a loop (or cellfun/arrayfun) the code is vectorized.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2020 年 11 月 20 日

コメント済み:

Rik
2020 年 11 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by