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.

 采纳的回答

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

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by