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 commentaire

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

Connectez-vous pour commenter.

 Réponse acceptée

KSSV
KSSV le 20 Nov 2020
Modifié(e) : KSSV le 20 Nov 2020

0 votes

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

5 commentaires

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

Connectez-vous pour commenter.

Plus de réponses (1)

Rik
Rik le 20 Nov 2020

0 votes

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

2 commentaires

Zeynep Toprak
Zeynep Toprak le 20 Nov 2020
this is with vectorization method?
Rik
Rik le 20 Nov 2020
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by