---
title: "NSum"
language: "en"
type: "Symbol"
summary: "NSum[f, {i, imin, imax}] gives a numerical approximation to the sum \\[Sum]i = imin imax f. NSum[f, {i, imin, imax, di}] uses a step di in the sum."
keywords: 
- alternating series summation
- alternating signs
- Euler–Maclaurin
- Euler–Maclaurin summation
- numerical summation
- sequence acceleration
- sums
- Wynn epsilon
- AlternatingSigns
- EulerMaclaurin
- WynnEpsilon
canonical_url: "https://reference.wolfram.com/language/ref/NSum.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Numerical Evaluation & Precision"
    link: "https://reference.wolfram.com/language/guide/NumericalEvaluationAndPrecision.en.md"
  - 
    title: "Discrete Calculus"
    link: "https://reference.wolfram.com/language/guide/DiscreteCalculus.en.md"
  - 
    title: "Calculus"
    link: "https://reference.wolfram.com/language/guide/Calculus.en.md"
related_functions: 
  - 
    title: "Total"
    link: "https://reference.wolfram.com/language/ref/Total.en.md"
  - 
    title: "Plus"
    link: "https://reference.wolfram.com/language/ref/Plus.en.md"
  - 
    title: "NProduct"
    link: "https://reference.wolfram.com/language/ref/NProduct.en.md"
  - 
    title: "Sum"
    link: "https://reference.wolfram.com/language/ref/Sum.en.md"
  - 
    title: "AsymptoticSum"
    link: "https://reference.wolfram.com/language/ref/AsymptoticSum.en.md"
  - 
    title: "SumConvergence"
    link: "https://reference.wolfram.com/language/ref/SumConvergence.en.md"
  - 
    title: "NIntegrate"
    link: "https://reference.wolfram.com/language/ref/NIntegrate.en.md"
related_tutorials: 
  - 
    title: "Numerical Mathematics: Basic Operations"
    link: "https://reference.wolfram.com/language/tutorial/NumericalCalculations.en.md"
  - 
    title: "Numerical Sums, Products and Integrals"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#3848"
  - 
    title: "Numerical Mathematics"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#20411"
  - 
    title: "Numerical Evaluation of Sums and Products"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#10980"
  - 
    title: "Implementation notes: Numerical and Related Functions"
    link: "https://reference.wolfram.com/language/tutorial/SomeNotesOnInternalImplementation.en.md#5064"
---
# NSum

NSum[f, {i, imin, imax}] gives a numerical approximation to the sum $\sum _{i=i_{\min }}^{i_{\max }} f$.

NSum[f, {i, imin, imax, di}] uses a step di in the sum.

## Details and Options

* ``NSum`` can be used for sums with both finite and infinite limits.

* ``NSum[f, {i, …}, {j, …}, …]`` can be used to evaluate multidimensional sums.

* The following options can be given:

|                    |                  |                                                 |
| :----------------- | :--------------- | :---------------------------------------------- |
| AccuracyGoal       | Infinity         | number of digits of final accuracy sought       |
| EvaluationMonitor  | None             | expression to evaluate whenever f is evaluated  |
| Method             | Automatic        | method to use                                   |
| NSumTerms          | 15               | number of terms to use before extrapolation     |
| PrecisionGoal      | Automatic        | number of digits of final precision sought      |
| VerifyConvergence  | True             | whether to explicitly test for convergence      |
| WorkingPrecision   | MachinePrecision | the precision used in internal computations     |

* Possible settings for the ``Method`` option include:

|                    |                                            |
| ------------------ | ------------------------------------------ |
| "AlternatingSigns" | method for summands with alternating signs |
| "EulerMaclaurin"   | Euler–Maclaurin summation method           |
| "WynnEpsilon"      | Wynn epsilon extrapolation method          |

* With the Euler–Maclaurin method, the options ``AccuracyGoal`` and ``PrecisionGoal`` can be used to specify the accuracy and precision to try and get in the final answer. ``NSum`` stops when the error estimates it gets imply that either the accuracy or precision sought has been reached.

* You should realize that with sufficiently pathological summands, the algorithms used by ``NSum`` can give wrong answers. In most cases, you can test the answer by looking at its sensitivity to changes in the setting of options for ``NSum``.

* ``VerifyConvergence`` is only used for sums with infinite limits.

* ``N[Sum[…]]`` calls ``NSum`` for sums that cannot be done symbolically.

* ``NSum`` first localizes the values of all variables, then evaluates ``f`` with the variables being symbolic, and then repeatedly evaluates the result numerically.

* ``NSum`` has attribute ``HoldAll``, and effectively uses ``Block`` to localize variables.

