This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Symbolic Summation

Symbolic Math Toolbox™ provides two functions for calculating sums:

  • sum finds the sum of elements of symbolic vectors and matrices. Unlike the MATLAB® sum, the symbolic sum function does not work on multidimensional arrays. For details, follow the MATLAB sum page.

  • symsum finds the sum of a symbolic series.

Comparing symsum and sum

You can find definite sums by using both sum and symsum. The sum function sums the input over a dimension, while the symsum function sums the input over an index.

Consider the definite sum S=k=1101k2. First, find the terms of the definite sum by substituting the index values for k in the expression. Then, sum the resulting vector using sum.

syms k
f = 1/k^2;
V = subs(f, k, 1:10)
S_sum = sum(V)
V =
[ 1, 1/4, 1/9, 1/16, 1/25, 1/36, 1/49, 1/64, 1/81, 1/100]
S_sum =
1968329/1270080

Find the same sum by using symsum by specifying the index and the summation limits. sum and symsum return identical results.

S_symsum = symsum(f, k, 1, 10)
S_symsum =
1968329/1270080

Computational Speed of symsum versus sum

For summing definite series, symsum can be faster than sum. For summing an indefinite series, you can only use symsum.

You can demonstrate that symsum can be faster than sum by summing a large definite series such as S=k=1100000k2.

To compare runtimes on your computer, use the following commands.

syms k
tic
sum(sym(1:100000).^2);
toc
tic
symsum(k^2, k, 1, 100000);
toc

Output Format Differences Between symsum and sum

symsum can provide a more elegant representation of sums than sum provides. Demonstrate this difference by comparing the function outputs for the definite series S=k=110xk. To simplify the solution, assume x > 1.

syms x
assume(x > 1)
S_sum = sum(x.^(1:10))
S_symsum = symsum(x^k, k, 1, 10)
S_sum =
x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x
S_symsum =
x^11/(x - 1) - x/(x - 1)

Show that the outputs are equal by using isAlways. The isAlways function returns logical 1 (true), meaning that the outputs are equal.

isAlways(S_sum == S_symsum)
ans =
  logical
     1

For further computations, clear the assumptions.

assume(x, 'clear')