how to make sum of (for loop)

조회 수: 7 (최근 30일)
Mech
Mech 2014년 6월 7일
답변: David Sanchez 2014년 6월 9일
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
Cedric
Cedric 2014년 6월 7일
What have you done/tried so far?
Mech
Mech 2014년 6월 7일
편집: Mech 2014년 6월 7일
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

댓글을 달려면 로그인하십시오.

답변 (3개)

Roger Stafford
Roger Stafford 2014년 6월 7일
편집: Roger Stafford 2014년 6월 7일
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 2014년 6월 8일
help guys :(
  댓글 수: 1
Cedric
Cedric 2014년 6월 9일
What doesn't work in the solution proposed by Roger?

댓글을 달려면 로그인하십시오.


David Sanchez
David Sanchez 2014년 6월 9일
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;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by