I need to write for loop: s=sin(pi/1)+cos(pi/2)+sin(pi/3)+....cos(pi/100)
Show older comments
s=sin(pi/1)+cos(pi/2)+sin(pi/3)+....cos(pi/100)
I did this, but this is not working:
a= 0, b=0; for x=1:2:100 a=a+(sin(pi/x)); for y=2:2:100 b=b+(cos(pi/y)); end end
1 Comment
Stephen23
on 2 Nov 2015
Why use a loop? MATLAB is a high-level language, so why not learn to use its code vectorization abilities to write neat and fast code:
N = 100;
S = sum(sin(pi./(1:2:N))+cos(pi./(2:2:N)));
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!