how to make sum of (for loop)

2 vues (au cours des 30 derniers jours)
Mech
Mech le 7 Juin 2014
I need for loop or any method to calculat next sum
  • m=1:n
  • a+2(cos(b))
  • a+2(cos(b)+cos(2b))
  • a+2(cos(b)+cos(2b)+cos(3b))
  • a+2(cos(b)+cos(2b)+.............cos(nb))
  2 commentaires
Cedric
Cedric le 7 Juin 2014
What have you done/tried so far?
Mech
Mech le 7 Juin 2014
Modifié(e) : Mech le 7 Juin 2014
ok
  • a1=a+2(cos(b))
  • a2=a+2(cos(b)+cos(2b))
  • a3=a+2(cos(b)+cos(2b)+cos(3b))
I need value of a1,a2,a3

Connectez-vous pour commenter.

Réponses (3)

Roger Stafford
Roger Stafford le 7 Juin 2014
Modifié(e) : Roger Stafford le 7 Juin 2014
Assuming b is a scalar,
s = a + 2*sum(cos((1:n)*b));
An alternate formula without the long summation is:
s = a + 2*cos((n+1)*b/2)*sin(n*b/2)/sin(b/2);

Mech
Mech le 8 Juin 2014
help guys :(
  1 commentaire
Cedric
Cedric le 9 Juin 2014
What doesn't work in the solution proposed by Roger?

Connectez-vous pour commenter.


David Sanchez
David Sanchez le 9 Juin 2014
the for loop:
a = 3; % or whatever value you have in mind
b = 2; % or whatever value you have in mind
n = 10; % or whatever value you have in mind
your_sum = 0; % initialization of summation part
for m=1:n
your_sum = your_sum + cos(m*b);
end
your_sum = a + 2*your_sum;

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by