sum of series. Vectorised (no loop)
Show older comments
How can I sum n terms of
1-1/2+1/3-1/4......
Accepted Answer
More Answers (2)
Mohammad Ali
on 27 Apr 2021
1 Comment
DGM
on 27 Apr 2021
This only gives the correct answer for odd inputs.
You can calculate the sum of a finite alternating harmonic series easy enough:
N = 1000;
n = 1:N;
s = sum((2*mod(n,2)-1)./n)
gives
s =
0.6926
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!