how to make sum of (for loop)
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
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);
0 个评论
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!