---

## Examples (23)

### Basic Examples (1)

Approximate an infinite sum numerically:

```wl
In[1]:= NSum[(-5) ^ i / i!, {i, 0, ∞}]

Out[1]= 0.00673795
```

The error versus the exact value of $e^{-5}$ :

```wl
In[2]:= % - Exp[-5]

Out[2]= -4.220582217051572`*^-15
```

### Scope (5)

Approximate the value of a finite sum:

```wl
In[1]:= NSum[1 / i ^ 2, {i, 100, 10 ^ 6}]

Out[1]= 0.0100492
```

This is effectively the difference of two infinite sums:

```wl
In[2]:= NSum[1 / i ^ 2, {i, 100, ∞}] - NSum[1 / i ^ 2, {i, 10 ^ 6, ∞}]

Out[2]= 0.0100492
```

---

Approximate a multidimensional sum:

```wl
In[1]:= NSum[(-1) ^ n(2 / n) ^ k / k ^ 2, {n, 2, ∞}, {k, 1, ∞}]

Out[1]= 1.14434
```

---

Approximate a multidimensional sum with the second index depending on the first:

```wl
In[1]:= NSum[(-1) ^ n(2 / n) ^ k / k ^ 2, {n, 2, ∞}, {k, 1, n}]

Out[1]= 0.770188
```

---

Complex series summation:

```wl
In[1]:= NSum[Log[x] / x ^ (2 + 2I), {x, 1, ∞}]

Out[1]= -0.182175 - 0.136618 I
```

---

Sum the even terms of a series:

```wl
In[1]:= NSum[1 / 2 ^ i, {i, 0, ∞, 2}]

Out[1]= 1.33333
```

An equivalent way of specifying the same sum:

```wl
In[2]:= NSum[1 / 2 ^ (2j), {j, 0, ∞}]

Out[2]= 1.33333
```

### Options (9)

#### AccuracyGoal and PrecisionGoal (1)

Approximate a sum with the default tolerances:

```wl
In[1]:= NSum[1 / n ^ (11 / 10), {n, 1, ∞}]

Out[1]= 10.5844
```

Find the error versus the exact value of $\zeta \left(\frac{11}{10}\right)$ :

```wl
In[2]:= % - Zeta[11 / 10]

Out[2]= -2.227338313787186`*^-10
```

The error with smaller absolute and relative tolerances:

```wl
In[3]:= NSum[1 / n ^ (11 / 10), {n, 1, ∞}, AccuracyGoal -> 4, PrecisionGoal -> 4] - Zeta[11 / 10]

Out[3]= 1.1091653817629776`*^-8
```

The error with larger absolute and relative tolerances:

```wl
In[4]:= NSum[1 / n ^ (11 / 10), {n, 1, ∞}, AccuracyGoal -> 10, PrecisionGoal -> 10] - Zeta[11 / 10]

Out[4]= 9.36140054363932`*^-13
```

#### EvaluationMonitor (3)

Get the number of evaluation points used in a numerical summation:

```wl
In[1]:= Block[{k = 0}, {NSum[(1/(n + 1)^2), {n, 0, ∞}, EvaluationMonitor :> k++], k}]

Out[1]= {1.64493, 114}
```

---

The evaluation points used in a summation approximated by an integration method:

```wl
In[1]:= ListPlot[Reap[NSum[(1/(n + 1)^2), {n, 0, ∞}, EvaluationMonitor :> Sow[n]]][[2, 1]]]

Out[1]= [image]
```

---

The evaluation points used in a summation approximated by the sequence extrapolation method:

```wl
In[1]:= ListPlot[Reap[NSum[((-1)^n/(n + 1)^2), {n, 0, ∞}, EvaluationMonitor :> Sow[n]]][[2, 1]]]

Out[1]= [image]
```

#### Method (1)

Use a method for alternating series to get a very precise sum approximation:

```wl
In[1]:= NSum[(-1) ^ x / (1 + (x - 12) ^ 2), {x, 0, ∞}, Method -> "AlternatingSigns", WorkingPrecision -> 30]

Out[1]= 0.275193859413388147454815603683
```

The error from the exact result computed by ``Sum`` :

```wl
In[2]:= % - Sum[(-1) ^ x / (1 + (x - 12) ^ 2), {x, 0, ∞}]

Out[2]= 2.318916656366212551729`4.369556667877067*^-26
```

The error using the default method:

```wl
In[3]:= NSum[(-1) ^ x / (1 + (x - 12) ^ 2), {x, 0, ∞}, WorkingPrecision -> 30] - Sum[(-1) ^ x / (1 + (x - 12) ^ 2), {x, 0, ∞}]

