Main Content


Numerical summation using variable precision



s = vpasum(f,a,b) numerically approximates the sum of a series defined by f from a to b. The default summation variable x in f is determined by symvar. The summation bounds a and b must be real.

vpasum(f,[a,b]) is equal to vpasum(f,a,b).


s = vpasum(f,x,a,b) performs numerical summation using the summation variable x.


collapse all

Numerically sum the symbolic expression 11.2x from 1 to 1,000.

syms x;
s = vpasum(1/1.2^x,1,1000)
s = 5.0vpa('5.0')

Find the summation of the symbolic function y(x)=x2 from a to b. Since the limits of summation must be real values, assume that the bounds a and b are real.

syms y(x)
syms a b real
y(x) = x^2;
s = vpasum(y,a,b)
s = 

x=abx2symsum(x^2, x, a, b)

Compare the computation time to evaluate symbolic and numerical summations.

Find the symbolic summation of the series k=1(-1)klog(k)k3 using symsum. Use vpa to numerically evaluate the symbolic summation using 32 significant digits. Measure the time required to declare the symbolic summation and evaluate its numerical value.

syms k
y = symsum((-1)^k*log(k)/k^3,k,1,Inf)
y = 

k=1-1klog(k)k3symsum(((-sym(1))^k*log(k))/k^3, k, 1, sym(inf))

yVpa = vpa(y)
yVpa = 0.059705906160195358363429266287926vpa('0.059705906160195358363429266287926')
Elapsed time is 2.459844 seconds.

To increase computation performance (shorten computation time), use vpasum to evaluate the same numerical summation without evaluating the symbolic summation.

y = vpasum((-1)^k*log(k)/k^3,k,1,Inf)
y = 0.059705906160195358363429266287926vpa('0.059705906160195358363429266287926')
Elapsed time is 0.197117 seconds.

Input Arguments

collapse all

Expression or function to sum, specified as a symbolic number, variable, function, expression, vector, matrix, or multidimensional array.

Limits of summation, specified as two comma-separated numbers, symbolic numbers, symbolic variables, symbolic functions, or symbolic expressions. Specifying the summation range from a to b can also be done using a vector with two elements. The limits of summation must be real.

Summation variable, specified as a symbolic variable. If x is not specified, the integration variable is determined by symvar(f).


Depending on whether the series is alternating or monotone, vpasum tries a number of strategies to calculate its limit: Levin's u-transformation, the Euler–Maclaurin formula, or van Wijngaarden's trick.

For example, the Euler–Maclaurin formula is


where B2m represents the 2mth Bernoulli number and Rp is an error term which depends on a, b, p, and f.


[1] Olver, F. W. J., A. B. Olde Daalhuis, D. W. Lozier, B. I. Schneider, R. F. Boisvert, C. W. Clark, B. R. Miller, B. V. Saunders, H. S. Cohl, and M. A. McClain, eds., Chapter 2.10 Sums and Sequences, NIST Digital Library of Mathematical Functions, Release 1.0.26 of 2020-03-15.

See Also

| |

Introduced in R2020b