I need to write for loop: s=sin(pi/1​)+cos(pi/2​)+sin(pi/3​)+....cos(​pi/100)

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

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)));

Sign in to comment.

 Accepted Answer

s=0;
for k=1:50
s=s+sin(pi/(2*k-1))+cos(pi/(2*k));
end
Best wishes
Torsten.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!