Out[3]= 7.88844920109451095`0.02490010873590122*^-12
```

#### NSumTerms (1)

By default ``NSum`` uses 15 terms at the beginning before approximating the tail:

```wl
In[1]:= NSum[1 / ((k - 20) ^ 2 + 1), {k, 0, ∞}]

Out[1]= 3.09286
```

The error for this example is large because the summand peaks at 20:

```wl
In[2]:= % - Sum[1 / ((k - 20) ^ 2 + 1), {k, 0, ∞}]

Out[2]= -0.0117554

In[3]:= ListPlot[Table[1 / ((k - 20) ^ 2 + 1), {k, 1, 40}], PlotRange -> All]

Out[3]= [image]
```

Increasing ``NSumTerms`` to include this feature improves the approximation:

```wl
In[4]:= NSum[1 / ((k - 20) ^ 2 + 1), {k, 0, ∞}, NSumTerms -> 30]

Out[4]= 3.10462

In[5]:= % - Sum[1 / ((k - 20) ^ 2 + 1), {k, 0, ∞}]

Out[5]= -2.1403532279862247`*^-9
```

#### VerifyConvergence (2)

By default the summand's convergence is verified:

```wl
In[1]:= NSum[(-1) ^ k / (k + Log[k]), {k, 1, ∞}]//Timing

Out[1]= {0.07, -0.769402}
```

Generally, if the convergence is not verified the summation is faster:

```wl
In[2]:= NSum[(-1) ^ k / (k + Log[k]), {k, 1, ∞}, VerifyConvergence -> False]//Timing

Out[2]= {0.01, -0.769402}
```

---

Approximate the formal sum without verifying convergence:

```wl
In[1]:= NSum[2^i, {i, 0, Infinity}, VerifyConvergence -> False]
```

SequenceLimit::seqlim: The general form of the sequence could not be determined, and the result may be incorrect.

```wl
Out[1]= -1.
```

This is roughly equivalent to the formal symbolic result:

```wl
In[2]:= Sum[a^i, {i, 0, Infinity}]

Out[2]= (1/1 - a)

In[3]:= % /. a -> 2

Out[3]= -1
```

#### WorkingPrecision (1)

Use higher precision to get a better approximation:

```wl
In[1]:= NSum[1 / n ^ (51 / 50), {n, 1, ∞}, WorkingPrecision -> 40]

Out[1]= 50.57867004101560321761330253282588267405
```

Find the error versus the exact value of $\zeta \left(\frac{51}{50}\right)$ :

```wl
In[2]:= % - Zeta[51 / 50]

Out[2]= -2.581533075642050449458`11.707907587919056*^-27
```

### Applications (2)

Approximate the sum of the reciprocals of the ``Fibonacci`` numbers:

```wl
In[1]:= NSum[1 / Fibonacci[i], {i, ∞}]

Out[1]= 3.35989
```

---

Find the approximate limit of a slowly converging sequence by summing the difference:

```wl
In[1]:= f[n_] := Exp[-1 / (n + 1)];

In[2]:=
NSum[If[n == 1, f[1], 
	f[n] - f[n - 1]], {n, 1, ∞}, Method -> "WynnEpsilon"]

Out[2]= 1.00008
```

Compare to the exact result:

```wl
In[3]:= Limit[f[n], n -> Infinity]

Out[3]= 1
```

### Properties & Relations (3)

When the closed-form sum is available, ``Sum`` can be used instead of ``NSum`` :

```wl
In[1]:= exact = Sum[(1/x^2 + 5), {x, 0, ∞}]

Out[1]= (Csch[Sqrt[5] π] (5 π Cosh[Sqrt[5] π] + Sqrt[5] Sinh[Sqrt[5] π])/10 Sqrt[5])
```

The results of ``Sum`` and ``NSum`` agree closely:

```wl
In[2]:= NSum[(1/x^2 + 5), {x, 0, ∞}] - exact

Out[2]= -1.125365356458019`*^-10
```

---

Using ``Log`` and ``Exp`` to estimate a product:

```wl
In[1]:= Exp[NSum[Log[(4n ^ 2) / (4n ^ 2 - 1)], {n, 1, ∞}]]

Out[1]= 1.5708
```

The estimate compares well with the exact result:

```wl
In[2]:= % - Product[(4n ^ 2) / (4n ^ 2 - 1), {n, 1, ∞}]

Out[2]= -3.360911549066259`*^-11
```

The approximation can be done directly with ``NProduct`` :

