How do I sum a series in Matlab without using a for loop?

The following is the code I've written to solve this series (chose an arbitrary N value of 10)
n=0:10
sum((4./(8.*n+1)-2./(8.*n+4)-1./(8.*n+5)-1./(8.*n+6)).*(1.^n)/(16.^n))
This series should ideally give a rough approximation of pi but I keep getting 2.1396e-15. I'm not sure what exactly Matlab is summing up.

 Accepted Answer

You seem to have made some typographical errors.

In your last division, you forgot to use element-wise operations in the division:

...  .*(1.^n)./(16.^n))
             ↑

You could further simplify it:

n=0:10;
sum((4./(8.*n+1)-2./(8.*n+4)-1./(8.*n+5)-1./(8.*n+6)).*((1/16).^n))

Both will now produce:

ans =
          3.14159265358979

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

on 30 Sep 2018

Answered:

on 30 Sep 2018

Community Treasure Hunt

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

Start Hunting!