How to calculate sum without loop?

29 views (last 30 days)
Numerical Noob
Numerical Noob on 12 Feb 2021
Commented: Numerical Noob on 12 Feb 2021
Hello,
I must calculate S in the range of n=1 to n=100 without a loop.
s=(1...100)(101-n)*cos(n/100)
Example= 100*cos(1/100) + 99*cos(2/100) ... + 1*cos(100/100)

Answers (1)

KSSV
KSSV on 12 Feb 2021
n = 1:100 ;
s = sum(n.*cos(n/100))
  2 Comments
Aditya Kommajosula
Aditya Kommajosula on 12 Feb 2021
Edited: Aditya Kommajosula on 12 Feb 2021
If I understand the OP's question right, the solution might have to be:
n = 1:100;
s = sum(n(end:-1:1).*cos(n/100));
Regards
Numerical Noob
Numerical Noob on 12 Feb 2021
Thank you Aditya, I tried it with an iterative approach with n=1..3. Your solution is right.

Sign in to comment.

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!