```wl
In[3]:= NProduct[(4n ^ 2) / (4n ^ 2 - 1), {n, 1, ∞}]

Out[3]= 1.5708
```

---

``NSum`` can be used to calculate oscillatory integrals over ``(0, ∞)`` :

```wl
In[1]:= f[i_ ? NumericQ] := NIntegrate[BesselJ[3, x] / Sqrt[x], {x, BesselJZero[3, i - 1], BesselJZero[3, i]}]

In[2]:= NIntegrate[BesselJ[3, x] / Sqrt[x], {x, 0, BesselJZero[3, 1]}] + NSum[f[i], {i, 2, ∞}]

Out[2]= 0.573587
```

``NIntegrate`` gives the same result:

```wl
In[3]:= NIntegrate[BesselJ[3, x] / Sqrt[x], {x, 0, ∞}]

Out[3]= 0.573587
```

### Possible Issues (3)

Wynn's extrapolation algorithm loses precision for series that are not alternating:

```wl
In[1]:= NSum[(1/n^3), {n, 1, ∞}, Method -> "WynnEpsilon", WorkingPrecision -> 30]//Precision

Out[1]= 7.27779
```

---

Uses of ``NIntegrate`` by ``NSum`` may lead to convergence messages:

```wl
In[1]:= NSum[1 / n ^ (1001 / 1000), {n, 1, ∞}]
```

NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in n near {n} = {8.16907\*10^224}. NIntegrate obtained 997.8607733254651\` and 5.458904190527407\` for the integral and error estimates.

```wl
Out[1]= 1001.21
```

The error compared to the exact value of $\zeta \left(\frac{1001}{1000}\right)$ is large:

```wl
In[2]:= % - Zeta[1001 / 1000]

Out[2]= 0.629522
```

You can increase the appropriate parameters for ``NIntegrate`` using ``Method`` options:

```wl
In[3]:= NSum[1 / n ^ (1001 / 1000), {n, 1, ∞}, Method -> {"EulerMaclaurin", Method -> {"NIntegrate", "MaxRecursion" -> 20, Method -> "DoubleExponential"}}]

Out[3]= 1000.58
```

The error is much smaller:

```wl
In[4]:= % - Zeta[1001 / 1000]

Out[4]= 1.423744606654509`*^-7
```

Find the error versus the exact value of $\zeta \left(\frac{11}{10}\right)$ :

```wl
In[5]:= % - Zeta[11 / 10]

Out[5]= -10.5844
```

---

``NSum`` may not detect divergence for some infinite sums:

```wl
In[1]:= NSum[1 / k, {k, 1, ∞}]
```

NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in k near {k} = {8.16907\*10^224}. NIntegrate obtained 191609.51762979227\` and 160378.5178102709\` for the integral and error estimates.

```wl
Out[1]= 191613.
```

Convergence verification is based on a ratio test that is inconclusive when equal to 1:

```wl
In[2]:= Limit[(1 / (k + 1)) / (1 / k), k -> ∞]

Out[2]= 1
```

## See Also

* [`Total`](https://reference.wolfram.com/language/ref/Total.en.md)
* [`Plus`](https://reference.wolfram.com/language/ref/Plus.en.md)
* [`NProduct`](https://reference.wolfram.com/language/ref/NProduct.en.md)
* [`Sum`](https://reference.wolfram.com/language/ref/Sum.en.md)
* [`AsymptoticSum`](https://reference.wolfram.com/language/ref/AsymptoticSum.en.md)
* [`SumConvergence`](https://reference.wolfram.com/language/ref/SumConvergence.en.md)
* [`NIntegrate`](https://reference.wolfram.com/language/ref/NIntegrate.en.md)

## Tech Notes

* [Numerical Mathematics: Basic Operations](https://reference.wolfram.com/language/tutorial/NumericalCalculations.en.md)
* [Numerical Sums, Products and Integrals](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#3848)
* [Numerical Mathematics](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#20411)
* [Numerical Evaluation of Sums and Products](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnFunctions.en.md#10980)
* [Implementation notes: Numerical and Related Functions](https://reference.wolfram.com/language/tutorial/SomeNotesOnInternalImplementation.en.md#5064)

## Related Guides

* [Numerical Evaluation & Precision](https://reference.wolfram.com/language/guide/NumericalEvaluationAndPrecision.en.md)
* [Discrete Calculus](https://reference.wolfram.com/language/guide/DiscreteCalculus.en.md)
* [`Calculus`](https://reference.wolfram.com/language/guide/Calculus.en.md)

## History

* Introduced in 1988 (1.0) \| Updated in 2003 (5.0) ▪ [2007 (6.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn60.en.md)