MATLAB: How to sum a series in Matlab without using a for loop

MATLABseries

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.

Best 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